Subversion Repositories XServices

Rev

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

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