Subversion Repositories XServices

Rev

Rev 114 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 114 Rev 127
Line 19... Line 19...
19
import java.io.ByteArrayInputStream;
19
import java.io.ByteArrayInputStream;
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.InputStream;
21
import java.io.InputStream;
22
import java.io.StringReader;
22
import java.io.StringReader;
23
import java.io.StringWriter;
23
import java.io.StringWriter;
-
 
24
import java.io.UnsupportedEncodingException;
-
 
25
import java.nio.charset.Charset;
-
 
26
import java.nio.charset.IllegalCharsetNameException;
24
import java.util.Iterator;
27
import java.util.Iterator;
25
import java.util.List;
28
import java.util.List;
26
import java.util.StringTokenizer;
29
import java.util.StringTokenizer;
Line 27... Line 30...
27
 
30
 
-
 
31
import javax.jws.WebService;
28
import javax.jws.WebService;
32
import javax.xml.namespace.QName;
29
import javax.xml.stream.XMLOutputFactory;
33
import javax.xml.stream.XMLOutputFactory;
30
import javax.xml.stream.XMLStreamException;
34
import javax.xml.stream.XMLStreamException;
31
import javax.xml.stream.XMLStreamReader;
35
import javax.xml.stream.XMLStreamReader;
-
 
36
import javax.xml.stream.XMLStreamWriter;
-
 
37
 
32
import javax.xml.stream.XMLStreamWriter;
38
import net.brutex.xservices.types.AttributeType;
33
import net.brutex.xservices.types.NamespaceListType;
39
import net.brutex.xservices.types.NamespaceListType;
-
 
40
import net.brutex.xservices.types.NamespaceType;
34
import net.brutex.xservices.types.NamespaceType;
41
import net.brutex.xservices.types.StringSplitType;
35
import net.brutex.xservices.types.ant.FileResource;
42
import net.brutex.xservices.types.ant.FileResource;
36
import net.brutex.xservices.ws.XServicesFault;
43
import net.brutex.xservices.ws.XServicesFault;
37
import net.brutex.xservices.ws.XmlService;
44
import net.brutex.xservices.ws.XmlService;
-
 
45
import org.apache.axiom.om.OMAbstractFactory;
38
import org.apache.axiom.om.OMAbstractFactory;
46
import org.apache.axiom.om.OMAttribute;
-
 
47
import org.apache.axiom.om.OMCloneOptions;
39
import org.apache.axiom.om.OMCloneOptions;
48
import org.apache.axiom.om.OMComment;
40
import org.apache.axiom.om.OMContainer;
49
import org.apache.axiom.om.OMContainer;
41
import org.apache.axiom.om.OMDocument;
50
import org.apache.axiom.om.OMDocument;
-
 
51
import org.apache.axiom.om.OMElement;
42
import org.apache.axiom.om.OMElement;
52
import org.apache.axiom.om.OMFactory;
-
 
53
import org.apache.axiom.om.OMNode;
43
import org.apache.axiom.om.OMNode;
54
import org.apache.axiom.om.OMProcessingInstruction;
44
import org.apache.axiom.om.OMText;
55
import org.apache.axiom.om.OMText;
45
import org.apache.axiom.om.OMXMLBuilderFactory;
56
import org.apache.axiom.om.OMXMLBuilderFactory;
46
import org.apache.axiom.om.xpath.AXIOMXPath;
57
import org.apache.axiom.om.xpath.AXIOMXPath;
47
import org.apache.log4j.Logger;
58
import org.apache.log4j.Logger;
Line 70... Line 81...
70
			} else {
81
			} else {
71
				throw new XServicesFault("No xmldata to insert.");
82
				throw new XServicesFault("No xmldata to insert.");
72
			}
83
			}
Line 73... Line 84...
73
 
84
 
74
			// Initialize XPath context
85
			// Initialize XPath context
75
			SimpleNamespaceContext context = new SimpleNamespaceContext();
-
 
76
			for (NamespaceType ns : nsList.getNamespaces()) {
-
 
77
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
-
 
78
				this.logger.debug(Messages.getString("XmlService.0")
-
 
79
						+ ns.getPrefix() + "=\"" + ns.getUri().toString()
-
 
80
						+ "\"'");
-
 
81
			}
86
			SimpleNamespaceContext context = createContext(nsList);
82
			axp.setNamespaceContext(context);
87
			axp.setNamespaceContext(context);
Line 83... Line 88...
83
			axp.addNamespaces(fragdoc.getOMDocumentElement());
88
			axp.addNamespaces(fragdoc.getOMDocumentElement());
Line 101... Line 106...
101
			e.printStackTrace();
106
			e.printStackTrace();
102
			throw new XServicesFault(e);
107
			throw new XServicesFault(e);
103
		}
108
		}
104
	}
109
	}
Line 105... Line 110...
105
 
110
 
106
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String replace) throws XServicesFault {
111
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
107
		try {
112
		try {
108
			AXIOMXPath axp = new AXIOMXPath(xpath);
-
 
109
 
113
				AXIOMXPath axp = new AXIOMXPath(xpath);
110
			InputStream is = res.getAntResource(null).getInputStream();
-
 
111
 
114
				InputStream is = res.getAntResource(null).getInputStream();
112
			XMLStreamReader reader = OMXMLBuilderFactory.createOMBuilder(is)
-
 
113
					.getDocument().getXMLStreamReader(false);
-
 
114
			StringWriter sw = new StringWriter();
-
 
115
			OMAbstractFactory.getOMFactory();
-
 
116
 
-
 
117
			while (reader.hasNext()) {
-
 
118
				if (reader.getEventType() == 1) {
-
 
119
					OMElement element = OMXMLBuilderFactory
-
 
120
							.createStAXOMBuilder(reader).getDocumentElement();
115
				OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
121
					element.build();
-
 
122
 
-
 
123
					SimpleNamespaceContext context = new SimpleNamespaceContext();
116
				OMDocument fragdoc = null;
124
					for (NamespaceType ns : nsList.getNamespaces()) {
-
 
125
						context.addNamespace(ns.getPrefix(), ns.getUri()
-
 
126
								.toString());
-
 
127
						this.logger.debug(Messages.getString(
-
 
128
								"XmlService.adding_namespace",
-
 
129
								new Object[] { ns.getPrefix(),
-
 
130
										ns.getUri().toString() }));
-
 
131
					}
-
 
132
					axp.setNamespaceContext(context);
-
 
133
					axp.addNamespaces(element);
-
 
134
 
117
				if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
135
					OMDocument xfrag = OMXMLBuilderFactory.createOMBuilder(
118
					fragdoc = OMXMLBuilderFactory.createOMBuilder(
136
							new StringReader("<XS>" + replace + "</XS>"))
119
							new StringReader("<XS>" + xmlFragment + "</XS>"))
137
							.getDocument();
-
 
138
					xfrag.build();
-
 
139
 
-
 
140
					List list = axp.selectNodes(element);
-
 
141
					for (Iterator localIterator2 = list.iterator(); localIterator2
-
 
142
							.hasNext();) {
-
 
143
						Object o = localIterator2.next();
-
 
144
						if (!(o instanceof OMNode))
-
 
145
							throw new XServicesFault(
-
 
146
									"You must match a node to be replaced, but you matched something differen (attribute, etc.");
-
 
147
						OMNode node = (OMNode) o;
-
 
148
 
-
 
149
						Iterator<?> children = xfrag.getOMDocumentElement()
-
 
150
								.getChildren();
-
 
151
						while (children.hasNext()) {
-
 
152
							OMNode container = (OMNode) children.next();
-
 
153
							node.insertSiblingAfter((OMNode) container
-
 
154
									.clone(new OMCloneOptions()));
-
 
155
						}
-
 
156
						node.detach();
-
 
157
 
-
 
158
						// node.insertSiblingAfter(AXIOMUtil.stringToOM(replace));
-
 
159
 
-
 
160
					}
-
 
161
					XMLOutputFactory xof = XMLOutputFactory.newInstance();
-
 
162
					XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
-
 
163
					element.serialize(writer);
-
 
164
					sw.flush();
120
							.getDocument();
165
				} else {
121
				} else {
166
					reader.next();
122
					throw new XServicesFault("No xmldata to insert.");
-
 
123
				}
-
 
124
 
-
 
125
				// Initialize XPath context
-
 
126
				SimpleNamespaceContext context = createContext(nsList);
-
 
127
				axp.setNamespaceContext(context);
-
 
128
				axp.addNamespaces(fragdoc.getOMDocumentElement());
-
 
129
 
-
 
130
				OMDocument document = replaceNodes(sourcedoc, axp, fragdoc);
-
 
131
 
-
 
132
				StringWriter sw = new StringWriter();
-
 
133
				XMLOutputFactory xof = XMLOutputFactory.newInstance();
-
 
134
				XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
-
 
135
				document.serialize(writer);
-
 
136
 
-
 
137
				this.logger.trace(sw.getBuffer().toString());
-
 
138
				return sw.getBuffer().toString();
-
 
139
			} catch (JaxenException e) {
-
 
140
				e.printStackTrace();
-
 
141
				throw new XServicesFault(e);
-
 
142
			} catch (XMLStreamException e) {
-
 
143
				e.printStackTrace();
-
 
144
				throw new XServicesFault(e);
-
 
145
			} catch (IOException e) {
-
 
146
				e.printStackTrace();
167
				}
147
				throw new XServicesFault(e);
168
			}
-
 
169
			this.logger.trace(sw.getBuffer().toString());
-
 
170
			return sw.getBuffer().toString();
-
 
171
		} catch (JaxenException e) {
-
 
172
			e.printStackTrace();
-
 
173
			throw new XServicesFault(e);
-
 
174
		} catch (IOException e) {
-
 
175
			e.printStackTrace();
-
 
176
			throw new XServicesFault(e);
-
 
177
		} catch (XMLStreamException e) {
-
 
178
			e.printStackTrace();
-
 
179
			throw new XServicesFault(e);
-
 
180
		}
148
			}
Line 181... Line 149...
181
	}
149
	}
-
 
150
 
182
 
151
	public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
183
	public String replaceNodes(String source, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
152
		encoding = validateEncoding(encoding);
184
		try {
153
		try {
185
			AXIOMXPath axp = new AXIOMXPath(xpath);
154
			AXIOMXPath axp = new AXIOMXPath(xpath);
186
			InputStream is = new ByteArrayInputStream(source.getBytes());
155
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
187
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
156
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
188
					.getDocument();
157
					.getDocument();
189
			OMDocument fragdoc = null;
158
			OMDocument fragdoc = null;
Line 194... Line 163...
194
			} else {
163
			} else {
195
				throw new XServicesFault("No xmldata to insert.");
164
				throw new XServicesFault("No xmldata to insert.");
196
			}
165
			}
Line 197... Line 166...
197
 
166
 
198
			// Initialize XPath context
167
			// Initialize XPath context
199
			SimpleNamespaceContext context = new SimpleNamespaceContext();
-
 
200
			for (NamespaceType ns : nsList.getNamespaces()) {
-
 
201
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
-
 
202
				this.logger.debug(Messages.getString("XmlService.0")
-
 
203
						+ ns.getPrefix() + "=\"" + ns.getUri().toString()
-
 
204
						+ "\"'");
-
 
205
			}
168
			SimpleNamespaceContext context = createContext(nsList);
206
			axp.setNamespaceContext(context);
169
			axp.setNamespaceContext(context);
Line 207... Line 170...
207
			axp.addNamespaces(fragdoc.getOMDocumentElement());
170
			axp.addNamespaces(fragdoc.getOMDocumentElement());
Line 219... Line 182...
219
			e.printStackTrace();
182
			e.printStackTrace();
220
			throw new XServicesFault(e);
183
			throw new XServicesFault(e);
221
		} catch (XMLStreamException e) {
184
		} catch (XMLStreamException e) {
222
			e.printStackTrace();
185
			e.printStackTrace();
223
			throw new XServicesFault(e);
186
			throw new XServicesFault(e);
-
 
187
		} catch (UnsupportedEncodingException e) {
-
 
188
			throw new XServicesFault(e);
224
		}
189
		}
225
	}
190
	}
Line 226... Line 191...
226
	
191
	
-
 
192
	public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
227
	public String insertNodes(String source, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
193
		encoding = validateEncoding(encoding);
228
		try {
194
		try {
229
			AXIOMXPath axp = new AXIOMXPath(xpath);
195
			AXIOMXPath axp = new AXIOMXPath(xpath);
230
			InputStream is = new ByteArrayInputStream(source.getBytes());
196
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
231
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
197
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
232
					.getDocument();
198
					.getDocument();
233
			OMDocument fragdoc = null;
199
			OMDocument fragdoc = null;
234
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
200
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
Line 238... Line 204...
238
			} else {
204
			} else {
239
				throw new XServicesFault("No xmldata to insert.");
205
				throw new XServicesFault("No xmldata to insert.");
240
			}
206
			}
Line 241... Line 207...
241
 
207
 
242
			// Initialize XPath context
208
			// Initialize XPath context
243
			SimpleNamespaceContext context = new SimpleNamespaceContext();
-
 
244
			for (NamespaceType ns : nsList.getNamespaces()) {
-
 
245
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
-
 
246
				this.logger.debug(Messages.getString("XmlService.0")
-
 
247
						+ ns.getPrefix() + "=\"" + ns.getUri().toString()
-
 
248
						+ "\"'");
-
 
249
			}
209
			SimpleNamespaceContext context = createContext(nsList);
250
			axp.setNamespaceContext(context);
210
			axp.setNamespaceContext(context);
Line 251... Line 211...
251
			axp.addNamespaces(fragdoc.getOMDocumentElement());
211
			axp.addNamespaces(fragdoc.getOMDocumentElement());
Line 263... Line 223...
263
			e.printStackTrace();
223
			e.printStackTrace();
264
			throw new XServicesFault(e);
224
			throw new XServicesFault(e);
265
		} catch (XMLStreamException e) {
225
		} catch (XMLStreamException e) {
266
			e.printStackTrace();
226
			e.printStackTrace();
267
			throw new XServicesFault(e);
227
			throw new XServicesFault(e);
-
 
228
		} catch (UnsupportedEncodingException e) {
-
 
229
			throw new XServicesFault(e);
268
		}
230
		}
269
	}
231
	}
Line 270... Line 232...
270
 
232
 
271
	public String wrapInCDATA(String data) throws XServicesFault {
233
	public String wrapInCDATA(String data) throws XServicesFault {
Line 278... Line 240...
278
		}
240
		}
Line 279... Line 241...
279
		
241
		
280
		result = "<![CDATA[" + result + "]]>";
242
		result = "<![CDATA[" + result + "]]>";
281
		return result;
243
		return result;
-
 
244
	}
-
 
245
 
-
 
246
	public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath) throws XServicesFault {
-
 
247
		encoding = validateEncoding(encoding);
-
 
248
		try {
-
 
249
			StringSplitType rarray = new StringSplitType();
-
 
250
			AXIOMXPath axp = new AXIOMXPath(xpath);
-
 
251
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
-
 
252
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
-
 
253
			
-
 
254
			// Initialize XPath context
-
 
255
			SimpleNamespaceContext context = createContext(nsList);
-
 
256
			
-
 
257
			axp.setNamespaceContext(context);
-
 
258
			List results = axp.selectNodes(sourcedoc);
-
 
259
			for(Object o : results) {
-
 
260
				String text = null;
-
 
261
				
-
 
262
				if(o instanceof OMNode) {
-
 
263
					switch (((OMNode)o).getType()) {
-
 
264
						case OMNode.TEXT_NODE:
-
 
265
							text = ((OMText)o).getText();
-
 
266
							break;
-
 
267
						case OMNode.COMMENT_NODE:
-
 
268
							text = ((OMComment)o).getValue();
-
 
269
							break;
-
 
270
						case OMNode.PI_NODE:
-
 
271
							text = ((OMProcessingInstruction)o).getValue();
-
 
272
							break;
-
 
273
						default:
-
 
274
							StringWriter sw = new StringWriter();
-
 
275
							XMLOutputFactory xof = XMLOutputFactory.newInstance();
-
 
276
							XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
-
 
277
							((OMNode)o).serialize(writer);
-
 
278
							writer.flush();
-
 
279
							text = sw.toString();							
-
 
280
					}					
-
 
281
				} else if(o instanceof OMAttribute) {
-
 
282
					text = ((OMAttribute)o).getAttributeValue();
-
 
283
				} else {
-
 
284
					text = String.valueOf(o);
-
 
285
				}
-
 
286
				rarray.addStringMatch(text);
-
 
287
			}
-
 
288
			
-
 
289
 
-
 
290
			return rarray;
-
 
291
			} catch (JaxenException e) {
-
 
292
			e.printStackTrace();
-
 
293
			throw new XServicesFault(e);
-
 
294
		} catch (XMLStreamException e) {
-
 
295
				// TODO Auto-generated catch block
-
 
296
			throw new XServicesFault(e.getMessage());
-
 
297
			} catch (UnsupportedEncodingException e) {
-
 
298
				throw new XServicesFault(e);
-
 
299
		}
-
 
300
	}
-
 
301
	
-
 
302
	public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath, AttributeType attr) throws XServicesFault {
-
 
303
		encoding = validateEncoding(encoding);
-
 
304
		try {
-
 
305
			StringSplitType rarray = new StringSplitType();
-
 
306
			AXIOMXPath axp = new AXIOMXPath(xpath);
-
 
307
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
-
 
308
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
-
 
309
			OMFactory fac = OMAbstractFactory.getOMFactory();
-
 
310
			
-
 
311
			// Initialize XPath context
-
 
312
			SimpleNamespaceContext context = createContext(nsList);
-
 
313
			
-
 
314
			axp.setNamespaceContext(context);
-
 
315
			List results = axp.selectNodes(sourcedoc);
-
 
316
			for(Object o : results) {
-
 
317
				String text = null;
-
 
318
				
-
 
319
				if(o instanceof OMNode) {
-
 
320
					switch (((OMNode)o).getType()) {
-
 
321
						case OMNode.ELEMENT_NODE:
-
 
322
							OMElement node = ((OMElement)o);
-
 
323
							if(attr.value == null) {
-
 
324
								node.removeAttribute( node.getAttribute(new QName(attr.name)));
-
 
325
							} else {
-
 
326
								node.addAttribute(attr.name, attr.value, node.getNamespace());
-
 
327
							}
-
 
328
							break;
-
 
329
						default:
-
 
330
							throw new XServicesFault("XPath expression did not match an element node.");
-
 
331
					}
-
 
332
				} else {
-
 
333
					throw new XServicesFault("XPath expression did not match a node.");
-
 
334
				}
-
 
335
			}
-
 
336
						
-
 
337
			StringWriter sw = new StringWriter();
-
 
338
			XMLOutputFactory xof = XMLOutputFactory.newInstance();
-
 
339
			XMLStreamWriter writer = xof.createXMLStreamWriter(sw);
-
 
340
			sourcedoc.serialize(writer);
-
 
341
			writer.flush();
-
 
342
			return sw.toString();	
-
 
343
			} catch (JaxenException e) {
-
 
344
			e.printStackTrace();
-
 
345
			throw new XServicesFault(e);
-
 
346
		} catch (XMLStreamException e) {
-
 
347
				// TODO Auto-generated catch block
-
 
348
			throw new XServicesFault(e.getMessage());
-
 
349
			} catch (UnsupportedEncodingException e) {
-
 
350
				throw new XServicesFault(e);
-
 
351
		}
-
 
352
	}
282
	}
353
	
283
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp,OMDocument xmlfragment) throws XServicesFault {
354
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp,OMDocument xmlfragment) throws XServicesFault {
284
		List<?> olist = null;
355
		List<?> olist = null;
285
		try {
356
		try {
286
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
357
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
Line 403... Line 474...
403
			match.detach();
474
			match.detach();
404
		}
475
		}
405
		xmldocument.build();
476
		xmldocument.build();
406
		return xmldocument;
477
		return xmldocument;
407
	}
478
	}
-
 
479
 
-
 
480
	private SimpleNamespaceContext createContext(NamespaceListType nsList) {
-
 
481
		// Initialize XPath context
-
 
482
		SimpleNamespaceContext context = new SimpleNamespaceContext();
-
 
483
		if(nsList != null) {
-
 
484
			for (NamespaceType ns : nsList.getNamespaces()) {
-
 
485
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
-
 
486
				this.logger.debug(Messages.getString("XmlService.0")
-
 
487
						+ ns.getPrefix() + "=\"" + ns.getUri().toString()
-
 
488
						+ "\"'");
-
 
489
			}
-
 
490
		} else {
-
 
491
			logger.debug("No namespaces defined.");
-
 
492
		}
-
 
493
		return context;
-
 
494
	}
-
 
495
 
-
 
496
	private String validateEncoding(String encoding) throws XServicesFault {
-
 
497
		if(encoding == null || encoding.equals("")) { encoding=Charset.defaultCharset().displayName(); }
-
 
498
		try { 
-
 
499
			Charset.isSupported(encoding);
-
 
500
		} catch (IllegalCharsetNameException e) {
-
 
501
			throw new XServicesFault("Endcoding '"+encoding+"' is not supported by this JRE.");
-
 
502
		}
-
 
503
		logger.debug("Setting source xml string encoding to '"+encoding+"'");
-
 
504
		return encoding;
-
 
505
	}
408
}
506
}