Java Code Examples for org.xml.sax.helpers.NamespaceSupport#declarePrefix()

The following examples show how to use org.xml.sax.helpers.NamespaceSupport#declarePrefix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSchemaValidationTube.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if (node==null || node.getNodeType()!=Node.ELEMENT_NODE) {
        return;
    }

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            //continue;
        }
    }
}
 
Example 2
Source File: NamespaceSupportTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testPush_PopContext() {
    int count;

    ns = new NamespaceSupport();
    count = countPrefixes();

    ns.pushContext();
    ns.declarePrefix("dc", "http://www.purl.org/dc#");
    assertEquals("Test 1: Incorrect prefix count;",
            count + 1, countPrefixes());

    ns.popContext();
    assertEquals("Test 2: Incorrect prefix count;",
            count, countPrefixes());

    // Check that only one context has been created by pushContext().
    try {
        ns.popContext();
        fail("Test 3: EmptyStackException expected.");
    } catch (EmptyStackException e) {
        // Expected.
    }
}
 
Example 3
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testProcessName() {
    NamespaceSupport nssupport = new NamespaceSupport();

    nssupport.pushContext();
    nssupport.declarePrefix("", "http://www.java.com");
    nssupport.declarePrefix("dc", "http://www.purl.org/dc");

    String[] parts = new String[3];
    nssupport.processName("dc:name1", parts, false);
    Assert.assertTrue(parts[0].equals("http://www.purl.org/dc"));
    Assert.assertTrue(parts[1].equals("name1"));
    Assert.assertTrue(parts[2].equals("dc:name1"));

    nssupport.processName("name2", parts, false);
    Assert.assertTrue(parts[0].equals("http://www.java.com"));
    Assert.assertTrue(parts[1].equals("name2"));
    Assert.assertTrue(parts[2].equals("name2"));
}
 
Example 4
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPrefixAndUri4() {
    NamespaceSupport nssupport = new NamespaceSupport();

    nssupport.pushContext();
    nssupport.declarePrefix("dc", "http://www.purl.org/dc");

    nssupport.pushContext();
    nssupport.declarePrefix("dc1", "http://www.purl.org/dc");
    nssupport.declarePrefix("dc2", "http://www.purl.org/dc2");
    nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew");

    AssertJUnit.assertTrue(nssupport.getURI("dc").equals("http://www.purl.org/dc"));
    AssertJUnit.assertTrue(nssupport.getURI("dc1").equals("http://www.purl.org/dc"));
    AssertJUnit.assertTrue(nssupport.getURI("dc2").equals("http://www.purl.org/dc2"));
    AssertJUnit.assertTrue(nssupport.getURI("dcnew").equals("http://www.purl.org/dcnew"));

    // Negative test
    Assert.assertNull(nssupport.getURI("wrong_prefix"));
    Assert.assertNull(nssupport.getURI(""));
}
 
Example 5
Source File: AbstractSchemaValidationTube.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if (node==null || node.getNodeType()!=Node.ELEMENT_NODE) {
        return;
    }

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            //continue;
        }
    }
}
 
Example 6
Source File: StreamMessage.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * There is no way to enumerate inscope namespaces for XMLStreamReader. That means
 * namespaces declared in envelope, and body tags need to be computed using their
 * {@link TagInfoset}s.
 *
 * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
 */
private String[] getInscopeNamespaces() {
    NamespaceSupport nss = new NamespaceSupport();

    nss.pushContext();
    for(int i=0; i < envelopeTag.ns.length; i+=2) {
        nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]);
    }

    nss.pushContext();
    for(int i=0; i < bodyTag.ns.length; i+=2) {
        nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]);
    }

    List<String> inscope = new ArrayList<String>();
    for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
        String prefix = (String)en.nextElement();
        inscope.add(prefix);
        inscope.add(nss.getURI(prefix));
    }
    return inscope.toArray(new String[inscope.size()]);
}
 
Example 7
Source File: StreamMessage.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * There is no way to enumerate inscope namespaces for XMLStreamReader. That means
 * namespaces declared in envelope, and body tags need to be computed using their
 * {@link TagInfoset}s.
 *
 * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
 */
private String[] getInscopeNamespaces() {
    NamespaceSupport nss = new NamespaceSupport();

    nss.pushContext();
    for(int i=0; i < envelopeTag.ns.length; i+=2) {
        nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]);
    }

    nss.pushContext();
    for(int i=0; i < bodyTag.ns.length; i+=2) {
        nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]);
    }

    List<String> inscope = new ArrayList<String>();
    for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
        String prefix = (String)en.nextElement();
        inscope.add(prefix);
        inscope.add(nss.getURI(prefix));
    }
    return inscope.toArray(new String[inscope.size()]);
}
 
Example 8
Source File: AbstractSchemaValidationTube.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if (node==null || node.getNodeType()!=Node.ELEMENT_NODE) {
        return;
    }

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            //continue;
        }
    }
}
 
Example 9
Source File: StreamMessage.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * There is no way to enumerate inscope namespaces for XMLStreamReader. That means
 * namespaces declared in envelope, and body tags need to be computed using their
 * {@link TagInfoset}s.
 *
 * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
 */
private String[] getInscopeNamespaces() {
    NamespaceSupport nss = new NamespaceSupport();

    nss.pushContext();
    for(int i=0; i < envelopeTag.ns.length; i+=2) {
        nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]);
    }

    nss.pushContext();
    for(int i=0; i < bodyTag.ns.length; i+=2) {
        nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]);
    }

    List<String> inscope = new ArrayList<String>();
    for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
        String prefix = (String)en.nextElement();
        inscope.add(prefix);
        inscope.add(nss.getURI(prefix));
    }
    return inscope.toArray(new String[inscope.size()]);
}
 
Example 10
Source File: DOMScanner.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Recursively visit ancestors and build up {@link NamespaceSupport} oject.
 */
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if(node==null || node.getNodeType()!=Node.ELEMENT_NODE)
        return;

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            continue;
        }
    }
}
 
Example 11
Source File: DOMScanner.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Recursively visit ancestors and build up {@link NamespaceSupport} oject.
 */
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if(node==null || node.getNodeType()!=Node.ELEMENT_NODE)
        return;

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            continue;
        }
    }
}
 
Example 12
Source File: StreamMessage.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * There is no way to enumerate inscope namespaces for XMLStreamReader. That means
 * namespaces declared in envelope, and body tags need to be computed using their
 * {@link TagInfoset}s.
 *
 * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
 */
private String[] getInscopeNamespaces() {
    NamespaceSupport nss = new NamespaceSupport();

    nss.pushContext();
    for(int i=0; i < envelopeTag.ns.length; i+=2) {
        nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]);
    }

    nss.pushContext();
    for(int i=0; i < bodyTag.ns.length; i+=2) {
        nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]);
    }

    List<String> inscope = new ArrayList<String>();
    for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
        String prefix = (String)en.nextElement();
        inscope.add(prefix);
        inscope.add(nss.getURI(prefix));
    }
    return inscope.toArray(new String[inscope.size()]);
}
 
Example 13
Source File: AbstractSchemaValidationTube.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
    if (node==null || node.getNodeType()!=Node.ELEMENT_NODE) {
        return;
    }

    buildNamespaceSupport( nss, node.getParentNode() );

    nss.pushContext();
    NamedNodeMap atts = node.getAttributes();
    for( int i=0; i<atts.getLength(); i++ ) {
        Attr a = (Attr)atts.item(i);
        if( "xmlns".equals(a.getPrefix()) ) {
            nss.declarePrefix( a.getLocalName(), a.getValue() );
            continue;
        }
        if( "xmlns".equals(a.getName()) ) {
            nss.declarePrefix( "", a.getValue() );
            //continue;
        }
    }
}
 
Example 14
Source File: StreamMessage.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * There is no way to enumerate inscope namespaces for XMLStreamReader. That means
 * namespaces declared in envelope, and body tags need to be computed using their
 * {@link TagInfoset}s.
 *
 * @return array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
 */
private String[] getInscopeNamespaces() {
    NamespaceSupport nss = new NamespaceSupport();

    nss.pushContext();
    for(int i=0; i < envelopeTag.ns.length; i+=2) {
        nss.declarePrefix(envelopeTag.ns[i], envelopeTag.ns[i+1]);
    }

    nss.pushContext();
    for(int i=0; i < bodyTag.ns.length; i+=2) {
        nss.declarePrefix(bodyTag.ns[i], bodyTag.ns[i+1]);
    }

    List<String> inscope = new ArrayList<String>();
    for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
        String prefix = (String)en.nextElement();
        inscope.add(prefix);
        inscope.add(nss.getURI(prefix));
    }
    return inscope.toArray(new String[inscope.size()]);
}
 
Example 15
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test for NamespaceSupport.popContext().
 */
@Test
public void testcase04() {
    NamespaceSupport support = new NamespaceSupport();

    support.pushContext();
    support.declarePrefix(EMPTY_PREFIX, W3_URI);
    support.declarePrefix(DC_PREFIX, PURL_URI);

    assertEquals(support.getURI(EMPTY_PREFIX), W3_URI);
    assertEquals(support.getURI(DC_PREFIX), PURL_URI);
    support.popContext();
    assertNull(support.getURI(EMPTY_PREFIX));
    assertNull(support.getURI(DC_PREFIX));
}
 
Example 16
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test for NamespaceSupport.getDeclaredPrefixes() and support.processName().
 */
@Test
public void testcase03() {
    String[] parts = new String[3];
    NamespaceSupport support = new NamespaceSupport();
    support.pushContext();
    support.declarePrefix(EMPTY_PREFIX, W3_URI);
    parts = support.processName("a", parts, false);
    support.popContext();
    assertEquals(parts, new String[]{W3_URI, "a", "a"});
}
 
Example 17
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPrefixAndUri3() {
    boolean hasdc = false;
    boolean hasdc1 = false;
    boolean hasdc2 = false;
    boolean hasdcnew = false;
    NamespaceSupport nssupport = new NamespaceSupport();

    nssupport.pushContext();
    nssupport.declarePrefix("dc", "http://www.purl.org/dc");

    nssupport.pushContext();
    nssupport.declarePrefix("dc1", "http://www.purl.org/dc");
    nssupport.declarePrefix("dc2", "http://www.purl.org/dc2");
    nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew");

    Enumeration enu1 = nssupport.getPrefixes("http://www.purl.org/dc");
    while (enu1.hasMoreElements()) {
        String str = (String) enu1.nextElement();
        if (str.equals("dc")) {
            hasdc = true;
        } else if (str.equals("dc1")) {
            hasdc1 = true;
        } else if (str.equals("dc2")) {
            hasdc2 = true;
        } else if (str.equals("dcnew")) {
            hasdcnew = true;
        }
    }
    AssertJUnit.assertTrue(hasdc1 && hasdc);
    AssertJUnit.assertFalse(hasdc2);
    AssertJUnit.assertFalse(hasdcnew);
}
 
Example 18
Source File: NamespaceSupportTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() {
    expected = new ArrayList<String>();
    expected.add("ak");
    expected.add("bk");

    ns = new NamespaceSupport();
    ns.pushContext();

    ns.declarePrefix("ak", marketUri);
    ns.declarePrefix("bk", marketUri);
    ns.declarePrefix("", defaultUri);
}
 
Example 19
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test for NamespaceSupport.getDeclaredPrefixes() and support.processName().
 */
@Test
public void testcase02() {
    String[] parts = new String[3];
    NamespaceSupport support = new NamespaceSupport();

    support.pushContext();
    support.declarePrefix(DC_PREFIX, PURL_URI);
    parts = support.processName("dc:title", parts, false);
    support.popContext();
    assertEquals(parts, new String[]{PURL_URI, "title", "dc:title"});
}
 
Example 20
Source File: NSSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testPrefixAndUri2() {
    boolean hasdc = false;
    boolean hasdc1 = false;
    boolean hasdc2 = false;
    boolean hasdcnew = false;
    NamespaceSupport nssupport = new NamespaceSupport();

    nssupport.pushContext();
    nssupport.declarePrefix("dc", "http://www.purl.org/dc");

    nssupport.pushContext();
    nssupport.declarePrefix("dc1", "http://www.purl.org/dc");
    nssupport.declarePrefix("dc2", "http://www.purl.org/dc2");
    nssupport.declarePrefix("dcnew", "http://www.purl.org/dcnew");

    Enumeration enu1 = nssupport.getPrefixes();
    while (enu1.hasMoreElements()) {
        String str = (String) enu1.nextElement();
        if (str.equals("dc")) {
            hasdc = true;
        } else if (str.equals("dc1")) {
            hasdc1 = true;
        } else if (str.equals("dc2")) {
            hasdc2 = true;
        } else if (str.equals("dcnew")) {
            hasdcnew = true;
        }
    }
    AssertJUnit.assertTrue(hasdcnew && hasdc1 && hasdc2 && hasdc);
}