Java Code Examples for org.w3c.dom.Node#setUserData()

The following examples show how to use org.w3c.dom.Node#setUserData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MergerXmlUtils.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * This is a CRUDE INEXACT HACK to decorate the DOM with some kind of line number
 * information for elements. It's inexact because by the time we get the DOM we
 * already have lost all the information about whitespace between attributes.
 * <p/>
 * Also we don't even try to deal with \n vs \r vs \r\n insanity. This only counts
 * the \n occurring in text nodes to determine line advances, which is clearly flawed.
 * <p/>
 * However it's good enough for testing, and we'll replace it by a PositionXmlParser
 * once it's moved into com.android.util.
 */
private static int findLineNumbers(Node node, int line) {
    for (; node != null; node = node.getNextSibling()) {
        node.setUserData(DATA_LINE_NUMBER, Integer.valueOf(line), null /*handler*/);

        if (node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getNodeValue();
            if (text.length() > 0) {
                for (int pos = 0; (pos = text.indexOf('\n', pos)) != -1; pos++) {
                    ++line;
                }
            }
        }

        Node child = node.getFirstChild();
        if (child != null) {
            line = findLineNumbers(child, line);
        }
    }
    return line;
}
 
Example 2
Source File: MergerXmlUtils.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This is a CRUDE INEXACT HACK to decorate the DOM with some kind of line number
 * information for elements. It's inexact because by the time we get the DOM we
 * already have lost all the information about whitespace between attributes.
 * <p/>
 * Also we don't even try to deal with \n vs \r vs \r\n insanity. This only counts
 * the \n occurring in text nodes to determine line advances, which is clearly flawed.
 * <p/>
 * However it's good enough for testing, and we'll replace it by a PositionXmlParser
 * once it's moved into com.android.util.
 */
private static int findLineNumbers(Node node, int line) {
    for (; node != null; node = node.getNextSibling()) {
        node.setUserData(DATA_LINE_NUMBER, Integer.valueOf(line), null /*handler*/);

        if (node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getNodeValue();
            if (!text.isEmpty()) {
                for (int pos = 0; (pos = text.indexOf('\n', pos)) != -1; pos++) {
                    ++line;
                }
            }
        }

        Node child = node.getFirstChild();
        if (child != null) {
            line = findLineNumbers(child, line);
        }
    }
    return line;
}
 
Example 3
Source File: XmlLocationAnnotator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(short operation, String key, Object data,
        Node src, Node dst) {
   
    if (src != null && dst != null) {
        XmlLocationData locatonData = (XmlLocationData)
                src.getUserData(XmlLocationData.LOCATION_DATA_KEY);
       
        if (locatonData != null) {
            dst.setUserData(XmlLocationData.LOCATION_DATA_KEY,
                    locatonData, dataHandler);
        }
    }
}
 
Example 4
Source File: StaxUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static boolean addLocation(Document doc, Node node,
                                   Location loc,
                                   boolean recordLoc) {
    if (recordLoc && loc != null && (loc.getColumnNumber() != 0 || loc.getLineNumber() != 0)) {
        try {
            final int charOffset = loc.getCharacterOffset();
            final int colNum = loc.getColumnNumber();
            final int linNum = loc.getLineNumber();
            final String pubId = loc.getPublicId() == null ? doc.getDocumentURI() : loc.getPublicId();
            final String sysId = loc.getSystemId() == null ? doc.getDocumentURI() : loc.getSystemId();
            Location loc2 = new Location() {
                public int getCharacterOffset() {
                    return charOffset;
                }
                public int getColumnNumber() {
                    return colNum;
                }
                public int getLineNumber() {
                    return linNum;
                }
                public String getPublicId() {
                    return pubId;
                }
                public String getSystemId() {
                    return sysId;
                }
            };
            node.setUserData("location", loc2, LocationUserDataHandler.INSTANCE);
        } catch (Throwable ex) {
            //possibly not DOM level 3, won't be able to record this then
            return false;
        }
    }
    return recordLoc;
}
 
Example 5
Source File: XmlLocationAnnotator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(short operation, String key, Object data,
        Node src, Node dst) {
   
    if (src != null && dst != null) {
        XmlLocationData locatonData = (XmlLocationData)
                src.getUserData(XmlLocationData.LOCATION_DATA_KEY);
       
        if (locatonData != null) {
            dst.setUserData(XmlLocationData.LOCATION_DATA_KEY,
                    locatonData, dataHandler);
        }
    }
}
 
Example 6
Source File: MergerXmlUtils.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public static void setSource(@NonNull Node node, @NonNull File source) {
    //noinspection ConstantConditions
    for (; node != null; node = node.getNextSibling()) {
        short nodeType = node.getNodeType();
        if (nodeType == Node.ELEMENT_NODE
                || nodeType == Node.COMMENT_NODE
                || nodeType == Node.DOCUMENT_NODE
                || nodeType == Node.CDATA_SECTION_NODE) {
            node.setUserData(DATA_ORIGIN_FILE, source, null);
        }
        Node child = node.getFirstChild();
        setSource(child, source);
    }
}
 
Example 7
Source File: DynamicAttributesProvider.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
private void setRebuild() {
    Node node = getNode();
    if (null != node) {
        node.setUserData(DATA_CATEGORY_OBJECT, new Object(), null);
    }
    _needToRebuild = true;
}
 
Example 8
Source File: Location.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(final short operation, final String key, final Object data,
        final Node src, final Node dst) {

    if (operation == NODE_IMPORTED || operation == NODE_CLONED) {
        if (dst != null) {
            dst.setUserData(key, data, this);
        }
    }
}
 
Example 9
Source File: XmlLocationAnnotator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(short operation, String key, Object data,
        Node src, Node dst) {
   
    if (src != null && dst != null) {
        XmlLocationData locatonData = (XmlLocationData)
                src.getUserData(XmlLocationData.LOCATION_DATA_KEY);
       
        if (locatonData != null) {
            dst.setUserData(XmlLocationData.LOCATION_DATA_KEY,
                    locatonData, dataHandler);
        }
    }
}
 
Example 10
Source File: Nodes.java    From caja with Apache License 2.0 5 votes vote down vote up
public void handle(
    short operation, String key, Object data, Node src, Node dest) {
  switch (operation) {
    case UserDataHandler.NODE_CLONED:
    case UserDataHandler.NODE_IMPORTED:
      dest.setUserData(HAS_XMLNS_DECLARATION_KEY, Boolean.TRUE, this);
      break;
  }
}
 
Example 11
Source File: MergerXmlUtils.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the file associated with the given node, if any
 */
public static void setFileFor(Node node, File file) {
    node.setUserData(MergerXmlUtils.DATA_ORIGIN_FILE, file, null);
}
 
Example 12
Source File: XmlNode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private static void setUserData(Node node, XmlNode wrap) {
    if (DOM_LEVEL_3) {
        node.setUserData(USER_DATA_XMLNODE_KEY, wrap, wrap.events);
    }
}
 
Example 13
Source File: LocationAwareContentHandler.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private void storeLineInformation(Node e) {
    e.setUserData(LINE_NUMBER_KEY_NAME, this.locator.getLineNumber(), null);
       e.setUserData(COLUMN_NUMBER_KEY_NAME, this.locator.getColumnNumber(), null);
}
 
Example 14
Source File: StaxUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handle(short operation, String key, Object data, Node src, Node dst) {
    if (operation == NODE_CLONED) {
        dst.setUserData(key, data, this);
    }
}
 
Example 15
Source File: LocationRecordingProgressReportingHandler.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 4 votes vote down vote up
protected void setLocationData(Node n) {
    if (locator != null) {
        n.setUserData(KEY_LIN_NO, locator.getLineNumber(), null);
        n.setUserData(KEY_COL_NO, locator.getColumnNumber(), null);
    }
}
 
Example 16
Source File: JAXBDataBinding.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Node cloneNode(Document document, Node node, boolean deep) throws DOMException {
    if (document == null || node == null) {
        return null;
    }
    int type = node.getNodeType();

    if (node.getOwnerDocument() == document) {
        return node.cloneNode(deep);
    }
    Node clone;
    switch (type) {
    case Node.CDATA_SECTION_NODE:
        clone = document.createCDATASection(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        clone = document.createComment(node.getNodeValue());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        clone = document.createEntityReference(node.getNodeName());
        break;
    case Node.ELEMENT_NODE:
        clone = document.createElement(node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            ((Element)clone).setAttributeNS(attributes.item(i).getNamespaceURI(),
                                            attributes.item(i).getNodeName(),
                                            attributes.item(i).getNodeValue());
        }
        try {
            clone.setUserData("location", node.getUserData("location"), null);
        } catch (Throwable t) {
            //non DOM level 3
        }
        break;

    case Node.TEXT_NODE:
        clone = document.createTextNode(node.getNodeValue());
        break;
    default:
        return null;
    }
    if (deep && type == Node.ELEMENT_NODE) {
        Node child = node.getFirstChild();
        while (child != null) {
            clone.appendChild(cloneNode(document, child, true));
            child = child.getNextSibling();
        }
    }
    return clone;
}
 
Example 17
Source File: XmlUtils.java    From buck with Apache License 2.0 4 votes vote down vote up
public static void attachSourceFile(Node node, SourceFile sourceFile) {
    node.setUserData(SOURCE_FILE_USER_DATA_KEY, sourceFile, null);
}
 
Example 18
Source File: XmlNode.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private static void setUserData(Node node, XmlNode wrap) {
    if (DOM_LEVEL_3) {
        node.setUserData(USER_DATA_XMLNODE_KEY, wrap, wrap.events);
    }
}
 
Example 19
Source File: MergerXmlUtils.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the file associated with the given node, if any
 */
public static void setFileFor(Node node, File file) {
    node.setUserData(MergerXmlUtils.DATA_ORIGIN_FILE, file, null);
}
 
Example 20
Source File: LocationAnnotator.java    From metafacture-core with Apache License 2.0 3 votes vote down vote up
@Override
public void endElement(final String uri, final String localName, final String qName)
        throws SAXException {

    super.endElement(uri, localName, qName);

    final Locator elementStartLocator = locatorsAtElementStart.pop();

    final Location location = new Location(elementStartLocator, locator);

    final Node domNode = domNodes.pop();
    domNode.setUserData(Location.USER_DATA_ID, location, Location.USER_DATA_HANDLER);
}