You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by dk...@apache.org on 2011/02/08 23:24:37 UTC
svn commit: r1068627 [2/2] - in /webservices/commons/trunk/modules/neethi:
./ src/main/java/org/apache/neethi/ src/main/java/org/apache/neethi/builders/
src/main/java/org/apache/neethi/builders/converters/
src/main/java/org/apache/neethi/builders/xml/ ...
Modified: webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/builders/xml/XmlPrimitiveAssertion.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/builders/xml/XmlPrimitiveAssertion.java?rev=1068627&r1=1068626&r2=1068627&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/builders/xml/XmlPrimitiveAssertion.java (original)
+++ webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/builders/xml/XmlPrimitiveAssertion.java Tue Feb 8 22:24:36 2011
@@ -19,12 +19,25 @@
package org.apache.neethi.builders.xml;
+import java.util.Iterator;
+
import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.Comment;
+import javax.xml.stream.events.Namespace;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
import org.apache.neethi.All;
import org.apache.neethi.Assertion;
import org.apache.neethi.Constants;
@@ -34,25 +47,25 @@ import org.apache.neethi.PolicyComponent
import org.apache.neethi.PolicyRegistry;
/**
- * XmlPrimitiveAssertion wraps an OMElement s.t. any unknown elements can be
+ * XmlPrimitiveAssertion wraps an Element s.t. any unknown elements can be
* treated an assertions if there is no AssertionBuilder that can build an
- * assertion from that OMElement.
+ * assertion from that Element.
*
*/
public class XmlPrimitiveAssertion implements Assertion {
- OMElement element;
+ Element element;
boolean optional;
boolean ignorable;
/**
- * Constructs a XmlPrimitiveAssertion from an OMElement.
+ * Constructs a XmlPrimitiveAssertion from an Element.
*
* @param element
- * the OMElement from which the XmlAssertion is constructed
+ * the Element from which the XmlAssertion is constructed
*/
- public XmlPrimitiveAssertion(OMElement element) {
+ public XmlPrimitiveAssertion(Element element) {
setValue(element);
setOptionality(element);
setIgnorability(element);
@@ -60,28 +73,28 @@ public class XmlPrimitiveAssertion imple
/**
- * Returns the QName of the wrapped OMElement.
+ * Returns the QName of the wrapped Element.
*/
public QName getName() {
- return (element != null) ? element.getQName() : null;
+ return (element != null) ? new QName(element.getNamespaceURI(), element.getLocalName()) : null;
}
/**
- * Sets the wrapped OMElement.
+ * Sets the wrapped Element.
*
* @param element
- * the OMElement to be set as wrapped
+ * the Element to be set as wrapped
*/
- public void setValue(OMElement element) {
+ public void setValue(Element element) {
this.element = element;
}
/**
- * Returns the wrapped OMElement.
+ * Returns the wrapped Element.
*
- * @return the wrapped OMElement
+ * @return the wrapped Element
*/
- public OMElement getValue() {
+ public Element getValue() {
return element;
}
@@ -101,7 +114,7 @@ public class XmlPrimitiveAssertion imple
}
/**
- * Returns the partial normalized version of the wrapped OMElement, that is
+ * Returns the partial normalized version of the wrapped Element, that is
* assumed to be an assertion.
*/
public PolicyComponent normalize() {
@@ -110,14 +123,16 @@ public class XmlPrimitiveAssertion imple
ExactlyOne exactlyOne = new ExactlyOne();
All all = new All();
- OMElement omElement = element.cloneOMElement();
-
- OMAttribute att = omElement.getAttribute(Constants.Q_ELEM_OPTIONAL_ATTR);
- if (att == null) {
- att = omElement.getAttribute(Constants.Q_ELEM_OPTIONAL_15_ATTR);
+ Element element = (Element)this.element.cloneNode(true);
+ Attr attr = element.getAttributeNodeNS(Constants.URI_POLICY_NS, Constants.ATTR_OPTIONAL);
+ if (attr != null) {
+ element.removeAttributeNode(attr);
+ }
+ attr = element.getAttributeNodeNS(Constants.URI_POLICY_15_NS, Constants.ATTR_OPTIONAL);
+ if (attr != null) {
+ element.removeAttributeNode(attr);
}
- omElement.removeAttribute(att);
- all.addPolicyComponent(new XmlPrimitiveAssertion(omElement));
+ all.addPolicyComponent(new XmlPrimitiveAssertion(element));
exactlyOne.addPolicyComponent(all);
exactlyOne.addPolicyComponent(new All());
@@ -139,8 +154,7 @@ public class XmlPrimitiveAssertion imple
public void serialize(XMLStreamWriter writer) throws XMLStreamException {
if (element != null) {
- element.serialize(writer);
-
+ copyEvents(XMLInputFactory.newInstance().createXMLEventReader(new DOMSource(element)), writer);
} else {
throw new RuntimeException("Wrapped Element is not set");
}
@@ -153,26 +167,23 @@ public class XmlPrimitiveAssertion imple
return Constants.TYPE_ASSERTION;
}
- private void setOptionality(OMElement element) {
- OMAttribute attribute = element
- .getAttribute(Constants.Q_ELEM_OPTIONAL_ATTR);
+ private void setOptionality(Element element2) {
+ Attr attribute = element2.getAttributeNodeNS(Constants.URI_POLICY_NS, Constants.ATTR_OPTIONAL);
if (attribute == null) {
- attribute = element
- .getAttribute(Constants.Q_ELEM_OPTIONAL_15_ATTR);
+ attribute = element2.getAttributeNodeNS(Constants.URI_POLICY_15_NS, Constants.ATTR_OPTIONAL);
}
if (attribute != null) {
- this.optional = (new Boolean(attribute.getAttributeValue())
+ this.optional = (new Boolean(attribute.getValue())
.booleanValue());
} else {
this.optional = false;
}
}
- private void setIgnorability(OMElement element2) {
- OMAttribute attribute = element
- .getAttribute(Constants.Q_ELEM_IGNORABLE_15_ATTR);
+ private void setIgnorability(Element element2) {
+ Attr attribute = element2.getAttributeNodeNS(Constants.URI_POLICY_15_NS, Constants.ATTR_IGNORABLE);
if (attribute != null) {
- this.ignorable = (new Boolean(attribute.getAttributeValue())
+ this.ignorable = (new Boolean(attribute.getValue())
.booleanValue());
} else {
@@ -188,4 +199,74 @@ public class XmlPrimitiveAssertion imple
return getName().equals(((Assertion) policyComponent).getName());
}
+
+ private void copyEvents(XMLEventReader reader, XMLStreamWriter writer)
+ throws XMLStreamException {
+ while (reader.hasNext()) {
+ XMLEvent event = reader.nextEvent();
+
+ switch (event.getEventType()) {
+ case XMLEvent.ATTRIBUTE: {
+ Attribute attr = (Attribute) event;
+ QName name = attr.getName();
+ writer.writeAttribute(name.getPrefix(), name.getNamespaceURI(),
+ name.getLocalPart(), attr.getValue());
+ break;
+ }
+ case XMLEvent.START_DOCUMENT:
+ case XMLEvent.END_DOCUMENT:
+ //not doing this as we're in a partial write mode
+ return;
+
+ case XMLEvent.END_ELEMENT:
+ writer.writeEndElement();
+ break;
+
+ case XMLEvent.NAMESPACE: {
+ Namespace ns = (Namespace) event;
+ writer.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
+ break;
+ }
+
+ case XMLEvent.START_ELEMENT: {
+ StartElement se = event.asStartElement();
+ QName n = se.getName();
+ writer.writeStartElement(n.getPrefix(), n.getLocalPart(),
+ n.getNamespaceURI());
+ Iterator it = se.getNamespaces();
+ while (it.hasNext()) {
+ Namespace ns = (Namespace) it.next();
+ writer.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
+ }
+ it = se.getAttributes();
+ while (it.hasNext()) {
+ Attribute attr = (Attribute) it.next();
+ QName name = attr.getName();
+ writer.writeAttribute(name.getPrefix(), name.getNamespaceURI(),
+ name.getLocalPart(), attr.getValue());
+ }
+ }
+ break;
+
+ case XMLEvent.CHARACTERS: {
+ Characters ch = event.asCharacters();
+ String text = ch.getData();
+ if (ch.isCData()) {
+ writer.writeCData(text);
+ } else {
+ writer.writeCharacters(text);
+ }
+ }
+ break;
+
+ case XMLEvent.CDATA:
+ writer.writeCData(event.asCharacters().getData());
+ break;
+
+ case XMLEvent.COMMENT:
+ writer.writeComment(((Comment) event).getText());
+ break;
+ }
+ }
+ }
}
Modified: webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/MergeTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/MergeTest.java?rev=1068627&r1=1068626&r2=1068627&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/MergeTest.java (original)
+++ webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/MergeTest.java Tue Feb 8 22:24:36 2011
@@ -27,8 +27,20 @@ public class MergeTest extends PolicyTes
public MergeTest() {
super("MergeTest");
}
+ public void testOM() throws Exception {
+ doTest(3);
+ }
+ public void testDOM() throws Exception {
+ doTest(1);
+ }
+ public void testStax() throws Exception {
+ doTest(2);
+ }
+ public void testStream() throws Exception {
+ doTest(3);
+ }
- public void test() throws Exception {
+ public void doTest(int type) throws Exception {
String r1, r2, r3;
Policy p1, p2, p3, p4;
@@ -52,9 +64,9 @@ public class MergeTest extends PolicyTes
r2 = "samples2" + File.separator + "Policy" + f2 + ".xml";
r3 = "merged" + File.separator + f;
- p1 = getPolicy(r1);
- p2 = getPolicy(r2);
- p3 = getPolicy(r3);
+ p1 = getPolicy(r1, type);
+ p2 = getPolicy(r2, type);
+ p3 = getPolicy(r3, type);
// result
p4 = (Policy)p1.merge(p2);
Modified: webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/Normalize2Test.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/Normalize2Test.java?rev=1068627&r1=1068626&r2=1068627&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/Normalize2Test.java (original)
+++ webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/Normalize2Test.java Tue Feb 8 22:24:36 2011
@@ -28,8 +28,20 @@ public class Normalize2Test extends Poli
public Normalize2Test() {
super("Normalize2Test");
}
+ public void testOM() throws Exception {
+ doTest(3);
+ }
+ public void testDOM() throws Exception {
+ doTest(1);
+ }
+ public void testStax() throws Exception {
+ doTest(2);
+ }
+ public void testStream() throws Exception {
+ doTest(3);
+ }
- public void test() throws Exception {
+ public void doTest(int type) throws Exception {
String r1, r2;
Policy p1, p2;
@@ -38,10 +50,10 @@ public class Normalize2Test extends Poli
r1 = "samples2" + File.separator + "Policy" + i + ".xml";
r2 = "normalized2" + File.separator + "Policy" + i + ".xml";
- p1 = getPolicy(r1);
+ p1 = getPolicy(r1, type);
p1 = (Policy)p1.normalize(true);
- p2 = getPolicy(r2);
+ p2 = getPolicy(r2, type);
if (!PolicyComparator.compare(p1, p2)) {
fail("samples2" + File.separator + "Policy" + i + ".normalize() FAILED");
Modified: webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/NormalizeTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/NormalizeTest.java?rev=1068627&r1=1068626&r2=1068627&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/NormalizeTest.java (original)
+++ webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/NormalizeTest.java Tue Feb 8 22:24:36 2011
@@ -34,7 +34,20 @@ public class NormalizeTest extends Polic
super("NormalizeTest");
}
- public void test() throws Exception {
+ public void testOM() throws Exception {
+ doTest(3);
+ }
+ public void testDOM() throws Exception {
+ doTest(1);
+ }
+ public void testStax() throws Exception {
+ doTest(2);
+ }
+ public void testStream() throws Exception {
+ doTest(3);
+ }
+
+ public void doTest(int type) throws Exception {
String r1, r2;
Policy p1, p2;
@@ -44,9 +57,9 @@ public class NormalizeTest extends Polic
r2 = "normalized" + File.separator + "test" + i + ".xml";
- p1 = getPolicy(r1);
+ p1 = getPolicy(r1, type);
p1 = (Policy) p1.normalize(true);
- p2 = getPolicy(r2);
+ p2 = getPolicy(r2, type);
if (!PolicyComparator.compare(p1, p2)) {
XMLStreamWriter writer;
Modified: webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/PolicyTestCase.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/PolicyTestCase.java?rev=1068627&r1=1068626&r2=1068627&view=diff
==============================================================================
--- webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/PolicyTestCase.java (original)
+++ webservices/commons/trunk/modules/neethi/src/test/java/org/apache/neethi/PolicyTestCase.java Tue Feb 8 22:24:36 2011
@@ -22,9 +22,20 @@ package org.apache.neethi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.w3c.dom.Element;
+
+import org.xml.sax.SAXException;
import junit.framework.TestCase;
@@ -45,34 +56,58 @@ public abstract class PolicyTestCase ext
}
}
- public Policy getPolicy(String name) {
- return PolicyEngine.getPolicy(getResourceAsElement(name));
+ public Policy getPolicy(String name) throws Exception {
+ return getPolicy(name, 3);
+ }
+ public Policy getPolicy(String name, int type) throws Exception {
+ switch (type) {
+ case 0:
+ return PolicyEngine.getPolicy(getResource(name));
+ case 1:
+ return PolicyEngine.getPolicy(getResourceAsDOM(name));
+ case 2:
+ return PolicyEngine.getPolicy(getResourceAsStax(name));
+ default:
+ return PolicyEngine.getPolicy(getResourceAsElement(name));
+ }
}
- public InputStream getResource(String name) {
+ public InputStream getResource(String name) throws FileNotFoundException {
String filePath = new File(testResourceDir, name).getAbsolutePath();
+ return new FileInputStream(filePath);
+ }
+ public Element getResourceAsDOM(String name)
+ throws ParserConfigurationException, SAXException, IOException {
+ InputStream in = getResource(name);
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
+ dbf.setValidating(false);
+ dbf.setIgnoringComments(false);
+ dbf.setIgnoringElementContentWhitespace(true);
+ dbf.setNamespaceAware(true);
+ // dbf.setCoalescing(true);
+ // dbf.setExpandEntityReferences(true);
- try {
- FileInputStream fis = new FileInputStream(filePath);
- return fis;
- } catch (FileNotFoundException e) {
- fail("Cannot get resource: " + e.getMessage());
- throw new RuntimeException();
- }
+ DocumentBuilder db = null;
+ db = dbf.newDocumentBuilder();
+
+ // db.setErrorHandler( new MyErrorHandler());
+
+ return db.parse(in).getDocumentElement();
}
- public OMElement getResourceAsElement(String name) {
- try {
- InputStream in = getResource(name);
- OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(
- OMAbstractFactory.getOMFactory(),
- XMLInputFactory.newInstance().createXMLStreamReader(in)).getDocumentElement();
- return element;
-
- } catch (Exception e) {
- fail("Cannot get resource: " + e.getMessage());
- throw new RuntimeException();
- }
+ public XMLStreamReader getResourceAsStax(String name)
+ throws XMLStreamException, FactoryConfigurationError, FileNotFoundException {
+ InputStream in = getResource(name);
+ return XMLInputFactory.newInstance().createXMLStreamReader(in);
+ }
+ public OMElement getResourceAsElement(String name)
+ throws XMLStreamException, FactoryConfigurationError, FileNotFoundException {
+ InputStream in = getResource(name);
+ OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(
+ OMAbstractFactory.getOMFactory(),
+ XMLInputFactory.newInstance().createXMLStreamReader(in)).getDocumentElement();
+ return element;
}
}