Subversion Repositories XServices

Rev

Rev 114 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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