Search in sources :

Example 56 with Element

use of org.w3c.dom.Element in project jmonkeyengine by jMonkeyEngine.

the class DOMOutputCapsule method write.

@Override
public void write(Savable object, String name, Savable defVal) throws IOException {
    if (object == null) {
        return;
    }
    if (object.equals(defVal)) {
        return;
    }
    Element old = currentElement;
    Element el = writtenSavables.get(object);
    String className = null;
    if (!object.getClass().getName().equals(name)) {
        className = object.getClass().getName();
    }
    try {
        doc.createElement(name);
    } catch (DOMException e) {
        // Ridiculous fallback behavior.
        // Would be far better to throw than to totally disregard the
        // specified "name" and write a class name instead!
        // (Besides the fact we are clobbering the managed .getClassTag()).
        name = "Object";
        className = object.getClass().getName();
    }
    if (el != null) {
        String refID = el.getAttribute("reference_ID");
        if (refID.length() == 0) {
            refID = object.getClass().getName() + "@" + object.hashCode();
            el.setAttribute("reference_ID", refID);
        }
        el = appendElement(name);
        el.setAttribute("ref", refID);
    } else {
        el = appendElement(name);
        // jME3 NEW: Append version number(s)
        int[] versions = SavableClassUtil.getSavableVersions(object.getClass());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < versions.length; i++) {
            sb.append(versions[i]);
            if (i != versions.length - 1) {
                sb.append(", ");
            }
        }
        el.setAttribute("savable_versions", sb.toString());
        writtenSavables.put(object, el);
        object.write(exporter);
    }
    if (className != null) {
        el.setAttribute("class", className);
    }
    currentElement = old;
}
Also used : DOMException(org.w3c.dom.DOMException) Element(org.w3c.dom.Element)

Example 57 with Element

use of org.w3c.dom.Element in project jmonkeyengine by jMonkeyEngine.

the class DOMOutputCapsule method write.

@Override
public void write(double[][] value, String name, double[][] defVal) throws IOException {
    if (value == null)
        return;
    if (Arrays.deepEquals(value, defVal))
        return;
    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.length));
    for (int i = 0; i < value.length; i++) {
        double[] array = value[i];
        write(array, "array_" + i, defVal == null ? null : defVal[i]);
    }
    currentElement = (Element) el.getParentNode();
}
Also used : Element(org.w3c.dom.Element)

Example 58 with Element

use of org.w3c.dom.Element in project jmonkeyengine by jMonkeyEngine.

the class DOMOutputCapsule method write.

@Override
public void write(IntBuffer value, String name, IntBuffer defVal) throws IOException {
    if (value == null) {
        return;
    }
    if (value.equals(defVal)) {
        return;
    }
    Element el = appendElement(name);
    el.setAttribute("size", String.valueOf(value.limit()));
    StringBuilder buf = new StringBuilder();
    int pos = value.position();
    value.rewind();
    int ctr = 0;
    while (value.hasRemaining()) {
        ctr++;
        buf.append(value.get());
        buf.append(" ");
    }
    if (ctr != value.limit()) {
        throw new IOException("'" + name + "' buffer contention resulted in write data consistency.  " + ctr + " values written when should have written " + value.limit());
    }
    if (buf.length() > 0) {
        //remove last space
        buf.setLength(buf.length() - 1);
    }
    value.position(pos);
    el.setAttribute(dataAttributeName, buf.toString());
    currentElement = (Element) el.getParentNode();
}
Also used : Element(org.w3c.dom.Element) IOException(java.io.IOException)

Example 59 with Element

use of org.w3c.dom.Element in project hadoop by apache.

the class QueueConfigurationParser method loadResource.

/**
   * Method to load the resource file.
   * generates the root.
   * 
   * @param resourceInput InputStream that provides the XML to parse
   * @return
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws IOException
   */
protected Queue loadResource(InputStream resourceInput) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    //ignore all comments inside the xml file
    docBuilderFactory.setIgnoringComments(true);
    //allow includes in the xml file
    docBuilderFactory.setNamespaceAware(true);
    try {
        docBuilderFactory.setXIncludeAware(true);
    } catch (UnsupportedOperationException e) {
        LOG.info("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + NAME_SEPARATOR + e);
    }
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document doc = null;
    Element queuesNode = null;
    doc = builder.parse(resourceInput);
    queuesNode = doc.getDocumentElement();
    return this.parseResource(queuesNode);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 60 with Element

use of org.w3c.dom.Element in project hadoop by apache.

the class QueueConfigurationParser method getQueueElement.

/**
   * Construct an {@link Element} for a single queue, constructing the inner
   * queue &lt;name/&gt;, &lt;properties/&gt;, &lt;state/&gt; and the inner
   * &lt;queue&gt; elements recursively.
   * 
   * @param document
   * @param jqi
   * @return
   */
static Element getQueueElement(Document document, JobQueueInfo jqi) {
    // Queue
    Element q = document.createElement(QUEUE_TAG);
    // Queue-name
    Element qName = document.createElement(QUEUE_NAME_TAG);
    qName.setTextContent(getSimpleQueueName(jqi.getQueueName()));
    q.appendChild(qName);
    // Queue-properties
    Properties props = jqi.getProperties();
    Element propsElement = document.createElement(PROPERTIES_TAG);
    if (props != null) {
        Set<String> propList = props.stringPropertyNames();
        for (String prop : propList) {
            Element propertyElement = document.createElement(PROPERTY_TAG);
            propertyElement.setAttribute(KEY_TAG, prop);
            propertyElement.setAttribute(VALUE_TAG, (String) props.get(prop));
            propsElement.appendChild(propertyElement);
        }
    }
    q.appendChild(propsElement);
    // Queue-state
    String queueState = jqi.getState().getStateName();
    if (queueState != null && !queueState.equals(QueueState.UNDEFINED.getStateName())) {
        Element qStateElement = document.createElement(STATE_TAG);
        qStateElement.setTextContent(queueState);
        q.appendChild(qStateElement);
    }
    // Queue-children
    List<JobQueueInfo> children = jqi.getChildren();
    if (children != null) {
        for (JobQueueInfo child : children) {
            q.appendChild(getQueueElement(document, child));
        }
    }
    return q;
}
Also used : Element(org.w3c.dom.Element) Properties(java.util.Properties)

Aggregations

Element (org.w3c.dom.Element)9072 Document (org.w3c.dom.Document)2651 NodeList (org.w3c.dom.NodeList)2103 Node (org.w3c.dom.Node)1855 ArrayList (java.util.ArrayList)957 DocumentBuilder (javax.xml.parsers.DocumentBuilder)793 IOException (java.io.IOException)732 Test (org.junit.Test)693 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)591 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)489 HashMap (java.util.HashMap)434 SAXException (org.xml.sax.SAXException)406 File (java.io.File)358 Attr (org.w3c.dom.Attr)333 InputStream (java.io.InputStream)309 QName (javax.xml.namespace.QName)292 Map (java.util.Map)285 JAXBElement (javax.xml.bind.JAXBElement)285 NamedNodeMap (org.w3c.dom.NamedNodeMap)266 List (java.util.List)264