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: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);
    }//from  ww w .jav a 2s  .c o m
    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);
}