Search in sources :

Example 56 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class FunResolveQName method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    final Sequence qnameSeq = args[0];
    if (qnameSeq.isEmpty()) {
        return EmptySequence.EMPTY_SEQUENCE;
    } else {
        context.pushInScopeNamespaces();
        final String qnameString = args[0].getStringValue();
        if (QName.isQName(qnameString) == VALID.val) {
            try {
                String prefix = QName.extractPrefix(qnameString);
                if (prefix == null) {
                    prefix = "";
                }
                String uri = null;
                final NodeValue node = (NodeValue) args[1].itemAt(0);
                if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    NodeProxy proxy = (NodeProxy) node;
                    final NodeSet ancestors = proxy.getAncestors(contextId, true);
                    for (NodeProxy ancestor : ancestors) {
                        proxy = ancestor;
                        final ElementImpl e = (ElementImpl) proxy.getNode();
                        uri = findNamespaceURI(e, prefix);
                        if (uri != null) {
                            break;
                        }
                    }
                } else {
                    NodeImpl next = (NodeImpl) node;
                    do {
                        uri = findNamespaceURI((org.exist.dom.memtree.ElementImpl) next, prefix);
                        if (uri != null) {
                            break;
                        } else {
                            next = (NodeImpl) next.getParentNode();
                        }
                    } while (next != null && next.getNodeType() == Node.ELEMENT_NODE);
                }
                if (uri == null && prefix != null && !prefix.isEmpty()) {
                    throw new XPathException(this, ErrorCodes.FONS0004, "No namespace found for prefix. No binding for prefix '" + prefix + "' was found.", args[0]);
                }
                final String localPart = QName.extractLocalName(qnameString);
                final QName qn = new QName(localPart, uri, prefix);
                final QNameValue result = new QNameValue(context, qn);
                if (context.getProfiler().isEnabled()) {
                    context.getProfiler().end(this, "", result);
                }
                context.popInScopeNamespaces();
                return result;
            } catch (final QName.IllegalQNameException e) {
                throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
            }
        } else {
            throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) EmptySequence(org.exist.xquery.value.EmptySequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) ElementImpl(org.exist.dom.persistent.ElementImpl)

Example 57 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class FunQName method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    // TODO : currently useless (but for empty sequences) since the type is forced :-(
    if (!args[0].isEmpty() && args[0].getItemType() != Type.STRING) {
        throw new XPathException(this, ErrorCodes.XPTY0004, "Namespace URI is of type '" + Type.getTypeName(args[0].getItemType()) + "', 'xs:string' expected", args[0]);
    }
    String namespace;
    if (args[0].isEmpty()) {
        namespace = "";
    } else {
        namespace = args[0].getStringValue();
    }
    final String param = args[1].getStringValue();
    String prefix = null;
    String localName = null;
    try {
        prefix = QName.extractPrefix(param);
        localName = QName.extractLocalName(param);
    } catch (final QName.IllegalQNameException e) {
        final ValueSequence argsSeq = new ValueSequence(args[0]);
        argsSeq.addAll(args[1]);
        throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical form of either prefix or local name.", argsSeq);
    }
    if ((prefix != null && !prefix.isEmpty()) && (namespace == null || namespace.isEmpty())) {
        final ValueSequence argsSeq = new ValueSequence(args[0]);
        argsSeq.addAll(args[1]);
        throw new XPathException(this, ErrorCodes.FOCA0002, "Non-empty namespace prefix with empty namespace URI", argsSeq);
    }
    if (namespace != null) {
        if (namespace.equalsIgnoreCase(Namespaces.XMLNS_NS)) {
            if (prefix == null)
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XMLNS_NS + "' can't be use with no prefix");
            else if (!prefix.equalsIgnoreCase("xmlns"))
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XMLNS_NS + "' can't be use with prefix '" + prefix + "'");
        }
        if (namespace.equalsIgnoreCase(Namespaces.XML_NS)) {
            if (prefix == null)
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XML_NS + "' can't be use with no prefix");
            else if (!prefix.equalsIgnoreCase("xml"))
                throw new XPathException(this, ErrorCodes.XQDY0044, "'" + Namespaces.XML_NS + "' can't be use with prefix '" + prefix + "'");
        }
    }
    if (prefix != null) {
        if (prefix.equalsIgnoreCase("xml") && !namespace.equalsIgnoreCase(Namespaces.XML_NS)) {
            throw new XPathException(this, ErrorCodes.XQDY0044, "prefix 'xml' can be used only with '" + Namespaces.XML_NS + "'");
        }
    }
    final QName qname = new QName(localName, namespace, prefix);
    if (prefix != null && namespace != null) {
        if (context.getURIForPrefix(prefix) == null) {
            // TOCHECK : context.declareInScopeNamespace(prefix, uri) ?
            context.declareNamespace(prefix, namespace);
        }
    // context.declareInScopeNamespace(prefix, namespace);
    }
    if (!XMLNames.isName(qname.getLocalPart())) {
        throw new XPathException(this, ErrorCodes.FOCA0002, "'" + qname.getLocalPart() + "' is not a valid local name.");
    }
    final Sequence result = new QNameValue(context, qname);
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 58 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class HTML5WriterTest method testAttributeQNameWithBooleanValue.

@Test
public void testAttributeQNameWithBooleanValue() throws Exception {
    final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n<input checked>";
    final QName elQName = new QName("input");
    final QName attrQName = new QName("checked");
    writer.startElement(elQName);
    writer.attribute(attrQName, attrQName.getLocalPart());
    writer.closeStartTag(true);
    final String actual = targetWriter.toString();
    assertEquals(expected, actual);
}
Also used : QName(org.exist.dom.QName) Test(org.junit.Test)

Example 59 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class HTML5WriterTest method testAttributeQNameWithNonBooleanValue.

@Test
public void testAttributeQNameWithNonBooleanValue() throws Exception {
    final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html>\n<input name=\"name\">";
    final QName elQName = new QName("input");
    final QName attrQName = new QName("name");
    writer.startElement(elQName);
    writer.attribute(attrQName, attrQName.getLocalPart());
    writer.closeStartTag(true);
    final String actual = targetWriter.toString();
    assertEquals(expected, actual);
}
Also used : QName(org.exist.dom.QName) Test(org.junit.Test)

Example 60 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class NamePoolTest method getSharedNameDifferentPrefix.

@Test
public void getSharedNameDifferentPrefix() throws Exception {
    NamePool pool = new NamePool();
    QName q1 = new QName("n1", "http://exist-db.org", "x");
    QName q2 = new QName("n2", "http://exist-db.org", "x");
    pool.getSharedName(q1);
    QName qr = pool.getSharedName(q2);
    assertNotSame(q1, qr);
}
Also used : QName(org.exist.dom.QName) Test(org.junit.Test)

Aggregations

QName (org.exist.dom.QName)271 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)18 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10