Subversion Repositories XServices

Rev

Rev 198 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
1
/*
1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
6
 *   You may obtain a copy of the License at
7
 *
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
14
 *   limitations under the License.
15
 */
15
 */
16
 
16
 
17
package net.brutex.xservices.ws.impl;
17
package net.brutex.xservices.ws.impl;
18
 
18
 
19
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayInputStream;
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.InputStream;
21
import java.io.InputStream;
22
import java.io.StringReader;
22
import java.io.StringReader;
23
import java.io.StringWriter;
23
import java.io.StringWriter;
24
import java.io.UnsupportedEncodingException;
24
import java.io.UnsupportedEncodingException;
25
import java.nio.charset.Charset;
25
import java.nio.charset.Charset;
26
import java.nio.charset.IllegalCharsetNameException;
26
import java.nio.charset.IllegalCharsetNameException;
27
import java.util.Iterator;
27
import java.util.Iterator;
28
import java.util.List;
28
import java.util.List;
29
import javax.jws.WebService;
29
import javax.jws.WebService;
30
import javax.xml.namespace.QName;
30
import javax.xml.namespace.QName;
31
import javax.xml.stream.XMLOutputFactory;
31
import javax.xml.stream.XMLOutputFactory;
32
import javax.xml.stream.XMLStreamException;
32
import javax.xml.stream.XMLStreamException;
33
import javax.xml.stream.XMLStreamWriter;
33
import javax.xml.stream.XMLStreamWriter;
-
 
34
 
34
 
35
import lombok.extern.slf4j.Slf4j;
35
import net.brutex.xservices.types.AttributeType;
36
import net.brutex.xservices.types.AttributeType;
36
import net.brutex.xservices.types.NamespaceListType;
37
import net.brutex.xservices.types.NamespaceListType;
37
import net.brutex.xservices.types.NamespaceType;
38
import net.brutex.xservices.types.NamespaceType;
38
import net.brutex.xservices.types.StringSplitType;
39
import net.brutex.xservices.types.StringSplitType;
39
import net.brutex.xservices.types.ant.FileResource;
40
import net.brutex.xservices.types.ant.FileResource;
40
import net.brutex.xservices.ws.XServicesFault;
41
import net.brutex.xservices.ws.XServicesFault;
41
import net.brutex.xservices.ws.XmlService;
42
import net.brutex.xservices.ws.XmlService;
42
 
43
 
43
import org.apache.axiom.om.OMAbstractFactory;
44
import org.apache.axiom.om.OMAbstractFactory;
44
import org.apache.axiom.om.OMAttribute;
45
import org.apache.axiom.om.OMAttribute;
45
import org.apache.axiom.om.OMCloneOptions;
46
import org.apache.axiom.om.OMCloneOptions;
46
import org.apache.axiom.om.OMComment;
47
import org.apache.axiom.om.OMComment;
47
import org.apache.axiom.om.OMContainer;
48
import org.apache.axiom.om.OMContainer;
48
import org.apache.axiom.om.OMDocument;
49
import org.apache.axiom.om.OMDocument;
49
import org.apache.axiom.om.OMElement;
50
import org.apache.axiom.om.OMElement;
50
import org.apache.axiom.om.OMFactory;
51
import org.apache.axiom.om.OMFactory;
51
import org.apache.axiom.om.OMNode;
52
import org.apache.axiom.om.OMNode;
52
import org.apache.axiom.om.OMProcessingInstruction;
53
import org.apache.axiom.om.OMProcessingInstruction;
53
import org.apache.axiom.om.OMText;
54
import org.apache.axiom.om.OMText;
54
import org.apache.axiom.om.OMXMLBuilderFactory;
55
import org.apache.axiom.om.OMXMLBuilderFactory;
55
import org.apache.axiom.om.xpath.AXIOMXPath;
56
import org.apache.axiom.om.xpath.AXIOMXPath;
56
 
-
 
57
import org.apache.logging.log4j.LogManager;
-
 
-
 
57
 
58
import org.apache.logging.log4j.Logger;
58
 
59
import org.apache.shiro.authz.annotation.RequiresPermissions;
59
import org.apache.shiro.authz.annotation.RequiresPermissions;
60
import org.jaxen.JaxenException;
60
import org.jaxen.JaxenException;
61
import org.jaxen.SimpleNamespaceContext;
61
import org.jaxen.SimpleNamespaceContext;
62
 
62
 
63
/**
63
/**
64
 * @author Brian Rosenberger, bru(at)brutex.de
64
 * @author Brian Rosenberger, bru(at)brutex.de
65
 * 
65
 * 
66
 */
66
 */
-
 
67
@Slf4j 
67
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.XmlService", serviceName = "XmlService")
68
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.XmlService", serviceName = "XmlService")
68
public class XmlServiceImpl implements XmlService {
69
public class XmlServiceImpl implements XmlService {
69
	private static final Logger logger = LogManager.getLogger();
-
 
-
 
70
 
70
 
71
 
71
	public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
72
	public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
72
			throws XServicesFault {
73
			throws XServicesFault {
73
		try {
74
		try {
74
			AXIOMXPath axp = new AXIOMXPath(xpath);
75
			AXIOMXPath axp = new AXIOMXPath(xpath);
75
			InputStream is = res.getAntResource(null).getInputStream();
76
			InputStream is = res.getAntResource(null).getInputStream();
76
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
77
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
77
			OMDocument fragdoc = null;
78
			OMDocument fragdoc = null;
78
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
79
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
79
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
80
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
80
						.getDocument();
81
						.getDocument();
81
			} else {
82
			} else {
82
				throw new XServicesFault("No xmldata to insert.");
83
				throw new XServicesFault("No xmldata to insert.");
83
			}
84
			}
84
 
85
 
85
			// Initialize XPath context
86
			// Initialize XPath context
86
			SimpleNamespaceContext context = createContext(nsList);
87
			SimpleNamespaceContext context = createContext(nsList);
87
			axp.setNamespaceContext(context);
88
			axp.setNamespaceContext(context);
88
			axp.addNamespaces(fragdoc.getOMDocumentElement());
89
			axp.addNamespaces(fragdoc.getOMDocumentElement());
89
 
90
 
90
			OMDocument document = insertNodes(sourcedoc, axp, fragdoc);
91
			OMDocument document = insertNodes(sourcedoc, axp, fragdoc);
91
 
92
 
92
			StringWriter sw = new StringWriter();
93
			StringWriter sw = new StringWriter();
93
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
94
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
94
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
95
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
95
			document.serialize(writer);
96
			document.serialize(writer);
96
 
97
 
97
			this.logger.trace(sw.getBuffer().toString());
98
			log.debug(sw.getBuffer().toString());
98
			return sw.getBuffer().toString();
99
			return sw.getBuffer().toString();
99
		} catch (JaxenException e) {
100
		} catch (JaxenException e) {
100
			e.printStackTrace();
101
			e.printStackTrace();
101
			throw new XServicesFault(e);
102
			throw new XServicesFault(e);
102
		} catch (IOException e) {
103
		} catch (IOException e) {
103
			e.printStackTrace();
104
			e.printStackTrace();
104
			throw new XServicesFault(e);
105
			throw new XServicesFault(e);
105
		} catch (XMLStreamException e) {
106
		} catch (XMLStreamException e) {
106
			e.printStackTrace();
107
			e.printStackTrace();
107
			throw new XServicesFault(e);
108
			throw new XServicesFault(e);
108
		}
109
		}
109
	}
110
	}
110
 
111
 
111
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
112
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
112
			throws XServicesFault {
113
			throws XServicesFault {
113
		try {
114
		try {
114
			AXIOMXPath axp = new AXIOMXPath(xpath);
115
			AXIOMXPath axp = new AXIOMXPath(xpath);
115
			InputStream is = res.getAntResource(null).getInputStream();
116
			InputStream is = res.getAntResource(null).getInputStream();
116
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
117
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
117
			OMDocument fragdoc = null;
118
			OMDocument fragdoc = null;
118
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
119
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
119
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
120
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
120
						.getDocument();
121
						.getDocument();
121
			} else {
122
			} else {
122
				throw new XServicesFault("No xmldata to insert.");
123
				throw new XServicesFault("No xmldata to insert.");
123
			}
124
			}
124
 
125
 
125
			// Initialize XPath context
126
			// Initialize XPath context
126
			SimpleNamespaceContext context = createContext(nsList);
127
			SimpleNamespaceContext context = createContext(nsList);
127
			axp.setNamespaceContext(context);
128
			axp.setNamespaceContext(context);
128
			axp.addNamespaces(fragdoc.getOMDocumentElement());
129
			axp.addNamespaces(fragdoc.getOMDocumentElement());
129
 
130
 
130
			OMDocument document = replaceNodes(sourcedoc, axp, fragdoc);
131
			OMDocument document = replaceNodes(sourcedoc, axp, fragdoc);
131
 
132
 
132
			StringWriter sw = new StringWriter();
133
			StringWriter sw = new StringWriter();
133
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
134
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
134
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
135
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
135
			document.serialize(writer);
136
			document.serialize(writer);
136
 
137
 
137
			this.logger.trace(sw.getBuffer().toString());
138
			log.debug(sw.getBuffer().toString());
138
			return sw.getBuffer().toString();
139
			return sw.getBuffer().toString();
139
		} catch (JaxenException e) {
140
		} catch (JaxenException e) {
140
			e.printStackTrace();
141
			e.printStackTrace();
141
			throw new XServicesFault(e);
142
			throw new XServicesFault(e);
142
		} catch (XMLStreamException e) {
143
		} catch (XMLStreamException e) {
143
			e.printStackTrace();
144
			e.printStackTrace();
144
			throw new XServicesFault(e);
145
			throw new XServicesFault(e);
145
		} catch (IOException e) {
146
		} catch (IOException e) {
146
			e.printStackTrace();
147
			e.printStackTrace();
147
			throw new XServicesFault(e);
148
			throw new XServicesFault(e);
148
		}
149
		}
149
	}
150
	}
150
 
151
 
151
	public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath,
152
	public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath,
152
			String xmlFragment) throws XServicesFault {
153
			String xmlFragment) throws XServicesFault {
153
		encoding = validateEncoding(encoding);
154
		encoding = validateEncoding(encoding);
154
		try {
155
		try {
155
			AXIOMXPath axp = new AXIOMXPath(xpath);
156
			AXIOMXPath axp = new AXIOMXPath(xpath);
156
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
157
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
157
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
158
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
158
			OMDocument fragdoc = null;
159
			OMDocument fragdoc = null;
159
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
160
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
160
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
161
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
161
						.getDocument();
162
						.getDocument();
162
			} else {
163
			} else {
163
				throw new XServicesFault("No xmldata to insert.");
164
				throw new XServicesFault("No xmldata to insert.");
164
			}
165
			}
165
 
166
 
166
			// Initialize XPath context
167
			// Initialize XPath context
167
			SimpleNamespaceContext context = createContext(nsList);
168
			SimpleNamespaceContext context = createContext(nsList);
168
			axp.setNamespaceContext(context);
169
			axp.setNamespaceContext(context);
169
			axp.addNamespaces(fragdoc.getOMDocumentElement());
170
			axp.addNamespaces(fragdoc.getOMDocumentElement());
170
 
171
 
171
			OMDocument document = replaceNodes(sourcedoc, axp, fragdoc);
172
			OMDocument document = replaceNodes(sourcedoc, axp, fragdoc);
172
 
173
 
173
			StringWriter sw = new StringWriter();
174
			StringWriter sw = new StringWriter();
174
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
175
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
175
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
176
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
176
			document.serialize(writer);
177
			document.serialize(writer);
177
 
178
 
178
			this.logger.trace(sw.getBuffer().toString());
179
			log.debug(sw.getBuffer().toString());
179
			return sw.getBuffer().toString();
180
			return sw.getBuffer().toString();
180
		} catch (JaxenException e) {
181
		} catch (JaxenException e) {
181
			e.printStackTrace();
182
			e.printStackTrace();
182
			throw new XServicesFault(e);
183
			throw new XServicesFault(e);
183
		} catch (XMLStreamException e) {
184
		} catch (XMLStreamException e) {
184
			e.printStackTrace();
185
			e.printStackTrace();
185
			throw new XServicesFault(e);
186
			throw new XServicesFault(e);
186
		} catch (UnsupportedEncodingException e) {
187
		} catch (UnsupportedEncodingException e) {
187
			throw new XServicesFault(e);
188
			throw new XServicesFault(e);
188
		}
189
		}
189
	}
190
	}
190
 
191
 
191
	@RequiresPermissions("insertNodes")
192
	@RequiresPermissions("insertNodes")
192
	public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath,
193
	public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath,
193
			String xmlFragment) throws XServicesFault {
194
			String xmlFragment) throws XServicesFault {
194
		encoding = validateEncoding(encoding);
195
		encoding = validateEncoding(encoding);
195
		try {
196
		try {
196
			AXIOMXPath axp = new AXIOMXPath(xpath);
197
			AXIOMXPath axp = new AXIOMXPath(xpath);
197
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
198
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
198
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
199
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
199
			OMDocument fragdoc = null;
200
			OMDocument fragdoc = null;
200
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
201
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
201
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
202
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
202
						.getDocument();
203
						.getDocument();
203
			} else {
204
			} else {
204
				throw new XServicesFault("No xmldata to insert.");
205
				throw new XServicesFault("No xmldata to insert.");
205
			}
206
			}
206
 
207
 
207
			// Initialize XPath context
208
			// Initialize XPath context
208
			SimpleNamespaceContext context = createContext(nsList);
209
			SimpleNamespaceContext context = createContext(nsList);
209
			axp.setNamespaceContext(context);
210
			axp.setNamespaceContext(context);
210
			axp.addNamespaces(fragdoc.getOMDocumentElement());
211
			axp.addNamespaces(fragdoc.getOMDocumentElement());
211
 
212
 
212
			OMDocument document = insertNodes(sourcedoc, axp, fragdoc);
213
			OMDocument document = insertNodes(sourcedoc, axp, fragdoc);
213
 
214
 
214
			StringWriter sw = new StringWriter();
215
			StringWriter sw = new StringWriter();
215
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
216
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
216
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
217
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
217
			document.serialize(writer);
218
			document.serialize(writer);
218
 
219
 
219
			this.logger.trace(sw.getBuffer().toString());
220
			log.debug(sw.getBuffer().toString());
220
			return sw.getBuffer().toString();
221
			return sw.getBuffer().toString();
221
		} catch (JaxenException e) {
222
		} catch (JaxenException e) {
222
			e.printStackTrace();
223
			e.printStackTrace();
223
			throw new XServicesFault(e);
224
			throw new XServicesFault(e);
224
		} catch (XMLStreamException e) {
225
		} catch (XMLStreamException e) {
225
			e.printStackTrace();
226
			e.printStackTrace();
226
			throw new XServicesFault(e);
227
			throw new XServicesFault(e);
227
		} catch (UnsupportedEncodingException e) {
228
		} catch (UnsupportedEncodingException e) {
228
			throw new XServicesFault(e);
229
			throw new XServicesFault(e);
229
		}
230
		}
230
	}
231
	}
231
 
232
 
232
	public String wrapInCDATA(String data) throws XServicesFault {
233
	public String wrapInCDATA(String data) throws XServicesFault {
233
		String result = "";
234
		String result = "";
234
		String[] tokens = data.split("\\]\\]>", -1);
235
		String[] tokens = data.split("\\]\\]>", -1);
235
 
236
 
236
		for (int i = 0; i < tokens.length; i++) {
237
		for (int i = 0; i < tokens.length; i++) {
237
			result += tokens[i];
238
			result += tokens[i];
238
			if (i + 1 < tokens.length)
239
			if (i + 1 < tokens.length)
239
				result += "]]]]><![CDATA[>";
240
				result += "]]]]><![CDATA[>";
240
		}
241
		}
241
 
242
 
242
		result = "<![CDATA[" + result + "]]>";
243
		result = "<![CDATA[" + result + "]]>";
243
		return result;
244
		return result;
244
	}
245
	}
245
 
246
 
246
	public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath)
247
	public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath)
247
			throws XServicesFault {
248
			throws XServicesFault {
248
		encoding = validateEncoding(encoding);
249
		encoding = validateEncoding(encoding);
249
		try {
250
		try {
250
			StringSplitType rarray = new StringSplitType();
251
			StringSplitType rarray = new StringSplitType();
251
			AXIOMXPath axp = new AXIOMXPath(xpath);
252
			AXIOMXPath axp = new AXIOMXPath(xpath);
252
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
253
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
253
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
254
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
254
 
255
 
255
			// Initialize XPath context
256
			// Initialize XPath context
256
			SimpleNamespaceContext context = createContext(nsList);
257
			SimpleNamespaceContext context = createContext(nsList);
257
 
258
 
258
			axp.setNamespaceContext(context);
259
			axp.setNamespaceContext(context);
259
			List results = axp.selectNodes(sourcedoc);
260
			List results = axp.selectNodes(sourcedoc);
260
			for (Object o : results) {
261
			for (Object o : results) {
261
				String text = null;
262
				String text = null;
262
 
263
 
263
				if (o instanceof OMNode) {
264
				if (o instanceof OMNode) {
264
					switch (((OMNode) o).getType()) {
265
					switch (((OMNode) o).getType()) {
265
					case OMNode.TEXT_NODE:
266
					case OMNode.TEXT_NODE:
266
						text = ((OMText) o).getText();
267
						text = ((OMText) o).getText();
267
						break;
268
						break;
268
					case OMNode.COMMENT_NODE:
269
					case OMNode.COMMENT_NODE:
269
						text = ((OMComment) o).getValue();
270
						text = ((OMComment) o).getValue();
270
						break;
271
						break;
271
					case OMNode.PI_NODE:
272
					case OMNode.PI_NODE:
272
						text = ((OMProcessingInstruction) o).getValue();
273
						text = ((OMProcessingInstruction) o).getValue();
273
						break;
274
						break;
274
					default:
275
					default:
275
						StringWriter sw = new StringWriter();
276
						StringWriter sw = new StringWriter();
276
						XMLOutputFactory xof = XMLOutputFactory.newInstance();
277
						XMLOutputFactory xof = XMLOutputFactory.newInstance();
277
						XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
278
						XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
278
						((OMNode) o).serialize(writer);
279
						((OMNode) o).serialize(writer);
279
						writer.flush();
280
						writer.flush();
280
						text = sw.toString();
281
						text = sw.toString();
281
					}
282
					}
282
				} else if (o instanceof OMAttribute) {
283
				} else if (o instanceof OMAttribute) {
283
					text = ((OMAttribute) o).getAttributeValue();
284
					text = ((OMAttribute) o).getAttributeValue();
284
				} else {
285
				} else {
285
					text = String.valueOf(o);
286
					text = String.valueOf(o);
286
				}
287
				}
287
				rarray.addStringMatch(text);
288
				rarray.addStringMatch(text);
288
			}
289
			}
289
 
290
 
290
			return rarray;
291
			return rarray;
291
		} catch (JaxenException e) {
292
		} catch (JaxenException e) {
292
			e.printStackTrace();
293
			e.printStackTrace();
293
			throw new XServicesFault(e);
294
			throw new XServicesFault(e);
294
		} catch (XMLStreamException e) {
295
		} catch (XMLStreamException e) {
295
			// TODO Auto-generated catch block
296
			// TODO Auto-generated catch block
296
			throw new XServicesFault(e.getMessage());
297
			throw new XServicesFault(e.getMessage());
297
		} catch (UnsupportedEncodingException e) {
298
		} catch (UnsupportedEncodingException e) {
298
			throw new XServicesFault(e);
299
			throw new XServicesFault(e);
299
		}
300
		}
300
	}
301
	}
301
 
302
 
302
	public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath,
303
	public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath,
303
			AttributeType attr) throws XServicesFault {
304
			AttributeType attr) throws XServicesFault {
304
		encoding = validateEncoding(encoding);
305
		encoding = validateEncoding(encoding);
305
		try {
306
		try {
306
			StringSplitType rarray = new StringSplitType();
307
			StringSplitType rarray = new StringSplitType();
307
			AXIOMXPath axp = new AXIOMXPath(xpath);
308
			AXIOMXPath axp = new AXIOMXPath(xpath);
308
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
309
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
309
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
310
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
310
			OMFactory fac = OMAbstractFactory.getOMFactory();
311
			OMFactory fac = OMAbstractFactory.getOMFactory();
311
 
312
 
312
			// Initialize XPath context
313
			// Initialize XPath context
313
			SimpleNamespaceContext context = createContext(nsList);
314
			SimpleNamespaceContext context = createContext(nsList);
314
 
315
 
315
			axp.setNamespaceContext(context);
316
			axp.setNamespaceContext(context);
316
			List results = axp.selectNodes(sourcedoc);
317
			List results = axp.selectNodes(sourcedoc);
317
			for (Object o : results) {
318
			for (Object o : results) {
318
				String text = null;
319
				String text = null;
319
 
320
 
320
				if (o instanceof OMNode) {
321
				if (o instanceof OMNode) {
321
					switch (((OMNode) o).getType()) {
322
					switch (((OMNode) o).getType()) {
322
					case OMNode.ELEMENT_NODE:
323
					case OMNode.ELEMENT_NODE:
323
						OMElement node = ((OMElement) o);
324
						OMElement node = ((OMElement) o);
324
						if (attr.value == null) {
325
						if (attr.value == null) {
325
							node.removeAttribute(node.getAttribute(new QName(attr.name)));
326
							node.removeAttribute(node.getAttribute(new QName(attr.name)));
326
						} else {
327
						} else {
327
							node.addAttribute(attr.name, attr.value, node.getNamespace());
328
							node.addAttribute(attr.name, attr.value, node.getNamespace());
328
						}
329
						}
329
						break;
330
						break;
330
					default:
331
					default:
331
						throw new XServicesFault("XPath expression did not match an element node.");
332
						throw new XServicesFault("XPath expression did not match an element node.");
332
					}
333
					}
333
				} else {
334
				} else {
334
					throw new XServicesFault("XPath expression did not match a node.");
335
					throw new XServicesFault("XPath expression did not match a node.");
335
				}
336
				}
336
			}
337
			}
337
 
338
 
338
			StringWriter sw = new StringWriter();
339
			StringWriter sw = new StringWriter();
339
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
340
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
340
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
341
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
341
			sourcedoc.serialize(writer);
342
			sourcedoc.serialize(writer);
342
			writer.flush();
343
			writer.flush();
343
			return sw.toString();
344
			return sw.toString();
344
		} catch (JaxenException e) {
345
		} catch (JaxenException e) {
345
			e.printStackTrace();
346
			e.printStackTrace();
346
			throw new XServicesFault(e);
347
			throw new XServicesFault(e);
347
		} catch (XMLStreamException e) {
348
		} catch (XMLStreamException e) {
348
			// TODO Auto-generated catch block
349
			// TODO Auto-generated catch block
349
			throw new XServicesFault(e.getMessage());
350
			throw new XServicesFault(e.getMessage());
350
		} catch (UnsupportedEncodingException e) {
351
		} catch (UnsupportedEncodingException e) {
351
			throw new XServicesFault(e);
352
			throw new XServicesFault(e);
352
		}
353
		}
353
	}
354
	}
354
 
355
 
355
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
356
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
356
			throws XServicesFault {
357
			throws XServicesFault {
357
		List<?> olist = null;
358
		List<?> olist = null;
358
		try {
359
		try {
359
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
360
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
360
			this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
361
			log.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
361
			this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
362
			log.debug("XPath root expression is: '" + axp.debug() + "'.");
362
		} catch (JaxenException e) {
363
		} catch (JaxenException e) {
363
			throw new XServicesFault(e.getMessage(), e);
364
			throw new XServicesFault(e.getMessage(), e);
364
		}
365
		}
365
		if (olist.size() == 0)
366
		if (olist.size() == 0)
366
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
367
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
367
 
368
 
368
		// Prepare children to insert
369
		// Prepare children to insert
369
		xmlfragment.build();
370
		xmlfragment.build();
370
 
371
 
371
		// Determine what has been matched
372
		// Determine what has been matched
372
		OMContainer match = null;
373
		OMContainer match = null;
373
		for (Object o : olist) {
374
		for (Object o : olist) {
374
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
375
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
375
			if ((o instanceof OMNode)) {
376
			if ((o instanceof OMNode)) {
376
				OMNode node = (OMNode) o;
377
				OMNode node = (OMNode) o;
377
				switch (node.getType()) {
378
				switch (node.getType()) {
378
				case OMNode.ELEMENT_NODE:
379
				case OMNode.ELEMENT_NODE:
379
					if ((o instanceof OMElement))
380
					if ((o instanceof OMElement))
380
						match = (OMElement) o;
381
						match = (OMElement) o;
381
					if ((o instanceof OMDocument))
382
					if ((o instanceof OMDocument))
382
						match = ((OMDocument) o).getOMDocumentElement();
383
						match = ((OMDocument) o).getOMDocumentElement();
383
					this.logger.debug(Messages.getString("XmlService.8"));
384
					log.debug(Messages.getString("XmlService.8"));
384
					break;
385
					break;
385
				case OMNode.TEXT_NODE:
386
				case OMNode.TEXT_NODE:
386
					match = ((OMText) o).getParent();
387
					match = ((OMText) o).getParent();
387
					this.logger.debug(Messages.getString("XmlService.9"));
388
					log.debug(Messages.getString("XmlService.9"));
388
					break;
389
					break;
389
				case OMNode.COMMENT_NODE:
390
				case OMNode.COMMENT_NODE:
390
					// match = node.getParent();
391
					// match = node.getParent();
391
					match = (OMContainer) node;
392
					match = (OMContainer) node;
392
					this.logger.debug(Messages.getString("XmlService.10"));
393
					log.debug(Messages.getString("XmlService.10"));
393
					break;
394
					break;
394
				default:
395
				default:
395
					this.logger
-
 
396
							.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
396
					log.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
397
					this.logger.error(Messages.getString("XmlService.11"));
397
					log.error(Messages.getString("XmlService.11"));
398
					throw new XServicesFault(Messages.getString("XmlService.12"));
398
					throw new XServicesFault(Messages.getString("XmlService.12"));
399
				}
399
				}
400
			} else {
400
			} else {
401
				this.logger.error("XPath matched " + o.getClass().getCanonicalName());
401
				log.error("XPath matched " + o.getClass().getCanonicalName());
402
				this.logger.error(Messages.getString("XmlService.11"));
402
				log.error(Messages.getString("XmlService.11"));
403
				throw new XServicesFault(Messages.getString("XmlService.12"));
403
				throw new XServicesFault(Messages.getString("XmlService.12"));
404
			}
404
			}
405
 
405
 
406
			while (children.hasNext()) {
406
			while (children.hasNext()) {
407
				OMNode container = (OMNode) children.next();
407
				OMNode container = (OMNode) children.next();
408
				match.addChild((OMNode) container.clone(new OMCloneOptions()));
408
				match.addChild((OMNode) container.clone(new OMCloneOptions()));
409
			}
409
			}
410
		}
410
		}
411
 
411
 
412
		xmldocument.build();
412
		xmldocument.build();
413
		return xmldocument;
413
		return xmldocument;
414
	}
414
	}
415
 
415
 
416
	private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
416
	private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
417
			throws XServicesFault {
417
			throws XServicesFault {
418
 
418
 
419
		List<?> olist = null;
419
		List<?> olist = null;
420
		try {
420
		try {
421
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
421
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
422
			this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
422
			log.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
423
			this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
423
			log.debug("XPath root expression is: '" + axp.debug() + "'.");
424
		} catch (JaxenException e) {
424
		} catch (JaxenException e) {
425
			throw new XServicesFault(e.getMessage(), e);
425
			throw new XServicesFault(e.getMessage(), e);
426
		}
426
		}
427
		if (olist.size() == 0)
427
		if (olist.size() == 0)
428
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
428
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
429
 
429
 
430
		// Prepare children to insert
430
		// Prepare children to insert
431
		xmlfragment.build();
431
		xmlfragment.build();
432
 
432
 
433
		// Determine what has been matched
433
		// Determine what has been matched
434
		OMNode match = null;
434
		OMNode match = null;
435
		for (Object o : olist) {
435
		for (Object o : olist) {
436
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
436
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
437
			if ((o instanceof OMNode)) {
437
			if ((o instanceof OMNode)) {
438
				OMNode node = (OMNode) o;
438
				OMNode node = (OMNode) o;
439
				switch (node.getType()) {
439
				switch (node.getType()) {
440
				case OMNode.ELEMENT_NODE:
440
				case OMNode.ELEMENT_NODE:
441
					if ((o instanceof OMElement))
441
					if ((o instanceof OMElement))
442
						match = (OMElement) o;
442
						match = (OMElement) o;
443
					if ((o instanceof OMDocument))
443
					if ((o instanceof OMDocument))
444
						match = ((OMDocument) o).getOMDocumentElement();
444
						match = ((OMDocument) o).getOMDocumentElement();
445
					this.logger.debug(Messages.getString("XmlService.8"));
445
					log.debug(Messages.getString("XmlService.8"));
446
					break;
446
					break;
447
				default:
447
				default:
448
					this.logger
-
 
449
							.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
448
					log.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
450
					this.logger.error(Messages.getString("XmlService.11"));
449
					log.error(Messages.getString("XmlService.11"));
451
					throw new XServicesFault(Messages.getString("XmlService.12"));
450
					throw new XServicesFault(Messages.getString("XmlService.12"));
452
				}
451
				}
453
			} else {
452
			} else {
454
				this.logger.error("XPath matched " + o.getClass().getCanonicalName());
453
				log.error("XPath matched " + o.getClass().getCanonicalName());
455
				this.logger.error(Messages.getString("XmlService.11"));
454
				log.error(Messages.getString("XmlService.11"));
456
				throw new XServicesFault(Messages.getString("XmlService.12"));
455
				throw new XServicesFault(Messages.getString("XmlService.12"));
457
			}
456
			}
458
 
457
 
459
			while (children.hasNext()) {
458
			while (children.hasNext()) {
460
				OMNode container = (OMNode) children.next();
459
				OMNode container = (OMNode) children.next();
461
				match.insertSiblingBefore((OMNode) container.clone(new OMCloneOptions()));
460
				match.insertSiblingBefore((OMNode) container.clone(new OMCloneOptions()));
462
			}
461
			}
463
			match.detach();
462
			match.detach();
464
		}
463
		}
465
		xmldocument.build();
464
		xmldocument.build();
466
		return xmldocument;
465
		return xmldocument;
467
	}
466
	}
468
 
467
 
469
	private SimpleNamespaceContext createContext(NamespaceListType nsList) {
468
	private SimpleNamespaceContext createContext(NamespaceListType nsList) {
470
		// Initialize XPath context
469
		// Initialize XPath context
471
		SimpleNamespaceContext context = new SimpleNamespaceContext();
470
		SimpleNamespaceContext context = new SimpleNamespaceContext();
472
		if (nsList != null) {
471
		if (nsList != null) {
473
			for (NamespaceType ns : nsList.getNamespaces()) {
472
			for (NamespaceType ns : nsList.getNamespaces()) {
474
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
473
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
475
				this.logger.debug(
474
				log.debug(
476
						Messages.getString("XmlService.0") + ns.getPrefix() + "=\"" + ns.getUri().toString() + "\"'");
475
						Messages.getString("XmlService.0") + ns.getPrefix() + "=\"" + ns.getUri().toString() + "\"'");
477
			}
476
			}
478
		} else {
477
		} else {
479
			logger.debug("No namespaces defined.");
478
			log.debug("No namespaces defined.");
480
		}
479
		}
481
		return context;
480
		return context;
482
	}
481
	}
483
 
482
 
484
	private String validateEncoding(String encoding) throws XServicesFault {
483
	private String validateEncoding(String encoding) throws XServicesFault {
485
		if (encoding == null || encoding.equals("")) {
484
		if (encoding == null || encoding.equals("")) {
486
			encoding = Charset.defaultCharset().displayName();
485
			encoding = Charset.defaultCharset().displayName();
487
		}
486
		}
488
		try {
487
		try {
489
			Charset.isSupported(encoding);
488
			Charset.isSupported(encoding);
490
		} catch (IllegalCharsetNameException e) {
489
		} catch (IllegalCharsetNameException e) {
491
			throw new XServicesFault("Endcoding '" + encoding + "' is not supported by this JRE.");
490
			throw new XServicesFault("Endcoding '" + encoding + "' is not supported by this JRE.");
492
		}
491
		}
493
		logger.debug("Setting source xml string encoding to '" + encoding + "'");
492
		log.debug("Setting source xml string encoding to '" + encoding + "'");
494
		return encoding;
493
		return encoding;
495
	}
494
	}
496
 
495
 
497
}
496
}