Search in sources :

Example 1 with NamePool

use of org.exist.util.hashtable.NamePool in project exist by eXist-db.

the class DocumentBuilderReceiverTest method use_namespace_prefix_from_context.

@Test
public void use_namespace_prefix_from_context() throws SAXException {
    // if a namespace is mapped in the current context, use its prefix and overwrite
    // local prefix
    final String title = "title";
    final String titleQName = "atom:title";
    XQueryContext mockContext = EasyMock.createMock(XQueryContext.class);
    expect(mockContext.getDatabase()).andReturn(null);
    expect(mockContext.getSharedNamePool()).andReturn(new NamePool());
    // namespace mapping in context
    expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn("a");
    replay(mockContext);
    MemTreeBuilder builder = new MemTreeBuilder(mockContext);
    DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
    builder.startDocument();
    receiver.startElement(ATOM_NS, title, titleQName, null);
    receiver.endElement(ATOM_NS, title, titleQName);
    builder.endDocument();
    verify(mockContext);
    Document doc = builder.getDocument();
    Node entryNode = doc.getFirstChild();
    assertEquals("Explicit namespace prefix should be preserved", "a:title", entryNode.getNodeName());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) NamePool(org.exist.util.hashtable.NamePool) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with NamePool

use of org.exist.util.hashtable.NamePool in project exist by eXist-db.

the class DocumentBuilderReceiverTest method when_prefix_is_known_in_context_dont_use_if_namespace_equals_default_namespace.

// private static String ATOM_PREFIX = "atom";
@Test
public void when_prefix_is_known_in_context_dont_use_if_namespace_equals_default_namespace() throws SAXException {
    final String entry_name = "entry";
    final String id_name = "id";
    XQueryContext mockContext = EasyMock.createMock(XQueryContext.class);
    expect(mockContext.getDatabase()).andReturn(null);
    expect(mockContext.getSharedNamePool()).andReturn(new NamePool());
    // expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn(ATOM_PREFIX).times(2);
    replay(mockContext);
    MemTreeBuilder builder = new MemTreeBuilder(mockContext);
    DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
    builder.startDocument();
    receiver.startPrefixMapping("", ATOM_NS);
    receiver.startElement(ATOM_NS, entry_name, entry_name, null);
    receiver.startElement(ATOM_NS, id_name, id_name, null);
    receiver.endElement(ATOM_NS, id_name, id_name);
    receiver.endElement(ATOM_NS, entry_name, entry_name);
    builder.endDocument();
    verify(mockContext);
    Document doc = builder.getDocument();
    Node entryNode = doc.getFirstChild();
    assertEquals(entry_name, entryNode.getNodeName());
    Node idNode = entryNode.getFirstChild();
    assertEquals(id_name, idNode.getNodeName());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) NamePool(org.exist.util.hashtable.NamePool) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 3 with NamePool

use of org.exist.util.hashtable.NamePool in project exist by eXist-db.

the class DocumentBuilderReceiverTest method use_given_namespace_prefix.

@Test
public void use_given_namespace_prefix() throws SAXException {
    // if an explicit namespace prefix is present, it should be used
    // unless a different mapping was defined in context
    final String title = "title";
    final String titleQName = "atom:title";
    XQueryContext mockContext = EasyMock.createMock(XQueryContext.class);
    expect(mockContext.getDatabase()).andReturn(null);
    expect(mockContext.getSharedNamePool()).andReturn(new NamePool());
    // no namespace mapping in context
    expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn(null);
    replay(mockContext);
    MemTreeBuilder builder = new MemTreeBuilder(mockContext);
    DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
    builder.startDocument();
    receiver.startElement(ATOM_NS, title, titleQName, null);
    receiver.endElement(ATOM_NS, title, titleQName);
    builder.endDocument();
    verify(mockContext);
    Document doc = builder.getDocument();
    Node entryNode = doc.getFirstChild();
    assertEquals("Explicit namespace prefix should be preserved", titleQName, entryNode.getNodeName());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) NamePool(org.exist.util.hashtable.NamePool) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

NamePool (org.exist.util.hashtable.NamePool)3 XQueryContext (org.exist.xquery.XQueryContext)3 Test (org.junit.Test)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3