Example usage for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

List of usage examples for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI.

Prototype

String XMLNS_ATTRIBUTE_NS_URI

To view the source code for javax.xml XMLConstants XMLNS_ATTRIBUTE_NS_URI.

Click Source Link

Document

The official XML attribute used for specifying XML Namespace declarations, #XMLNS_ATTRIBUTE XMLConstants.XMLNS_ATTRIBUTE , Namespace name URI.

Usage

From source file:nz.co.testamation.common.util.PrefixMapNamespaceContext.java

public String getNamespaceURI(String prefix) {
    switch (prefix) {
    case XMLConstants.XML_NS_PREFIX:
        return XMLConstants.XML_NS_URI;
    case XMLConstants.XMLNS_ATTRIBUTE:
        return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
    default:/*from   www.  jav  a  2 s  . c om*/
        return StringUtils.defaultString(prefixNamespaceMap.get(prefix), XMLConstants.NULL_NS_URI);
    }
}

From source file:org.apache.sling.its.servlets.ItsImportServlet.java

/**
 * Store the element and its attribute. The child node of global rules are
 * specially handled so they will not be traversed.
 *
 * @param path//from w w w.jav  a  2 s  .c  om
 *         the target path
 * @param resourceType
 *         the resourceType
 * @param doc
 *         the document
 * @param file
 *        the file.
 * @param isExternalDoc
 *         true if this is for storing global rules for external documents
 */
private void store(String path, final String resourceType, final Document doc, final File file,
        final boolean isExternalDoc) {
    final ITraversal itsEng = applyITSRules(doc, file, null, false);
    itsEng.startTraversal();
    Node node;
    while ((node = itsEng.nextNode()) != null) {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            final Element element = (Element) node;
            // Use !backTracking() to get to the elements only once
            // and to include the empty elements (for attributes).
            if (itsEng.backTracking()) {
                if (!SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) {
                    path = backTrack(path);
                }
            } else {
                if (element.isSameNode(doc.getDocumentElement()) && !isExternalDoc) {
                    path += "/" + element.getNodeName();
                    output(path, null, null);
                    setAttributes(element, path);
                } else if (SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())) {
                    storeGlobalRule(element, resourceType, itsEng);
                } else if (!isExternalDoc
                        && !SlingItsConstants.getGlobalRules().containsKey(element.getLocalName())
                        && !(element.getParentNode().getLocalName().equals(SlingItsConstants.ITS_RULES)
                                && element.getParentNode().getPrefix() != null)) {
                    if (element.getLocalName().equals(SlingItsConstants.ITS_RULES)
                            && element.getPrefix() != null) {
                        this.hasGlobalRules = true;
                    }
                    if (element.getPrefix() != null) {
                        path += String.format("/%s(%d)", element.getLocalName(),
                                getCounter(path + "/" + element.getLocalName()));
                        element.setAttribute(SlingItsConstants.NODE_PREFIX, element.getPrefix());
                    } else if (element.getNodeName().equals("link")
                            && StringUtils.endsWith(element.getAttribute("rel"), "-rules")) {
                        path += String.format("/%s(%d)", SlingItsConstants.ITS_RULES,
                                getCounter(path + "/" + SlingItsConstants.ITS_RULES));
                        final String prefix = StringUtils.substringBefore(element.getAttribute("rel"),
                                "-rules");
                        element.setAttribute(SlingItsConstants.NODE_PREFIX, prefix);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                SlingItsConstants.XMLNS + prefix, Namespaces.ITS_NS_URI);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:h",
                                Namespaces.HTML_NS_URI);
                        element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:jcr",
                                NamespaceRegistry.NAMESPACE_JCR);
                        this.hasGlobalRules = true;
                    } else {
                        path += String.format("/%s(%d)", element.getNodeName(),
                                getCounter(path + "/" + element.getNodeName()));
                    }
                    output(path, null, null);
                    setAttributes(element, path);
                    if (!element.hasChildNodes()) // Empty elements:
                    {
                        path = backTrack(path);
                    }
                }
            }
            break;
        case Node.TEXT_NODE:
            if (StringUtils.isNotBlank(node.getNodeValue()) && !isExternalDoc) {
                path += String.format("/%s(%d)", SlingItsConstants.TEXT_CONTENT_NODE,
                        getCounter(path + "/" + SlingItsConstants.TEXT_CONTENT_NODE));
                output(path, null, node.getNodeValue());
                path = backTrack(path);
            }
            break;
        default:
            break;
        }
    }
}

From source file:org.apache.sling.its.servlets.ItsServlet.java

/**
 * Add its, sling-its, jcr and sling namespaces to the root element of the
 * xml document./*from   ww w .  java 2  s .  com*/
 *
 * @param rootElement
 *           the root element of the Document.
 */
private void addNamespaces(final Element rootElement) {
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
            SlingItsConstants.XMLNS + Namespaces.ITS_NS_PREFIX, Namespaces.ITS_NS_URI);
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, SlingItsConstants.XMLNS + "sling-its",
            "http://www.w3.org/2013/7/sling-its");
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
            SlingItsConstants.XMLNS + NamespaceRegistry.PREFIX_JCR, NamespaceRegistry.NAMESPACE_JCR);
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
            SlingItsConstants.XMLNS + SlingConstants.NAMESPACE_PREFIX, "http://sling.apache.org/jcr/sling/1.0");
}

From source file:org.ballerinalang.model.values.BXMLItem.java

/**
 * {@inheritDoc}/*from   ww w. j a  v  a 2 s. c o  m*/
 */
@Override
public BMap<?, ?> getAttributesMap() {
    BXmlAttrMap attrMap = new BXmlAttrMap(this);

    if (nodeType != XMLNodeType.ELEMENT) {
        return attrMap;
    }

    OMNamespace defaultNs = ((OMElement) omNode).getDefaultNamespace();
    String namespaceOfPrefix = '{'
            + (defaultNs == null ? XMLConstants.XMLNS_ATTRIBUTE_NS_URI : defaultNs.getNamespaceURI()) + '}';

    Iterator<OMNamespace> namespaceIterator = ((OMElement) omNode).getAllDeclaredNamespaces();
    while (namespaceIterator.hasNext()) {
        OMNamespace namespace = namespaceIterator.next();
        String prefix = namespace.getPrefix();
        if (prefix.isEmpty()) {
            continue;
        }
        attrMap.put(namespaceOfPrefix + prefix, new BString(namespace.getNamespaceURI()));
    }

    Iterator<OMAttribute> attrIterator = ((OMElement) omNode).getAllAttributes();
    while (attrIterator.hasNext()) {
        OMAttribute attr = attrIterator.next();
        attrMap.put(attr.getQName().toString(), new BString(attr.getAttributeValue()));
    }

    attrMap.finishConstruction();
    return attrMap;
}

From source file:org.betaconceptframework.astroboa.engine.jcr.io.contenthandler.ImportContentHandler.java

private void importAttributes(Attributes atts) throws SAXException {

    if (atts != null && atts.getLength() > 0) {
        for (int i = 0; i < atts.getLength(); i++) {
            //Ignore attributes with specific namespaces
            String uri = atts.getURI(i);

            if (StringUtils.equals(uri, XMLConstants.XMLNS_ATTRIBUTE_NS_URI)
                    || StringUtils.equals(uri, XMLConstants.XML_NS_URI)
                    || StringUtils.equals(uri, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
                    || StringUtils.equals(uri, XMLConstants.W3C_XML_SCHEMA_NS_URI)
                    || StringUtils.equals(uri, XMLConstants.W3C_XPATH_DATATYPE_NS_URI)) {
                continue;
            }//from  w w w.j  a va2s .co  m

            addAttributeToImportedEntity(atts.getLocalName(i), atts.getValue(i));
        }
    }

}

From source file:org.dhatim.delivery.dom.DOMBuilder.java

public void startElement(StartElementEvent startEvent) throws SAXException {
    Element newElement = null;/*  w w  w  . j a  v  a 2 s.c  o  m*/
    int attsCount = startEvent.atts.getLength();
    Node currentNode = (Node) nodeStack.peek();

    try {
        if (startEvent.uri != null && startEvent.qName != null && !startEvent.qName.equals("")) {
            newElement = ownerDocument.createElementNS(startEvent.uri.intern(), startEvent.qName);
        } else {
            newElement = ownerDocument.createElement(startEvent.localName.intern());
        }

        currentNode.appendChild(newElement);
        if (!emptyElements.contains(startEvent.qName != null ? startEvent.qName : startEvent.localName)) {
            nodeStack.push(newElement);
        }
    } catch (DOMException e) {
        logger.error("DOMException creating start element: namespaceURI=" + startEvent.uri + ", localName="
                + startEvent.localName, e);
        throw e;
    }

    for (int i = 0; i < attsCount; i++) {
        String attNamespace = startEvent.atts.getURI(i);
        String attQName = startEvent.atts.getQName(i);
        String attLocalName = startEvent.atts.getLocalName(i);
        String attValue = startEvent.atts.getValue(i);
        try {
            if (attNamespace != null && attQName != null) {
                attNamespace = attNamespace.intern();
                if (attNamespace.equals(XMLConstants.NULL_NS_URI)) {
                    if (attQName.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
                        attNamespace = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
                    } else if (attQName.startsWith("xml:")) {
                        attNamespace = XMLConstants.XML_NS_URI;
                    }
                }
                newElement.setAttributeNS(attNamespace, attQName, attValue);
            } else {
                newElement.setAttribute(attLocalName.intern(), attValue);
            }
        } catch (DOMException e) {
            logger.error("DOMException setting element attribute " + attLocalName + "=" + attValue
                    + "[namespaceURI=" + startEvent.uri + ", localName=" + startEvent.localName + "].", e);
            throw e;
        }
    }
}

From source file:org.dspace.app.xmlui.aspect.submission.submit.JSONLookupSearcher.java

@Override
public void generate() throws IOException, SAXException, ProcessingException {
    String query = request.getParameter("search");

    int start = 0;
    String startString = request.getParameter("start");
    if (StringUtils.isNotBlank(startString)) {
        int parsedStart = Integer.parseInt(startString);
        if (parsedStart >= 0) {
            start = parsedStart;//from  www .  j a  va2s  . co m
        }
    }

    try {
        int total = importService.getNbRecords(getLookupURI(), query);
        Collection<ImportRecord> records = importService.getRecords(getLookupURI(), query, start, 20);

        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        org.w3c.dom.Document document = docBuilder.newDocument();

        Element rootnode = document.createElement("root");
        document.appendChild(rootnode);
        rootnode.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:i18n", WingConstants.I18N.URI);

        Element totalNode = document.createElement("total");
        totalNode.setTextContent(String.valueOf(total));
        rootnode.appendChild(totalNode);

        Element startNode = document.createElement("start");
        startNode.setTextContent(String.valueOf(start));
        rootnode.appendChild(startNode);

        Element recordsNode = document.createElement("records");
        recordsNode.setAttribute("array", "true");
        rootnode.appendChild(recordsNode);
        recordsNode.setAttribute("array", "true");

        MetadataFieldConfig importIdField = new DSpace().getServiceManager().getServiceByName("lookupID",
                MetadataFieldConfig.class);

        for (ImportRecord record : records) {
            Element recordWrapperNode = document.createElement("recordWrapper");
            recordWrapperNode.setAttribute("object", "true");
            recordsNode.appendChild(recordWrapperNode);

            Element recordNode = document.createElement("record");
            recordNode.setAttribute("namedObject", "true");

            HashMap<String, Element> metadatumValueNodes = new HashMap();

            for (MetadatumDTO metadatum : record.getValueList()) {
                if (!metadatumValueNodes.containsKey(getField(metadatum))) {
                    Element metadatumNode = document.createElement(getField(metadatum));
                    metadatumNode.setAttribute("array", "true");
                    metadatumValueNodes.put(getField(metadatum), metadatumNode);

                    if (getField(metadatum).equals(importIdField.getField())) {
                        Iterator<Item> iterator = itemService.findByMetadataField(context,
                                importIdField.getSchema(), importIdField.getElement(),
                                importIdField.getQualifier(), metadatum.getValue());

                        if (iterator.hasNext()) {
                            Element existsInDSpaceNode = document.createElement("imported");
                            existsInDSpaceNode.setTextContent("true");
                            recordNode.appendChild(existsInDSpaceNode);
                        }
                    }
                }

                Element metadatumValueNode = document.createElement("metadatumValue");
                metadatumValueNode.setTextContent(metadatum.getValue());

                metadatumValueNodes.get(getField(metadatum)).appendChild(metadatumValueNode);
            }

            for (Element element : metadatumValueNodes.values()) {
                recordNode.appendChild(element);
            }

            recordWrapperNode.appendChild(recordNode);
        }

        DOMStreamer streamer = new DOMStreamer(contentHandler, lexicalHandler);
        streamer.stream(document);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.smila.datamodel.record.dom.RecordBuilder.java

/**
 * Builds the record as document element.
 *
 * @param factory/*w  w  w  .  j  a v  a  2  s .c o m*/
 *          the factory
 * @param record
 *          the record
 */
public void buildRecordAsDocumentElement(final Document factory, final Record record) {
    final Element recordElement = buildRecord(factory, record);
    recordElement.setAttribute(ATTRIBUTE_VERSION, SCHEMA_VERSION_RECORD);
    recordElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, ATTRIBUTE_XMLNSREC, NAMESPACE_RECORD);
    factory.appendChild(recordElement);
}

From source file:org.eclipse.smila.datamodel.xml.DOMRecordWriter.java

/**
 * Builds the record as document element.
 * /*from www.  j  a  v  a 2s . c  om*/
 * @param factory
 *          the factory
 * @param record
 *          the record
 */
public void buildRecordAsDocumentElement(final Document factory, final Record record) {
    final Element recordElement = buildRecord(factory, record);
    recordElement.setAttribute(XmlConstants.ATTRIBUTE_VERSION, XmlConstants.SCHEMA_VERSION_RECORD);
    recordElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XmlConstants.ATTRIBUTE_XMLNSREC,
            XmlConstants.NAMESPACE_RECORD);
    factory.appendChild(recordElement);
}

From source file:org.opencastproject.mediapackage.XMLCatalogImpl.java

/**
 * Creates an abstract metadata container.
 * //from  w  w w.  j  av  a2s  .  com
 * @param id
 *          the element identifier withing the package
 * @param flavor
 *          the catalog flavor
 * @param uri
 *          the document location
 * @param size
 *          the catalog size in bytes
 * @param checksum
 *          the catalog checksum
 * @param mimeType
 *          the catalog mime type
 */
protected XMLCatalogImpl(String id, MediaPackageElementFlavor flavor, URI uri, long size, Checksum checksum,
        MimeType mimeType) {
    super(id, flavor, uri, size, checksum, mimeType);
    bindPrefix(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    bindPrefix(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
    bindPrefix(XSI_NS_PREFIX, XSI_NS_URI);
}