Search in sources :

Example 21 with QName

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

the class DocumentImpl method createAttributeNS.

/**
 * The method <code>createAttributeNS</code>
 *
 * @param namespaceURI  a <code>String</code> value
 * @param qualifiedName a <code>String</code> value
 * @return an <code>Attr</code> value
 * @throws DOMException if an error occurs
 */
@Override
public Attr createAttributeNS(final String namespaceURI, final String qualifiedName) throws DOMException {
    final QName qname;
    try {
        qname = QName.parse(namespaceURI, qualifiedName);
    } catch (final IllegalQNameException e) {
        final short errCode;
        if (e.getValidity() == ILLEGAL_FORMAT.val || (e.getValidity() & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
            errCode = DOMException.NAMESPACE_ERR;
        } else {
            errCode = DOMException.INVALID_CHARACTER_ERR;
        }
        throw new DOMException(errCode, "qualified name is invalid");
    }
    // check the QName is valid for use
    final byte validity = qname.isValid(false);
    if ((validity & QName.Validity.INVALID_LOCAL_PART.val) == QName.Validity.INVALID_LOCAL_PART.val) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "qualified name is invalid");
    } else if ((validity & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
        throw new DOMException(DOMException.NAMESPACE_ERR, "qualified name is invalid");
    }
    final AttrImpl attr = new AttrImpl(qname, getBrokerPool().getSymbols());
    attr.setOwnerDocument(this);
    return attr;
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 22 with QName

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

the class DocumentImpl method createElement.

/**
 * The method <code>createElement</code>
 *
 * @param tagName a <code>String</code> value
 * @return an <code>Element</code> value
 * @throws DOMException if an error occurs
 */
@Override
public Element createElement(final String tagName) throws DOMException {
    final QName qname;
    try {
        qname = new QName(tagName);
    } catch (final IllegalQNameException e) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "name is invalid");
    }
    // check the QName is valid for use
    if (qname.isValid(false) != QName.Validity.VALID.val) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "name is invalid");
    }
    final ElementImpl element = new ElementImpl(qname, getBrokerPool().getSymbols());
    element.setOwnerDocument(this);
    return element;
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 23 with QName

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

the class DocumentImpl method createElementNS.

/**
 * The method <code>createElementNS</code>
 *
 * @param namespaceURI  a <code>String</code> value
 * @param qualifiedName a <code>String</code> value
 * @return an <code>Element</code> value
 * @throws DOMException if an error occurs
 */
@Override
public Element createElementNS(final String namespaceURI, final String qualifiedName) throws DOMException {
    final QName qname;
    try {
        qname = QName.parse(namespaceURI, qualifiedName);
    } catch (final IllegalQNameException e) {
        final short errCode;
        if (e.getValidity() == ILLEGAL_FORMAT.val || (e.getValidity() & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
            errCode = DOMException.NAMESPACE_ERR;
        } else {
            errCode = DOMException.INVALID_CHARACTER_ERR;
        }
        throw new DOMException(errCode, "qualified name is invalid");
    }
    // check the QName is valid for use
    final byte validity = qname.isValid(false);
    if ((validity & QName.Validity.INVALID_LOCAL_PART.val) == QName.Validity.INVALID_LOCAL_PART.val) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "qualified name is invalid");
    } else if ((validity & QName.Validity.INVALID_NAMESPACE.val) == QName.Validity.INVALID_NAMESPACE.val) {
        throw new DOMException(DOMException.NAMESPACE_ERR, "qualified name is invalid");
    }
    final ElementImpl element = new ElementImpl(qname, getBrokerPool().getSymbols());
    element.setOwnerDocument(this);
    return element;
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 24 with QName

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

the class SymbolTable method getQName.

/**
 * Retrieve a shared QName instance from the temporary pool.
 *
 * TODO: make the namePool thread-local to avoid synchronization.
 * @param type qname type
 * @param namespaceURI qname namespace uri
 * @param localName qname localname
 * @param prefix qname prefix
 * @return qname from pool
 */
public synchronized QName getQName(final short type, final String namespaceURI, final String localName, final String prefix) {
    final byte itype = type == Node.ATTRIBUTE_NODE ? ElementValue.ATTRIBUTE : ElementValue.ELEMENT;
    QName qn = namePool.get(itype, namespaceURI, localName, prefix);
    if (qn == null) {
        qn = namePool.add(itype, namespaceURI, localName, prefix);
    }
    return qn;
}
Also used : QName(org.exist.dom.QName)

Example 25 with QName

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

the class AdaptiveWriter method write.

/**
 * Serialize the given sequence using adaptive output mode.
 *
 * @param sequence input sequence
 * @param itemSep separator string to output between items in the sequence
 * @param enclose if set to true: enclose sequences of items into parentheses
 * @throws SAXException if an error occurs during serialization
 * @throws XPathException if an XPath error occurs
 * @throws TransformerException if an error occurs whilst transforming
 */
public void write(final Sequence sequence, final String itemSep, final boolean enclose) throws SAXException, XPathException, TransformerException {
    try {
        if (enclose && sequence.getItemCount() != 1) {
            writer.write('(');
        }
        for (final SequenceIterator si = sequence.iterate(); si.hasNext(); ) {
            final Item item = si.nextItem();
            switch(item.getType()) {
                case Type.DOCUMENT:
                case Type.ELEMENT:
                case Type.TEXT:
                case Type.COMMENT:
                case Type.CDATA_SECTION:
                case Type.PROCESSING_INSTRUCTION:
                    writeXML(item);
                    break;
                case Type.ATTRIBUTE:
                    final Attr node = (Attr) ((NodeValue) item).getNode();
                    writeText(node.getName() + "=\"" + node.getValue() + '"');
                    break;
                case Type.NAMESPACE:
                    final NamespaceNode ns = (NamespaceNode) item;
                    writeText(ns.getName() + "=\"" + ns.getValue() + '"');
                    break;
                case Type.STRING:
                case Type.NORMALIZED_STRING:
                case Type.TOKEN:
                case Type.LANGUAGE:
                case Type.NMTOKEN:
                case Type.NAME:
                case Type.NCNAME:
                case Type.ID:
                case Type.IDREF:
                case Type.ENTITY:
                case Type.UNTYPED_ATOMIC:
                case Type.ANY_URI:
                    final String v = item.getStringValue();
                    writeText('"' + escapeQuotes(v) + '"');
                    break;
                case Type.INTEGER:
                case Type.DECIMAL:
                case Type.INT:
                case Type.LONG:
                case Type.SHORT:
                case Type.BYTE:
                case Type.UNSIGNED_LONG:
                case Type.UNSIGNED_INT:
                case Type.UNSIGNED_SHORT:
                case Type.UNSIGNED_BYTE:
                case Type.NON_NEGATIVE_INTEGER:
                case Type.NON_POSITIVE_INTEGER:
                case Type.POSITIVE_INTEGER:
                case Type.NEGATIVE_INTEGER:
                    writeText(item.getStringValue());
                    break;
                case Type.DOUBLE:
                    writeDouble((DoubleValue) item);
                    break;
                case Type.BOOLEAN:
                    writeText(item.getStringValue() + "()");
                    break;
                case Type.QNAME:
                case Type.NOTATION:
                    final QName qn = ((QNameValue) item).getQName();
                    writeText("Q{" + qn.getNamespaceURI() + '}' + qn.getLocalPart());
                    break;
                case Type.ARRAY:
                    writeArray((ArrayType) item);
                    break;
                case Type.MAP:
                    writeMap((AbstractMapType) item);
                    break;
                case Type.FUNCTION_REFERENCE:
                    writeFunctionItem((FunctionReference) item);
                    break;
                default:
                    writeAtomic(item.atomize());
                    break;
            }
            if (si.hasNext()) {
                try {
                    writer.write(itemSep);
                } catch (IOException e) {
                    throw new SAXException(e.getMessage());
                }
            }
        }
        if (enclose && sequence.getItemCount() != 1) {
            writer.write(')');
        }
    } catch (IOException e) {
        throw new SAXException(e.getMessage(), e);
    }
}
Also used : QName(org.exist.dom.QName) NamespaceNode(org.exist.dom.memtree.NamespaceNode) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException)

Aggregations

QName (org.exist.dom.QName)260 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 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 PermissionDeniedException (org.exist.security.PermissionDeniedException)10