Search in sources :

Example 76 with OMFactory

use of org.apache.axiom.om.OMFactory in project webservices-axiom by apache.

the class TestDTDReader method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDocument document = factory.createOMDocument();
    factory.createOMDocType(document, "root", "-//MY//DTD", "my.dtd", "<!ELEMENT root (#PCDATA)>");
    factory.createOMElement("root", null, document);
    XMLStreamReader reader = document.getXMLStreamReader();
    // Note that according to the specification of the DTDReader interface, it is
    // allowed to look up the extension before reaching the DTD event.
    DTDReader dtdReader = (DTDReader) reader.getProperty(DTDReader.PROPERTY);
    assertNotNull(dtdReader);
    assertEquals(XMLStreamReader.DTD, reader.next());
    assertEquals("root", dtdReader.getRootName());
    assertEquals("-//MY//DTD", dtdReader.getPublicId());
    assertEquals("my.dtd", dtdReader.getSystemId());
    assertEquals("<!ELEMENT root (#PCDATA)>", reader.getText());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) DTDReader(org.apache.axiom.ext.stax.DTDReader) OMDocument(org.apache.axiom.om.OMDocument)

Example 77 with OMFactory

use of org.apache.axiom.om.OMFactory in project webservices-axiom by apache.

the class TestAddAttributeMultiple method runTest.

@Override
protected void runTest() throws Throwable {
    String expectedXML = "<AttributeTester xmlns:myAttr2NS=\"http://test-attributes-2.org\" " + "xmlns:myAttr1NS=\"http://test-attributes-1.org\" myAttr2NS:attrNumber=\"2\" myAttr1NS:attrNumber=\"1\" />";
    OMFactory omFactory = metaFactory.getOMFactory();
    OMNamespace attrNS1 = omFactory.createOMNamespace("http://test-attributes-1.org", "myAttr1NS");
    OMNamespace attrNS2 = omFactory.createOMNamespace("http://test-attributes-2.org", "myAttr2NS");
    OMElement omElement = omFactory.createOMElement("AttributeTester", null);
    strategy.addAttribute(omElement, "attrNumber", attrNS1, "1");
    strategy.addAttribute(omElement, "attrNumber", attrNS2, "2");
    assertAbout(xml()).that(xml(OMElement.class, omElement)).hasSameContentAs(expectedXML);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement)

Example 78 with OMFactory

use of org.apache.axiom.om.OMFactory in project webservices-axiom by apache.

the class TestAddAttributeGeneratedPrefix method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMNamespace otherNS = factory.createOMNamespace("urn:ns2", "p");
    OMElement parent = factory.createOMElement("parent", otherNS);
    if (defaultNamespaceInScope) {
        parent.declareDefaultNamespace("urn:test");
    }
    OMElement element = factory.createOMElement("test", otherNS, parent);
    OMAttribute attr = element.addAttribute("attr", "value", factory.createOMNamespace("urn:test", null));
    OMNamespace ns = attr.getNamespace();
    assertTrue(ns.getPrefix().length() > 0);
    Iterator<OMNamespace> it = element.getAllDeclaredNamespaces();
    assertTrue(it.hasNext());
    assertEquals(ns, it.next());
    assertFalse(it.hasNext());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 79 with OMFactory

use of org.apache.axiom.om.OMFactory in project webservices-axiom by apache.

the class TestAddAttributeReplace method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Use same namespace URI but different prefixes
    OMNamespace ns1 = factory.createOMNamespace("urn:ns", "p1");
    OMNamespace ns2 = factory.createOMNamespace("urn:ns", "p2");
    OMElement element = factory.createOMElement(new QName("test"));
    OMAttribute att1 = strategy.addAttribute(element, "test", ns1, "value1");
    OMAttribute att2 = strategy.addAttribute(element, "test", ns2, "value2");
    Iterator<OMAttribute> it = element.getAllAttributes();
    assertThat(it.hasNext()).isTrue();
    assertThat(it.next()).isSameAs(att2);
    assertThat(it.hasNext()).isFalse();
    assertThat(att1.getOwner()).isNull();
    assertThat(att2.getOwner()).isSameAs(element);
    assertThat(att1).hasValue("value1");
    assertThat(att2).hasValue("value2");
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 80 with OMFactory

use of org.apache.axiom.om.OMFactory in project webservices-axiom by apache.

the class TestAddAttributeReuseExistingPrefix method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement parent = factory.createOMElement("parent", null);
    OMElement element = factory.createOMElement("element", null, parent);
    parent.declareNamespace("urn:test", "p");
    OMAttribute attr = element.addAttribute("attr", "test", factory.createOMNamespace("urn:test", null));
    assertThat(attr).hasPrefix("p");
    assertThat(element).hasNoNamespaceDeclarations();
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

OMFactory (org.apache.axiom.om.OMFactory)270 OMElement (org.apache.axiom.om.OMElement)202 OMNamespace (org.apache.axiom.om.OMNamespace)101 QName (javax.xml.namespace.QName)71 StringReader (java.io.StringReader)37 OMText (org.apache.axiom.om.OMText)35 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)32 OMAttribute (org.apache.axiom.om.OMAttribute)31 PullOMDataSource (org.apache.axiom.ts.om.sourcedelement.util.PullOMDataSource)26 StringWriter (java.io.StringWriter)18 OMDocument (org.apache.axiom.om.OMDocument)18 DataHandler (javax.activation.DataHandler)14 XMLStreamReader (javax.xml.stream.XMLStreamReader)14 OMNode (org.apache.axiom.om.OMNode)14 OMException (org.apache.axiom.om.OMException)12 DataSource (javax.activation.DataSource)11 StringOMDataSource (org.apache.axiom.om.ds.StringOMDataSource)9 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)9 OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)8 Element (org.w3c.dom.Element)8