Example usage for javax.xml XMLConstants XML_NS_URI

List of usage examples for javax.xml XMLConstants XML_NS_URI

Introduction

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

Prototype

String XML_NS_URI

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

Click Source Link

Document

The official XML Namespace name URI.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//w w w  . j a v  a 2s  .  c o  m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    Element rootElement = document.createElement("root");
    document.appendChild(rootElement);
    rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve");

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:Main.java

public static boolean isSpacePreserved(Node node) {
    if (node == null) {
        return false;
    }/*from   ww  w  .j a  v  a 2 s.  c om*/
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        final String spaceAttr = ((Element) node).getAttributeNS(XMLConstants.XML_NS_URI, "space");
        if (spaceAttr != null) {
            return ("preserve".equals(spaceAttr));
        }
    }
    return isSpacePreserved(node.getParentNode());
}

From source file:Main.java

public static String getNamespaceUriDeclaration(Element ele) {
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) {
            return attr.getTextContent();
        }//  www.  ja v a  2s  .  c o  m
    }

    return "";
}

From source file:MainClass.java

public SimpleNamespaceContext() {
    addNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    addNamespace(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
}

From source file:com.elsevier.xml.NamespaceContextMappings.java

/**
 * Init the cache with the passed HashMap of prefixes and namespaces. 
 * //ww  w. ja va2  s.c  o m
 * @param initPrefixNamespaceMap HashMap of prefixes and namespaces
 */
public static void init(HashMap<String, String> initPrefixNamespaceMap) {

    if (prefixNamespaceMap == null) {
        synchronized (NamespaceContextMappings.class) {
            if (prefixNamespaceMap == null) {

                // Add the defaults
                prefixNamespaceMap = new HashMap<String, String>();
                prefixNamespaceMap.put("xml", XMLConstants.XML_NS_URI);
                prefixNamespaceMap.put("fn", "http://www.w3.org/2005/xpath-functions");
                prefixNamespaceMap.put("xs", "http://www.w3.org/2001/XMLSchema");

                // Add those passed to init
                prefixNamespaceMap.putAll(initPrefixNamespaceMap);

                // Output the list of namespace prefix to uri mappings
                Set<String> keys = prefixNamespaceMap.keySet();
                Iterator<String> it = keys.iterator();
                log.info("** Namespace Mappings **");
                while (it.hasNext()) {
                    String key = it.next();
                    String val = prefixNamespaceMap.get(key);
                    log.info(key + "=" + val);
                }

            }
        }
    }

}

From source file:org.maodian.flyingcat.xmpp.codec.InfoQueryCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {//from w  ww.jav  a 2  s .  c  om
        String id = xmlsr.getAttributeValue("", "id");
        String type = xmlsr.getAttributeValue("", "type");
        Builder builder = new Builder(id, type);

        String from = xmlsr.getAttributeValue("", "from");
        String to = xmlsr.getAttributeValue("", "to");
        String language = xmlsr.getAttributeValue(XMLConstants.XML_NS_URI, "lang");
        builder.from(from).to(to).language(language);

        switch (type) {
        case InfoQuery.GET:
        case InfoQuery.SET:
            if (xmlsr.nextTag() != XMLStreamConstants.START_ELEMENT) {
                throw new XmppException(StanzaErrorCondition.BAD_REQUEST);
            }
            QName key = xmlsr.getName();
            builder.payload(findDecoder(key, builder.build()).decode(xmlsr));
            break;
        case InfoQuery.RESULT:
            // do nothing
            break;
        case InfoQuery.ERROR:
            throw new IllegalStateException(
                    "Since this is a server, it should not dealing with incoming result and error");
        default:
            break;
        }
        return builder.build();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.maodian.flyingcat.xmpp.codec.InfoQueryCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    InfoQuery iq = (InfoQuery) object;/*from   www . j  a v a 2  s .c o  m*/
    xmlsw.writeStartElement("iq");
    writeRequiredAttribute(xmlsw, "id", iq.getId());
    writeRequiredAttribute(xmlsw, "type", iq.getType());

    writeAttributeIfNotBlank(xmlsw, "from", iq.getFrom());
    writeAttributeIfNotBlank(xmlsw, "to", iq.getTo());
    writeAttributeIfNotBlank(xmlsw, XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI, "lang",
            iq.getLanguage());

    Object payload = iq.getPayload();
    if (payload != null) {
        Encoder encoder = findEncoder(payload.getClass());
        encoder.encode(payload, xmlsw);
    }

    xmlsw.writeEndDocument();
}

From source file:com.jkoolcloud.tnt4j.streams.utils.NamespaceMap.java

@Override
public String getNamespaceURI(String prefix) {
    String uri = mapNS.get(prefix);
    if (uri == null) {
        uri = XMLConstants.XML_NS_URI;
    }//from  ww w  .  j  av a2 s. c om
    return uri;
}

From source file:org.maodian.flyingcat.xmpp.state.StreamState.java

private void doHandle(XmppContext context, XMLStreamReader xmlsr, XMLStreamWriter xmlsw)
        throws XMLStreamException {
    xmlsr.nextTag();//from   ww  w .jav a2 s.c  om
    QName qname = new QName(XmppNamespace.STREAM, "stream");
    if (!xmlsr.getName().equals(qname)) {
        throw new XmppException(StreamError.INVALID_NAMESPACE).set("QName", xmlsr.getName());
    }

    // throw exception if client version > 1.0
    BigDecimal version = new BigDecimal(xmlsr.getAttributeValue("", "version"));
    if (version.compareTo(SUPPORTED_VERSION) > 0) {
        throw new XmppException(StreamError.UNSUPPORTED_VERSION);
    }

    xmlsw.writeStartDocument();
    xmlsw.writeStartElement("stream", "stream", XmppNamespace.STREAM);
    xmlsw.writeNamespace("stream", XmppNamespace.STREAM);
    xmlsw.writeDefaultNamespace(XmppNamespace.CLIENT_CONTENT);

    xmlsw.writeAttribute("id", RandomStringUtils.randomAlphabetic(32));
    xmlsw.writeAttribute("version", "1.0");
    xmlsw.writeAttribute("from", "localhost");
    xmlsw.writeAttribute(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI, "lang", "en");
    String from = xmlsr.getAttributeValue(null, "from");
    if (from != null) {
        xmlsw.writeAttribute("to", from);
    }

    // features
    xmlsw.writeStartElement(XmppNamespace.STREAM, "features");
    writeFeatures(xmlsw);
    xmlsw.writeEndElement();
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityXmlParser.java

/**
 * Constructs a new activity XML string parser.
 *
 * @throws ParserConfigurationException/*  w  w  w.  j a  va2  s . co m*/
 *             if any errors configuring the parser
 */
public ActivityXmlParser() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    builder = domFactory.newDocumentBuilder();
    xPath = StreamsXMLUtils.getStreamsXPath();

    if (namespaces == null) {
        if (xPath.getNamespaceContext() instanceof NamespaceMap) {
            namespaces = (NamespaceMap) xPath.getNamespaceContext();
        } else {
            namespaces = new NamespaceMap();
            xPath.setNamespaceContext(namespaces);
        }
    }

    namespaces.addPrefixUriMapping(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    namespaces.addPrefixUriMapping("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); // NON-NLS
}