Subversion Repositories XServices

Rev

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

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