21,26 → 21,37 |
import java.io.InputStream; |
import java.io.StringReader; |
import java.io.StringWriter; |
import java.io.UnsupportedEncodingException; |
import java.nio.charset.Charset; |
import java.nio.charset.IllegalCharsetNameException; |
import java.util.Iterator; |
import java.util.List; |
import java.util.StringTokenizer; |
|
import javax.jws.WebService; |
import javax.xml.namespace.QName; |
import javax.xml.stream.XMLOutputFactory; |
import javax.xml.stream.XMLStreamException; |
import javax.xml.stream.XMLStreamReader; |
import javax.xml.stream.XMLStreamWriter; |
|
import net.brutex.xservices.types.AttributeType; |
import net.brutex.xservices.types.NamespaceListType; |
import net.brutex.xservices.types.NamespaceType; |
import net.brutex.xservices.types.StringSplitType; |
import net.brutex.xservices.types.ant.FileResource; |
import net.brutex.xservices.ws.XServicesFault; |
import net.brutex.xservices.ws.XmlService; |
import org.apache.axiom.om.OMAbstractFactory; |
import org.apache.axiom.om.OMAttribute; |
import org.apache.axiom.om.OMCloneOptions; |
import org.apache.axiom.om.OMComment; |
import org.apache.axiom.om.OMContainer; |
import org.apache.axiom.om.OMDocument; |
import org.apache.axiom.om.OMElement; |
import org.apache.axiom.om.OMFactory; |
import org.apache.axiom.om.OMNode; |
import org.apache.axiom.om.OMProcessingInstruction; |
import org.apache.axiom.om.OMText; |
import org.apache.axiom.om.OMXMLBuilderFactory; |
import org.apache.axiom.om.xpath.AXIOMXPath; |
72,13 → 83,7 |
} |
|
// Initialize XPath context |
SimpleNamespaceContext context = new SimpleNamespaceContext(); |
for (NamespaceType ns : nsList.getNamespaces()) { |
context.addNamespace(ns.getPrefix(), ns.getUri().toString()); |
this.logger.debug(Messages.getString("XmlService.0") |
+ ns.getPrefix() + "=\"" + ns.getUri().toString() |
+ "\"'"); |
} |
SimpleNamespaceContext context = createContext(nsList); |
axp.setNamespaceContext(context); |
axp.addNamespaces(fragdoc.getOMDocumentElement()); |
|
103,87 → 108,51 |
} |
} |
|
public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String replace) throws XServicesFault { |
public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault { |
try { |
AXIOMXPath axp = new AXIOMXPath(xpath); |
|
InputStream is = res.getAntResource(null).getInputStream(); |
|
XMLStreamReader reader = OMXMLBuilderFactory.createOMBuilder(is) |
.getDocument().getXMLStreamReader(false); |
StringWriter sw = new StringWriter(); |
OMAbstractFactory.getOMFactory(); |
|
while (reader.hasNext()) { |
if (reader.getEventType() == 1) { |
OMElement element = OMXMLBuilderFactory |
.createStAXOMBuilder(reader).getDocumentElement(); |
element.build(); |
|
SimpleNamespaceContext context = new SimpleNamespaceContext(); |
for (NamespaceType ns : nsList.getNamespaces()) { |
context.addNamespace(ns.getPrefix(), ns.getUri() |
.toString()); |
this.logger.debug(Messages.getString( |
"XmlService.adding_namespace", |
new Object[] { ns.getPrefix(), |
ns.getUri().toString() })); |
} |
axp.setNamespaceContext(context); |
axp.addNamespaces(element); |
|
OMDocument xfrag = OMXMLBuilderFactory.createOMBuilder( |
new StringReader("<XS>" + replace + "</XS>")) |
AXIOMXPath axp = new AXIOMXPath(xpath); |
InputStream is = res.getAntResource(null).getInputStream(); |
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument(); |
OMDocument fragdoc = null; |
if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) { |
fragdoc = OMXMLBuilderFactory.createOMBuilder( |
new StringReader("<XS>" + xmlFragment + "</XS>")) |
.getDocument(); |
xfrag.build(); |
} else { |
throw new XServicesFault("No xmldata to insert."); |
} |
|
List list = axp.selectNodes(element); |
for (Iterator localIterator2 = list.iterator(); localIterator2 |
.hasNext();) { |
Object o = localIterator2.next(); |
if (!(o instanceof OMNode)) |
throw new XServicesFault( |
"You must match a node to be replaced, but you matched something differen (attribute, etc."); |
OMNode node = (OMNode) o; |
// Initialize XPath context |
SimpleNamespaceContext context = createContext(nsList); |
axp.setNamespaceContext(context); |
axp.addNamespaces(fragdoc.getOMDocumentElement()); |
|
Iterator<?> children = xfrag.getOMDocumentElement() |
.getChildren(); |
while (children.hasNext()) { |
OMNode container = (OMNode) children.next(); |
node.insertSiblingAfter((OMNode) container |
.clone(new OMCloneOptions())); |
} |
node.detach(); |
OMDocument document = replaceNodes(sourcedoc, axp, fragdoc); |
|
// node.insertSiblingAfter(AXIOMUtil.stringToOM(replace)); |
StringWriter sw = new StringWriter(); |
XMLOutputFactory xof = XMLOutputFactory.newInstance(); |
XMLStreamWriter writer = xof.createXMLStreamWriter(sw); |
document.serialize(writer); |
|
} |
XMLOutputFactory xof = XMLOutputFactory.newInstance(); |
XMLStreamWriter writer = xof.createXMLStreamWriter(sw); |
element.serialize(writer); |
sw.flush(); |
} else { |
reader.next(); |
} |
this.logger.trace(sw.getBuffer().toString()); |
return sw.getBuffer().toString(); |
} catch (JaxenException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (XMLStreamException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (IOException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} |
this.logger.trace(sw.getBuffer().toString()); |
return sw.getBuffer().toString(); |
} catch (JaxenException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (IOException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (XMLStreamException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} |
} |
|
public String replaceNodes(String source, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault { |
public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault { |
encoding = validateEncoding(encoding); |
try { |
AXIOMXPath axp = new AXIOMXPath(xpath); |
InputStream is = new ByteArrayInputStream(source.getBytes()); |
InputStream is = new ByteArrayInputStream(source.getBytes(encoding)); |
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is) |
.getDocument(); |
OMDocument fragdoc = null; |
196,13 → 165,7 |
} |
|
// Initialize XPath context |
SimpleNamespaceContext context = new SimpleNamespaceContext(); |
for (NamespaceType ns : nsList.getNamespaces()) { |
context.addNamespace(ns.getPrefix(), ns.getUri().toString()); |
this.logger.debug(Messages.getString("XmlService.0") |
+ ns.getPrefix() + "=\"" + ns.getUri().toString() |
+ "\"'"); |
} |
SimpleNamespaceContext context = createContext(nsList); |
axp.setNamespaceContext(context); |
axp.addNamespaces(fragdoc.getOMDocumentElement()); |
|
221,13 → 184,16 |
} catch (XMLStreamException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (UnsupportedEncodingException e) { |
throw new XServicesFault(e); |
} |
} |
|
public String insertNodes(String source, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault { |
public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault { |
encoding = validateEncoding(encoding); |
try { |
AXIOMXPath axp = new AXIOMXPath(xpath); |
InputStream is = new ByteArrayInputStream(source.getBytes()); |
InputStream is = new ByteArrayInputStream(source.getBytes(encoding)); |
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is) |
.getDocument(); |
OMDocument fragdoc = null; |
240,13 → 206,7 |
} |
|
// Initialize XPath context |
SimpleNamespaceContext context = new SimpleNamespaceContext(); |
for (NamespaceType ns : nsList.getNamespaces()) { |
context.addNamespace(ns.getPrefix(), ns.getUri().toString()); |
this.logger.debug(Messages.getString("XmlService.0") |
+ ns.getPrefix() + "=\"" + ns.getUri().toString() |
+ "\"'"); |
} |
SimpleNamespaceContext context = createContext(nsList); |
axp.setNamespaceContext(context); |
axp.addNamespaces(fragdoc.getOMDocumentElement()); |
|
265,6 → 225,8 |
} catch (XMLStreamException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (UnsupportedEncodingException e) { |
throw new XServicesFault(e); |
} |
} |
|
280,6 → 242,115 |
result = "<![CDATA[" + result + "]]>"; |
return result; |
} |
|
public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath) throws XServicesFault { |
encoding = validateEncoding(encoding); |
try { |
StringSplitType rarray = new StringSplitType(); |
AXIOMXPath axp = new AXIOMXPath(xpath); |
InputStream is = new ByteArrayInputStream(source.getBytes(encoding)); |
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument(); |
|
// Initialize XPath context |
SimpleNamespaceContext context = createContext(nsList); |
|
axp.setNamespaceContext(context); |
List results = axp.selectNodes(sourcedoc); |
for(Object o : results) { |
String text = null; |
|
if(o instanceof OMNode) { |
switch (((OMNode)o).getType()) { |
case OMNode.TEXT_NODE: |
text = ((OMText)o).getText(); |
break; |
case OMNode.COMMENT_NODE: |
text = ((OMComment)o).getValue(); |
break; |
case OMNode.PI_NODE: |
text = ((OMProcessingInstruction)o).getValue(); |
break; |
default: |
StringWriter sw = new StringWriter(); |
XMLOutputFactory xof = XMLOutputFactory.newInstance(); |
XMLStreamWriter writer = xof.createXMLStreamWriter(sw); |
((OMNode)o).serialize(writer); |
writer.flush(); |
text = sw.toString(); |
} |
} else if(o instanceof OMAttribute) { |
text = ((OMAttribute)o).getAttributeValue(); |
} else { |
text = String.valueOf(o); |
} |
rarray.addStringMatch(text); |
} |
|
|
return rarray; |
} catch (JaxenException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (XMLStreamException e) { |
// TODO Auto-generated catch block |
throw new XServicesFault(e.getMessage()); |
} catch (UnsupportedEncodingException e) { |
throw new XServicesFault(e); |
} |
} |
|
public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath, AttributeType attr) throws XServicesFault { |
encoding = validateEncoding(encoding); |
try { |
StringSplitType rarray = new StringSplitType(); |
AXIOMXPath axp = new AXIOMXPath(xpath); |
InputStream is = new ByteArrayInputStream(source.getBytes(encoding)); |
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument(); |
OMFactory fac = OMAbstractFactory.getOMFactory(); |
|
// Initialize XPath context |
SimpleNamespaceContext context = createContext(nsList); |
|
axp.setNamespaceContext(context); |
List results = axp.selectNodes(sourcedoc); |
for(Object o : results) { |
String text = null; |
|
if(o instanceof OMNode) { |
switch (((OMNode)o).getType()) { |
case OMNode.ELEMENT_NODE: |
OMElement node = ((OMElement)o); |
if(attr.value == null) { |
node.removeAttribute( node.getAttribute(new QName(attr.name))); |
} else { |
node.addAttribute(attr.name, attr.value, node.getNamespace()); |
} |
break; |
default: |
throw new XServicesFault("XPath expression did not match an element node."); |
} |
} else { |
throw new XServicesFault("XPath expression did not match a node."); |
} |
} |
|
StringWriter sw = new StringWriter(); |
XMLOutputFactory xof = XMLOutputFactory.newInstance(); |
XMLStreamWriter writer = xof.createXMLStreamWriter(sw); |
sourcedoc.serialize(writer); |
writer.flush(); |
return sw.toString(); |
} catch (JaxenException e) { |
e.printStackTrace(); |
throw new XServicesFault(e); |
} catch (XMLStreamException e) { |
// TODO Auto-generated catch block |
throw new XServicesFault(e.getMessage()); |
} catch (UnsupportedEncodingException e) { |
throw new XServicesFault(e); |
} |
} |
|
private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp,OMDocument xmlfragment) throws XServicesFault { |
List<?> olist = null; |
try { |
405,4 → 476,31 |
xmldocument.build(); |
return xmldocument; |
} |
|
private SimpleNamespaceContext createContext(NamespaceListType nsList) { |
// Initialize XPath context |
SimpleNamespaceContext context = new SimpleNamespaceContext(); |
if(nsList != null) { |
for (NamespaceType ns : nsList.getNamespaces()) { |
context.addNamespace(ns.getPrefix(), ns.getUri().toString()); |
this.logger.debug(Messages.getString("XmlService.0") |
+ ns.getPrefix() + "=\"" + ns.getUri().toString() |
+ "\"'"); |
} |
} else { |
logger.debug("No namespaces defined."); |
} |
return context; |
} |
|
private String validateEncoding(String encoding) throws XServicesFault { |
if(encoding == null || encoding.equals("")) { encoding=Charset.defaultCharset().displayName(); } |
try { |
Charset.isSupported(encoding); |
} catch (IllegalCharsetNameException e) { |
throw new XServicesFault("Endcoding '"+encoding+"' is not supported by this JRE."); |
} |
logger.debug("Setting source xml string encoding to '"+encoding+"'"); |
return encoding; |
} |
} |