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:org.jivesoftware.openfire.websocket.XmppWebSocket.java

private void processStanza(Element stanza) {

    try {//from  w  ww.ja va  2s .  c o m
        String tag = stanza.getName();
        if (STREAM_FOOTER.equals(tag)) {
            closeStream(null);
        } else if ("auth".equals(tag)) {
            // User is trying to authenticate using SASL
            startedSASL = true;
            // Process authentication stanza
            xmppSession.incrementClientPacketCount();
            saslStatus = SASLAuthentication.handle(xmppSession, stanza);
        } else if (startedSASL && "response".equals(tag) || "abort".equals(tag)) {
            // User is responding to SASL challenge. Process response
            xmppSession.incrementClientPacketCount();
            saslStatus = SASLAuthentication.handle(xmppSession, stanza);
        } else if (STREAM_HEADER.equals(tag)) {
            // restart the stream
            openStream(stanza.attributeValue(QName.get("lang", XMLConstants.XML_NS_URI), "en"),
                    stanza.attributeValue("from"));
            configureStream();
        } else if (Status.authenticated.equals(saslStatus)) {
            if (router == null) {
                if (isStreamManagementAvailable()) {
                    router = new StreamManagementPacketRouter(xmppSession);
                } else {
                    // fall back for older Openfire installations
                    router = new SessionPacketRouter(xmppSession);
                }
            }
            router.route(stanza);
        } else {
            // require authentication
            Log.warn("Not authorized: " + stanza.asXML());
            sendPacketError(stanza, PacketError.Condition.not_authorized);
        }
    } catch (UnknownStanzaException use) {
        Log.warn("Received invalid stanza: " + stanza.asXML());
        sendPacketError(stanza, PacketError.Condition.bad_request);
    } catch (Exception ex) {
        Log.error("Failed to process incoming stanza: " + stanza.asXML(), ex);
        closeStream(new StreamError(StreamError.Condition.internal_server_error));
    }
}

From source file:org.jivesoftware.openfire.websocket.XmppWebSocket.java

private void initiateSession(Element stanza) {

    String host = stanza.attributeValue("to");
    StreamError streamError = null;// ww  w. ja v a2s .c om
    Locale language = Locale
            .forLanguageTag(stanza.attributeValue(QName.get("lang", XMLConstants.XML_NS_URI), "en"));
    if (STREAM_FOOTER.equals(stanza.getName())) {
        // an error occurred while setting up the session
        Log.warn("Client closed stream before session was established");
    } else if (!STREAM_HEADER.equals(stanza.getName())) {
        streamError = new StreamError(StreamError.Condition.unsupported_stanza_type);
        Log.warn("Closing session due to incorrect stream header. Tag: " + stanza.getName());
    } else if (!FRAMING_NAMESPACE.equals(stanza.getNamespace().getURI())) {
        // Validate the stream namespace (https://tools.ietf.org/html/rfc7395#section-3.3.2)
        streamError = new StreamError(StreamError.Condition.invalid_namespace);
        Log.warn("Closing session due to invalid namespace in stream header. Namespace: "
                + stanza.getNamespace().getURI());
    } else if (!validateHost(host)) {
        streamError = new StreamError(StreamError.Condition.host_unknown);
        Log.warn("Closing session due to incorrect hostname in stream header. Host: " + host);
    } else {
        // valid stream; initiate session
        xmppSession = SessionManager.getInstance().createClientSession(wsConnection, language);
        xmppSession.setSessionData("ws", Boolean.TRUE);
    }

    if (xmppSession == null) {
        closeStream(streamError);
    } else {
        openStream(language.toLanguageTag(), stanza.attributeValue("from"));
        configureStream();
    }
}

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

/**
 * Creates an abstract metadata container.
 * /*  w  w  w .  ja  va  2  s.c  o m*/
 * @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);
}

From source file:org.osaf.cosmo.calendar.hcalendar.HCalendarParser.java

private void buildProperty(Element element, String propName, ContentHandler handler) throws ParserException {
    if (element == null)
        return;/*from w  ww .  j av a  2s  .  co  m*/

    if (log.isDebugEnabled())
        log.debug("Building property " + propName);

    String className = _className(propName);
    String elementName = element.getLocalName().toLowerCase();

    String value = null;
    if (elementName.equals("abbr")) {
        // "If an <abbr> element is used for a property, then the 'title'
        // attribute of the <abbr> element is the value of the property,
        // instead of the contents of the element, which instead provide a
        // human presentable version of the value."
        value = element.getAttribute("title");
        if (StringUtils.isBlank(value))
            throw new ParserException("Abbr element '" + className + "' requires a non-empty title", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from title attribute");
    } else if (isHeaderElement(elementName)) {
        // try title first. if that's not set, fall back to text content.
        value = element.getAttribute("title");
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from title attribute");
        } else {
            value = getTextContent(element);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    } else if (elementName.equals("a") && isUrlProperty(propName)) {
        value = element.getAttribute("href");
        if (StringUtils.isBlank(value))
            throw new ParserException("A element '" + className + "' requires a non-empty href", -1);
        if (log.isDebugEnabled())
            log.debug("Setting value '" + value + "' from href attribute");
    } else if (elementName.equals("img")) {
        if (isUrlProperty(propName)) {
            value = element.getAttribute("src");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty src", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from src attribute");
        } else {
            value = element.getAttribute("alt");
            if (StringUtils.isBlank(value))
                throw new ParserException("Img element '" + className + "' requires a non-empty alt", -1);
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from alt attribute");
        }
    } else {
        value = getTextContent(element);
        if (!StringUtils.isBlank(value)) {
            if (log.isDebugEnabled())
                log.debug("Setting value '" + value + "' from text content");
        }
    }

    if (StringUtils.isBlank(value)) {
        if (log.isDebugEnabled())
            log.debug("Skipping property with empty value");
        return;
    }

    handler.startProperty(propName);

    // if it's a date property, we have to convert from the
    // hCalendar-formatted date (RFC 3339) to an iCalendar-formatted date
    if (isDateProperty(propName)) {
        try {
            Date date = _icalDate(value);
            value = date.toString();

            if (!(date instanceof DateTime))
                try {
                    handler.parameter(Parameter.VALUE, Value.DATE.getValue());
                } catch (Exception e) {
                }
        } catch (ParseException e) {
            throw new ParserException("Malformed date value for element '" + className + "'", -1, e);
        }
    }

    if (isTextProperty(propName)) {
        String lang = element.getAttributeNS(XMLConstants.XML_NS_URI, "lang");
        if (!StringUtils.isBlank(lang))
            try {
                handler.parameter(Parameter.LANGUAGE, lang);
            } catch (Exception e) {
            }
    }

    // XXX: other parameters?

    try {
        handler.propertyValue(value);
    } catch (URISyntaxException e) {
        throw new ParserException("Malformed URI value for element '" + className + "'", -1, e);
    } catch (ParseException e) {
        throw new ParserException("Malformed value for element '" + className + "'", -1, e);
    } catch (IOException e) {
        throw new RuntimeException("Unknown error setting property value for element '" + className + "'", e);
    }

    handler.endProperty(propName);
}

From source file:org.trancecode.xproc.step.AddXmlBaseStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final XdmNode sourceDoc = input.readNode(XProcPorts.SOURCE);
    final boolean allOption = Boolean.valueOf(input.getOptionValue(XProcOptions.ALL, "false"));
    final boolean relativeOption = Boolean.valueOf(input.getOptionValue(XProcOptions.RELATIVE, "true"));
    final QName xmlBase = new QName("xml", XMLConstants.XML_NS_URI, "base");

    if (allOption && relativeOption) {
        throw XProcExceptions.xc0058(input.getStep().getLocation());
    }//  w w w  . j  a  v  a  2s . co  m

    final SaxonProcessorDelegate addXmlBaseDelegate = new CopyingSaxonProcessorDelegate() {
        @Override
        public EnumSet<NextSteps> startElement(final XdmNode node, final SaxonBuilder builder) {
            builder.startElement(node.getNodeName(), node);
            for (final XdmNode attribute : SaxonAxis.attributes(node)) {
                LOG.trace("copy existing attribute except xml base: {}", attribute);
                if (!xmlBase.equals(attribute.getNodeName())) {
                    builder.attribute(attribute.getNodeName(), attribute.getStringValue());
                }
            }
            if (allOption || XdmNodeKind.DOCUMENT.equals(node.getParent().getNodeKind())) {
                builder.attribute(xmlBase, node.getBaseURI().toString());
            } else {
                if (!node.getBaseURI().equals(node.getParent().getBaseURI())) {
                    if (relativeOption) {
                        final String attrBase = node.getParent().getBaseURI().resolve(node.getBaseURI())
                                .toString();
                        final int lastIdx = StringUtils.lastIndexOf(attrBase, "/") + 1;
                        builder.attribute(xmlBase, StringUtils.substring(attrBase, lastIdx));
                    } else {
                        builder.attribute(xmlBase, node.getBaseURI().toString());
                    }
                }
            }

            return EnumSet.of(NextSteps.PROCESS_CHILDREN, NextSteps.START_CONTENT);
        }
    };

    final SaxonProcessor addXmlBaseProcessor = new SaxonProcessor(input.getPipelineContext().getProcessor(),
            addXmlBaseDelegate);
    final XdmNode result = addXmlBaseProcessor.apply(sourceDoc);
    output.writeNodes(XProcPorts.RESULT, result);
}

From source file:org.trancecode.xproc.step.NamespaceRenameStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final XdmNode sourceDocument = input.readNode(XProcPorts.SOURCE);
    final String from = input.getOptionValue(XProcOptions.FROM);
    final String to = input.getOptionValue(XProcOptions.TO);
    final String apply_to = input.getOptionValue(XProcOptions.APPLY_TO, APPLY_TO_ALL);
    if (StringUtils.equalsIgnoreCase(XMLConstants.XML_NS_URI, from)
            || StringUtils.equalsIgnoreCase(XMLConstants.XML_NS_URI, to)
            || StringUtils.equalsIgnoreCase(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, from)
            || StringUtils.equalsIgnoreCase(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, to)) {
        throw XProcExceptions.xc0014(sourceDocument);
    }/*w w w.j  av  a2 s.c  om*/
    final Processor processor = input.getPipelineContext().getProcessor();

    final SaxonProcessorDelegate nsRename = new CopyingSaxonProcessorDelegate() {
        @Override
        public EnumSet<NextSteps> startElement(final XdmNode node, final SaxonBuilder builder) {
            final QName newNodeTo;
            if (APPLY_TO_ATTRIBUTE.equals(apply_to)) {
                newNodeTo = node.getNodeName();
            } else {
                if (StringUtils.isNotBlank(to)) {
                    if (node.getNodeName().getNamespaceURI().equals(from)) {
                        newNodeTo = Steps.getNewNamespace(node.getNodeName().getPrefix(), to,
                                node.getNodeName().getLocalName(), SaxonLocation.of(node), node, processor);
                    } else if (StringUtils.isNotBlank(from)) {
                        newNodeTo = node.getNodeName();
                    } else {
                        newNodeTo = Steps.getNewNamespace("", to, node.getNodeName().getLocalName(),
                                SaxonLocation.of(node), node, processor);
                    }
                } else {
                    if (node.getNodeName().getNamespaceURI().equals(from)) {
                        newNodeTo = new QName(node.getNodeName().getLocalName());
                    } else {
                        newNodeTo = node.getNodeName();
                    }
                }
            }
            builder.startElement(newNodeTo);
            return EnumSet.of(NextSteps.PROCESS_ATTRIBUTES, NextSteps.PROCESS_CHILDREN,
                    NextSteps.START_CONTENT);
        }

        @Override
        public void attribute(final XdmNode node, final SaxonBuilder builder) {
            if (!APPLY_TO_ELEMENTS.equals(apply_to)) {
                final QName newNodeTo;
                if (StringUtils.isNotBlank(to)) {
                    if (node.getNodeName().getNamespaceURI().equals(from)) {
                        newNodeTo = Steps.getNewNamespace(node.getNodeName().getPrefix(), to,
                                node.getNodeName().getLocalName(), SaxonLocation.of(node), node, processor);
                    } else if (StringUtils.isNotBlank(from)
                            || StringUtils.isNotBlank(node.getNodeName().getNamespaceURI())) {
                        newNodeTo = node.getNodeName();
                    } else {
                        newNodeTo = Steps.getNewNamespace(null, to, node.getNodeName().getLocalName(),
                                SaxonLocation.of(node), node, processor);
                    }
                } else {
                    if (node.getNodeName().getNamespaceURI().equals(from)) {
                        newNodeTo = new QName(node.getNodeName().getLocalName());
                    } else {
                        newNodeTo = node.getNodeName();
                    }
                }
                builder.attribute(newNodeTo, node.getStringValue());
            } else {
                super.attribute(node, builder);
            }
        }
    };

    final SaxonProcessor saxonProcessor = new SaxonProcessor(input.getPipelineContext().getProcessor(),
            nsRename);

    final XdmNode result = saxonProcessor.apply(sourceDocument);
    output.writeNodes(XProcPorts.RESULT, result);
}

From source file:org.unitedinternet.cosmo.calendar.hcalendar.HCalendarParser.java

/**
 * Builds property.//from   w ww.  ja  v  a2s.c  o  m
 * @param element The element.
 * @param propName The prop name.
 * @param handler The content handler.
 * @throws ParserException - if something is wrong this exception is thrown.
 */
private void buildProperty(Element element, String propName, ContentHandler handler) throws ParserException {
    if (element == null) {
        return;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Building property " + propName);
    }

    String className = className(propName);
    String elementName = element.getLocalName().toLowerCase(CosmoConstants.LANGUAGE_LOCALE);

    String value = null;
    if (elementName.equals("abbr")) {
        // "If an <abbr> element is used for a property, then the 'title'
        // attribute of the <abbr> element is the value of the property,
        // instead of the contents of the element, which instead provide a
        // human presentable version of the value."
        value = element.getAttribute("title");
        if (StringUtils.isBlank(value)) {
            throw new ParserException("Abbr element '" + className + "' requires a non-empty title", -1);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Setting value '" + value + "' from title attribute");
        }
    } else if (isHeaderElement(elementName)) {
        // try title first. if that's not set, fall back to text content.
        value = element.getAttribute("title");
        if (!StringUtils.isBlank(value)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting value '" + value + "' from title attribute");
            }
        } else {
            value = getTextContent(element);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting value '" + value + "' from text content");
            }
        }
    } else if (elementName.equals("a") && isUrlProperty(propName)) {
        value = element.getAttribute("href");
        if (StringUtils.isBlank(value)) {
            throw new ParserException("A element '" + className + "' requires a non-empty href", -1);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Setting value '" + value + "' from href attribute");
        }
    } else if (elementName.equals("img")) {
        if (isUrlProperty(propName)) {
            value = element.getAttribute("src");
            if (StringUtils.isBlank(value)) {
                throw new ParserException("Img element '" + className + "' requires a non-empty src", -1);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting value '" + value + "' from src attribute");
            }
        } else {
            value = element.getAttribute("alt");
            if (StringUtils.isBlank(value)) {
                throw new ParserException("Img element '" + className + "' requires a non-empty alt", -1);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting value '" + value + "' from alt attribute");
            }
        }
    } else {
        value = getTextContent(element);
        if (!StringUtils.isBlank(value) && LOG.isDebugEnabled()) {
            LOG.debug("Setting value '" + value + "' from text content");
        }
    }

    if (StringUtils.isBlank(value) && LOG.isDebugEnabled()) {
        LOG.debug("Skipping property with empty value");
        return;
    }

    handler.startProperty(propName);

    // if it's a date property, we have to convert from the
    // hCalendar-formatted date (RFC 3339) to an iCalendar-formatted date
    if (isDateProperty(propName)) {
        try {
            Date date = icalDate(value);
            value = date.toString();

            if (!(date instanceof DateTime)) {
                try {
                    handler.parameter(Parameter.VALUE, Value.DATE.getValue());
                } catch (URISyntaxException e) {
                    LOG.warn("", e);
                }
            }
        } catch (ParseException e) {
            throw new ParserException("Malformed date value for element '" + className + "'", -1, e);
        }
    }

    if (isTextProperty(propName)) {
        String lang = element.getAttributeNS(XMLConstants.XML_NS_URI, "lang");
        if (!StringUtils.isBlank(lang)) {
            try {
                handler.parameter(Parameter.LANGUAGE, lang);
            } catch (Exception e) {
                LOG.warn("", e);
            }
        }
    }

    // XXX: other parameters?

    try {
        handler.propertyValue(value);
    } catch (URISyntaxException e) {
        throw new ParserException("Malformed URI value for element '" + className + "'", -1, e);
    } catch (ParseException e) {
        throw new ParserException("Malformed value for element '" + className + "'", -1, e);
    } catch (IOException e) {
        throw new CosmoIOException("Unknown error setting property value for element '" + className + "'", e);
    }

    handler.endProperty(propName);
}