Search in sources :

Example 11 with IllegalQNameException

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

the class DocumentImpl method createElementNS.

@Override
public Element createElementNS(final String namespaceURI, final String qualifiedName) throws DOMException {
    final QName qname;
    try {
        if (getContext() != null) {
            qname = QName.parse(getContext(), qualifiedName, namespaceURI);
        } else {
            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 int nodeNum = addNode(Node.ELEMENT_NODE, (short) 1, qname);
    return new ElementImpl(this, nodeNum);
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 12 with IllegalQNameException

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

the class DocumentImpl method getElementsByTagNameNS.

@Override
public NodeList getElementsByTagNameNS(final String namespaceURI, final String localName) {
    final boolean wildcardNS = namespaceURI != null && namespaceURI.equals(QName.WILDCARD);
    final boolean wildcardLocalPart = localName != null && localName.equals(QName.WILDCARD);
    if (wildcardNS && wildcardLocalPart) {
        return getElementsByTagName(QName.WildcardQName.getInstance());
    } else if (wildcardNS) {
        return getElementsByTagName(new QName.WildcardNamespaceURIQName(localName));
    } else if (wildcardLocalPart) {
        return getElementsByTagName(new QName.WildcardLocalPartQName(namespaceURI));
    } else {
        final QName qname;
        if (document.getContext() != null) {
            try {
                qname = QName.parse(document.context, localName, namespaceURI);
            } catch (final IllegalQNameException e) {
                throw new DOMException(DOMException.INVALID_CHARACTER_ERR, e.getMessage());
            }
        } else {
            qname = new QName(localName, namespaceURI);
        }
        return getElementsByTagName(qname);
    }
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 13 with IllegalQNameException

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

the class DocumentImpl method createElement.

@Override
public Element createElement(final String tagName) throws DOMException {
    final QName qname;
    try {
        if (getContext() != null) {
            qname = QName.parse(getContext(), tagName);
        } else {
            qname = new QName(tagName);
        }
    } catch (final IllegalQNameException e) {
        throw new DOMException(DOMException.INVALID_CHARACTER_ERR, e.getMessage());
    }
    // 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 int nodeNum = addNode(Node.ELEMENT_NODE, (short) 1, qname);
    return new ElementImpl(this, nodeNum);
}
Also used : QName(org.exist.dom.QName) IllegalQNameException(org.exist.dom.QName.IllegalQNameException)

Example 14 with IllegalQNameException

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

the class DocumentImpl method createAttribute.

/**
 * The method <code>createAttribute</code>
 *
 * @param name a <code>String</code> value
 * @return an <code>Attr</code> value
 * @throws DOMException if an error occurs
 */
@Override
public Attr createAttribute(final String name) throws DOMException {
    final QName qname;
    try {
        qname = new QName(name);
    } 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 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 15 with IllegalQNameException

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

the class DOMStreamer method startNode.

protected void startNode(final Node node) throws SAXException {
    String cdata;
    switch(node.getNodeType()) {
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            namespaceDecls.clear();
            nsSupport.pushContext();
            String uri = node.getNamespaceURI();
            String prefix = node.getPrefix();
            if (uri == null) {
                uri = XMLConstants.XML_NS_URI;
            }
            if (prefix == null) {
                prefix = XMLConstants.DEFAULT_NS_PREFIX;
            }
            if (nsSupport.getURI(prefix) == null) {
                namespaceDecls.put(prefix, uri);
                nsSupport.declarePrefix(prefix, uri);
            }
            // check attributes for required namespace declarations
            final NamedNodeMap attrs = node.getAttributes();
            Attr nextAttr;
            String attrName;
            for (int i = 0; i < attrs.getLength(); i++) {
                nextAttr = (Attr) attrs.item(i);
                attrName = nextAttr.getName();
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(attrName)) {
                    if (nsSupport.getURI(XMLConstants.NULL_NS_URI) == null) {
                        uri = nextAttr.getValue();
                        namespaceDecls.put(XMLConstants.DEFAULT_NS_PREFIX, uri);
                        nsSupport.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX, uri);
                    }
                } else if (attrName.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
                    prefix = attrName.substring(6);
                    if (nsSupport.getURI(prefix) == null) {
                        uri = nextAttr.getValue();
                        namespaceDecls.put(prefix, uri);
                        nsSupport.declarePrefix(prefix, uri);
                    }
                } else if (attrName.indexOf(':') > 0) {
                    prefix = nextAttr.getPrefix();
                    if (prefix == null) {
                        prefix = XMLConstants.DEFAULT_NS_PREFIX;
                    }
                    uri = nextAttr.getNamespaceURI();
                    if (nsSupport.getURI(prefix) == null) {
                        namespaceDecls.put(prefix, uri);
                        nsSupport.declarePrefix(prefix, uri);
                    }
                }
            }
            final ElementInfo info = new ElementInfo(node);
            String[] declaredPrefixes = null;
            if (!namespaceDecls.isEmpty()) {
                declaredPrefixes = new String[namespaceDecls.size()];
            }
            // output all namespace declarations
            Map.Entry<String, String> nsEntry;
            int j = 0;
            for (final Iterator<Map.Entry<String, String>> i = namespaceDecls.entrySet().iterator(); i.hasNext(); j++) {
                nsEntry = i.next();
                declaredPrefixes[j] = nsEntry.getKey();
                contentHandler.startPrefixMapping(declaredPrefixes[j], nsEntry.getValue());
            }
            info.prefixes = declaredPrefixes;
            stack.push(info);
            // output attributes
            final AttributesImpl saxAttrs = new AttributesImpl();
            String attrNS;
            String attrLocalName;
            for (int i = 0; i < attrs.getLength(); i++) {
                nextAttr = (Attr) attrs.item(i);
                attrNS = nextAttr.getNamespaceURI();
                if (attrNS == null) {
                    attrNS = XMLConstants.NULL_NS_URI;
                }
                attrLocalName = nextAttr.getLocalName();
                if (attrLocalName == null) {
                    try {
                        attrLocalName = QName.extractLocalName(nextAttr.getNodeName());
                    } catch (final IllegalQNameException e) {
                        throw new SAXException(e);
                    }
                }
                saxAttrs.addAttribute(attrNS, attrLocalName, nextAttr.getNodeName(), "CDATA", nextAttr.getValue());
            }
            String localName = node.getLocalName();
            if (localName == null) {
                try {
                    localName = QName.extractLocalName(node.getNodeName());
                } catch (final IllegalQNameException e) {
                    throw new SAXException(e);
                }
            }
            String namespaceURI = node.getNamespaceURI();
            if (namespaceURI == null) {
                namespaceURI = XMLConstants.NULL_NS_URI;
            }
            contentHandler.startElement(namespaceURI, localName, node.getNodeName(), saxAttrs);
            break;
        case Node.TEXT_NODE:
            cdata = ((CharacterData) node).getData();
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
            break;
        case Node.CDATA_SECTION_NODE:
            cdata = ((CharacterData) node).getData();
            if (lexicalHandler != null) {
                lexicalHandler.startCDATA();
            }
            contentHandler.characters(cdata.toCharArray(), 0, cdata.length());
            if (lexicalHandler != null) {
                lexicalHandler.endCDATA();
            }
            break;
        case Node.ATTRIBUTE_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            contentHandler.processingInstruction(((ProcessingInstruction) node).getTarget(), ((ProcessingInstruction) node).getData());
            break;
        case Node.COMMENT_NODE:
            if (lexicalHandler != null) {
                cdata = ((Comment) node).getData();
                lexicalHandler.comment(cdata.toCharArray(), 0, cdata.length());
            }
            break;
        default:
            // TODO : what kind of default here ? -pb
            LOG.error("Unknown node type: {}", node.getNodeType());
            break;
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attr(org.w3c.dom.Attr) IllegalQNameException(org.exist.dom.QName.IllegalQNameException) SAXException(org.xml.sax.SAXException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Aggregations

IllegalQNameException (org.exist.dom.QName.IllegalQNameException)15 QName (org.exist.dom.QName)13 SAXException (org.xml.sax.SAXException)2 Attr (org.w3c.dom.Attr)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1