Java XML Attribute Remove removePrefixes(final String xml, final boolean ignoreAttrValues)

Here you can find the source of removePrefixes(final String xml, final boolean ignoreAttrValues)

Description

Removes prefixes from an XML String

License

Open Source License

Parameter

Parameter Description
xml an XML String
ignoreAttrValues whether the values of attributes should be ignored when removing prefixes

Return

the XML String without prefixes

Declaration

public final static String removePrefixes(final String xml, final boolean ignoreAttrValues) 

Method Source Code

//package com.java2s;
/**/*  w  w  w . ja  va  2 s. c om*/
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import org.w3c.dom.*;

import java.util.regex.Pattern;

public class Main {
    private final static Pattern PREFIX_PATTERN = Pattern.compile("[^<\"'/\\s]+:");
    private final static char[] ATTRIBUTE_DELIMITERS = { '"', '\'' };

    /**
     * Removes prefixes from an XML String
     * 
     * @param xml an XML String
     * @return the XML String without prefixes
     **/
    public final static String removePrefixes(final String xml) {
        return removePrefixes(xml, true);
    }

    /**
     * Removes prefixes from an XML String
     * 
     * @param xml an XML String
     * @param ignoreAttrValues whether the values of attributes should be ignored when removing
     *            prefixes
     * @return the XML String without prefixes
     **/
    public final static String removePrefixes(final String xml, final boolean ignoreAttrValues) {
        final int len = xml.length();
        int start = -2, stop = -1, stop2;
        final StringBuilder sb = new StringBuilder(len);
        final char[] c = xml.toCharArray();

        while (start != -1) {
            start = xml.indexOf('<', start + 1);
            // Append text outside of tags as is
            sb.append(c, stop + 1, (start < 0 ? len : start) - stop - 1);
            if (start >= 0) {
                stop = xml.indexOf('>', start + 1);
                if (ignoreAttrValues) {
                    for (int i = 0; i < ATTRIBUTE_DELIMITERS.length; i++) {
                        stop2 = xml.indexOf(ATTRIBUTE_DELIMITERS[i], start + 1);
                        if ((stop2 >= 0) && (stop2 < stop)) {
                            stop = stop2;
                        }
                    }
                }
                // Append text with tags after removing prefixes
                sb.append(PREFIX_PATTERN.matcher(xml.substring(start, stop + 1)).replaceAll(""));
            }
        }
        return sb.toString();
    }

    /**
     * Retrieves the String of a Node
     * 
     * @param n the Node
     * @return the String
     **/
    public final static String toString(final Node n) {
        if (n == null) {
            return null;
        }

        return n instanceof Text ? n.getNodeValue() : n.getNodeName();
    }

    public final static String toString(final short nodeType) {
        return toClass(nodeType).getSimpleName();
    }

    public final static Class<? extends Node> toClass(final short nodeType) {
        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            return Attr.class;
        case Node.CDATA_SECTION_NODE:
            return CDATASection.class;
        case Node.COMMENT_NODE:
            return Comment.class;
        case Node.DOCUMENT_FRAGMENT_NODE:
            return DocumentFragment.class;
        case Node.DOCUMENT_NODE:
            return Document.class;
        case Node.DOCUMENT_TYPE_NODE:
            return DocumentType.class;
        case Node.ELEMENT_NODE:
            return Element.class;
        case Node.ENTITY_NODE:
            return Entity.class;
        case Node.ENTITY_REFERENCE_NODE:
            return EntityReference.class;
        case Node.NOTATION_NODE:
            return Notation.class;
        case Node.PROCESSING_INSTRUCTION_NODE:
            return ProcessingInstruction.class;
        case Node.TEXT_NODE:
            return Text.class;
        }
        throw new RuntimeException("Unrecognized node type " + nodeType);
    }
}

Related

  1. removeInvalidAttributes(Element element, String... validAttributeNames)
  2. removeNodeAttribute(Node node, String attributeName)
  3. removeNodeAttribute(Node node, String name)
  4. removeNodeAttributes(Node node)
  5. removeNodeContents(Node aSource, boolean aRemoveAttrs)
  6. removeUnspecifiedIfmapAttributes(Node meta)
  7. removeXmlBaseAttributes(Node node)
  8. removeXmlNsAttribute(final Node node)