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

The following examples show how to use org.w3c.dom.Node#setUserData() . These examples are extracted from open source projects. 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
/**
 * 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
/**
 * 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
@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
@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 5
Source Project: cxf   File: StaxUtils.java    License: 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 6
@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 7
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 8
Source Project: metafacture-core   File: Location.java    License: 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 Project: caja   File: Nodes.java    License: 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 10
private void setRebuild() {
    Node node = getNode();
    if (null != node) {
        node.setUserData(DATA_CATEGORY_OBJECT, new Object(), null);
    }
    _needToRebuild = true;
}
 
Example 11
/**
 * 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 Project: JsDroidCmd   File: XmlNode.java    License: 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 13
/**
 * 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 14
Source Project: buck   File: XmlUtils.java    License: 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 15
Source Project: cxf   File: JAXBDataBinding.java    License: 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 16
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 17
Source Project: cxf   File: StaxUtils.java    License: 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 18
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 19
Source Project: astor   File: XmlNode.java    License: 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 20
@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);
}