Subversion Repositories XServices

Rev

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

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