Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

In this page you can find the example usage for org.dom4j Element elements.

Prototype

List<Element> elements();

Source Link

Document

Returns the elements contained in this element.

Usage

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Deletes the specified property.//  ww w.j  a  v a 2s. c  o m
 *
 * @param name the property to delete.
 */
public synchronized void deleteProperty(String name) {
    // Remove property from cache.
    propertyCache.remove(name);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML hierarchy.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        // Can't find the property so return.
        if (element == null) {
            return;
        }
    }
    // Found the correct element to remove, so remove it...
    element.remove(element.element(propName[propName.length - 1]));
    if (element.elements().size() == 0) {
        element.getParent().remove(element);
    }
    // .. then write to disk.
    saveProperties();

}

From source file:com.jonschang.ai.network.feedforward.FeedForwardXmlUnmarshaller.java

License:LGPL

public void unmarshal(FeedForward obj, Reader xmlReader) throws XmlException {
    SAXReader reader = new SAXReader();
    Document doc = null;//from  w  w  w. ja  va 2s  .  co  m

    try {
        doc = reader.read(xmlReader);
    } catch (Exception e) {
        throw new XmlException("Could not read the Xml document", e);
    }

    Element root = doc.getRootElement();
    Element current = null;

    Map<Integer, Neuron> intToNeuronMap = new HashMap<Integer, Neuron>();
    Map<Integer, Activator> intToActivatorMap = new HashMap<Integer, Activator>();
    int neuronIndex = 0, activators = 0;
    Attribute attr = null;
    Neuron neuron = null;
    Neuron outputNeuron = null;

    obj.getAllLayers().clear();

    // build all the neurons and cache them in an
    // index-to-neuron map
    for (Object e : root.elements())
        if (e instanceof Element) {
            current = (Element) e;

            if (current.getName().compareTo("layers") == 0) {
                for (Object layerObject : current.elements())
                    if (((Element) layerObject).getName().compareTo("layer") == 0) {
                        List<Neuron> thisLayer = new ArrayList<Neuron>();
                        for (Object neuronObject : ((Element) layerObject).elements())
                            if (((Element) neuronObject).getName().compareTo("neuron") == 0) {
                                neuron = new GenericNeuron();

                                intToNeuronMap.put(neuronIndex, neuron);

                                attr = ((Element) neuronObject).attribute("threshold");
                                neuron.setThreshold(Double.valueOf(attr.getValue()));

                                thisLayer.add(neuron);

                                neuronIndex++;
                            }
                        obj.getAllLayers().add(thisLayer);
                    }
            } else if (current.getName().compareTo("activators") == 0 && current.elements().size() > 0) {
                for (Object a : current.elements())
                    if (a instanceof Element) {
                        Element activator = (Element) a;
                        ActivatorXmlFactory axf = new ActivatorXmlFactory();
                        Activator activatorObject = null;
                        String clazz = activator.attributeValue("type");
                        try {
                            activatorObject = (Activator) Class.forName(clazz).newInstance();
                            @SuppressWarnings(value = "unchecked")
                            XmlUnmarshaller<Activator> m = (XmlUnmarshaller<Activator>) axf
                                    .getUnmarshaller(activatorObject);
                            m.unmarshal(activatorObject, new StringReader(activator.asXML()));
                        } catch (Exception cnfe) {
                            throw new XmlException(cnfe);
                        }
                        intToActivatorMap.put(activators, activatorObject);
                        activators++;
                    }

            }
        }

    // now that we've built a cross-reference of index-to-neuron
    // we can process the synapses and easily reconstruct
    // the connections between neurons
    Integer inputIndex = 0, outputIndex, activatorIndex = 0;
    Double weight;
    for (Object e : root.elements())
        if (((Element) e).getName().compareTo("layers") == 0) {
            for (Object layerObject : current.elements())
                if (((Element) layerObject).getName().compareTo("layer") == 0) {
                    for (Object neuronObject : ((Element) layerObject).elements())
                        if (((Element) neuronObject).getName().compareTo("neuron") == 0) {
                            current = (Element) neuronObject;

                            neuron = intToNeuronMap.get(inputIndex);

                            // set the activator 
                            attr = current.attribute("activator-index");
                            activatorIndex = Integer.valueOf(attr.getValue());
                            neuron.setActivator(intToActivatorMap.get(activatorIndex));

                            // process the out-going synapses of the neuron
                            if (current.element("synapses") != null
                                    && current.element("synapses").element("synapse") != null) {
                                for (Object e2 : current.element("synapses").elements())
                                    if (e2 instanceof Element) {
                                        current = (Element) e2;

                                        // get the synapses output neuron
                                        attr = current.attribute("output");
                                        outputIndex = Integer.valueOf(attr.getValue());
                                        outputNeuron = intToNeuronMap.get(outputIndex);

                                        // set the weight of the synapse
                                        attr = current.attribute("weight");
                                        weight = Double.valueOf(attr.getValue());
                                        Synapse s = new GenericSynapse();
                                        neuron.setSynapseTo(s, outputNeuron);
                                        s.setWeight(weight);
                                    }
                            }
                            inputIndex++;
                        }
                }
        }

    obj.getInputNeurons().clear();
    obj.getOutputNeurons().clear();
    obj.getInputNeurons().addAll(obj.getAllLayers().get(0));
    obj.getOutputNeurons().addAll(obj.getAllLayers().get(obj.getAllLayers().size() - 1));
}

From source file:com.jswiff.xml.ActionXMLReader.java

License:Open Source License

private static ConstantPool readConstantPool(Element element) {
    List constantElements = element.elements();
    ConstantPool constantPool = new ConstantPool();
    List constants = constantPool.getConstants();
    for (Iterator it = constantElements.iterator(); it.hasNext();) {
        Element constantElement = (Element) it.next();
        String content = RecordXMLReader.getElement("value", constantElement).getText();
        String constant = (RecordXMLReader.getBooleanAttribute("base64", constantElement))
                ? Base64.decodeString(content)
                : content;//from   ww w  . jav a2s.  c om
        constants.add(constant);
    }
    return constantPool;
}

From source file:com.jswiff.xml.ActionXMLReader.java

License:Open Source License

private static Push readPush(Element element) {
    List valueElements = element.elements();
    Push push = new Push();
    for (Iterator it = valueElements.iterator(); it.hasNext();) {
        Element valueElement = (Element) it.next();
        String type = valueElement.getName();
        Push.StackValue value = new Push.StackValue();
        if (type.equals("boolean")) {
            value.setBoolean(RecordXMLReader.getBooleanAttribute("value", valueElement));
        } else if (type.equals("constant16")) {
            value.setConstant16(RecordXMLReader.getIntAttribute("id", valueElement));
        } else if (type.equals("constant8")) {
            value.setConstant8(RecordXMLReader.getShortAttribute("id", valueElement));
        } else if (type.equals("double")) {
            value.setDouble(RecordXMLReader.getDoubleAttribute("value", valueElement));
        } else if (type.equals("float")) {
            value.setFloat(RecordXMLReader.getFloatAttribute("value", valueElement));
        } else if (type.equals("integer")) {
            value.setInteger(RecordXMLReader.getIntAttribute("value", valueElement));
        } else if (type.equals("null")) {
            value.setNull();/*from w  w w .j  a  va  2 s. co m*/
        } else if (type.equals("register")) {
            value.setRegisterNumber(RecordXMLReader.getShortAttribute("number", valueElement));
        } else if (type.equals("string")) {
            value.setString(RecordXMLReader.getStringAttribute("value", valueElement));
        } else if (type.equals("undefined")) {
            value.setUndefined();
        } else {
            throw new IllegalArgumentException("Unexpected stack value type: " + type);
        }
        push.addValue(value);
    }
    return push;
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static void readActionBlock(ActionBlock actionBlock, Element parentElement) {
    Element blockElement = getElement("actionblock", parentElement);
    List actionElements = blockElement.elements();
    for (Iterator it = actionElements.iterator(); it.hasNext();) {
        Element actionElement = (Element) it.next();
        Action action = ActionXMLReader.readAction(actionElement);
        actionBlock.addAction(action);/* w  w  w .j a  v  a  2s  .c o  m*/
    }
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static ButtonCondAction[] readButtonCondActions(Element parentElement) {
    Element actionsElement = parentElement.element("actions");
    if (actionsElement != null) {
        List actions = actionsElement.elements();
        int arraySize = actions.size();
        ButtonCondAction[] actionArray = new ButtonCondAction[arraySize];
        for (int i = 0; i < arraySize; i++) {
            ButtonCondAction action = new ButtonCondAction();
            Element actionElement = (Element) actions.get(i);
            if (getBooleanAttribute("outdowntoidle", actionElement)) {
                action.setOutDownToIdle();
            }/*  ww w .  j  a va 2s.  c  om*/
            if (getBooleanAttribute("outdowntooverdown", actionElement)) {
                action.setOutDownToOverDown();
            }
            if (getBooleanAttribute("idletooverdown", actionElement)) {
                action.setIdleToOverDown();
            }
            if (getBooleanAttribute("idletooverup", actionElement)) {
                action.setIdleToOverUp();
            }
            if (getBooleanAttribute("overdowntoidle", actionElement)) {
                action.setOverDownToIdle();
            }
            if (getBooleanAttribute("overdowntooutdown", actionElement)) {
                action.setOverDownToOutDown();
            }
            if (getBooleanAttribute("overdowntooverup", actionElement)) {
                action.setOverDownToOverUp();
            }
            if (getBooleanAttribute("overuptoidle", actionElement)) {
                action.setOverUpToIdle();
            }
            if (getBooleanAttribute("overuptooverdown", actionElement)) {
                action.setOverUpToOverDown();
            }
            Attribute keyPress = actionElement.attribute("keypress");
            if (keyPress != null) {
                action.setKeyPress(Byte.parseByte(keyPress.getValue()));
            }
            readActionBlock(action.getActions(), actionElement);
            actionArray[i] = action;
        }
        return actionArray;
    }
    return null;
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static ButtonRecord[] readButtonRecords(Element parentElement) {
    Element charsElement = getElement("chars", parentElement);
    List recordElements = charsElement.elements();
    int arrayLength = recordElements.size();
    ButtonRecord[] records = new ButtonRecord[arrayLength];
    for (int i = 0; i < arrayLength; i++) {
        Element recordElement = (Element) recordElements.get(i);
        int charId = getIntAttribute("charid", recordElement);
        int depth = getIntAttribute("depth", recordElement);
        Element state = getElement("state", recordElement);
        boolean up = getBooleanAttribute("up", state);
        boolean down = getBooleanAttribute("down", state);
        boolean over = getBooleanAttribute("over", state);
        boolean hit = getBooleanAttribute("hit", state);
        Matrix placeMatrix = readMatrix("matrix", recordElement);
        ButtonRecord record = new ButtonRecord(charId, depth, placeMatrix, up, over, down, hit);
        Element colorTransform = recordElement.element("cxformwithalpha");
        if (colorTransform != null) {
            record.setColorTransform(readCXformWithAlpha(colorTransform));
        }// w w w .ja v  a 2 s. c o m
        records[i] = record;
    }
    return records;
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static List readFilters(Element element) {
    List filterElements = element.elements();
    int filterCount = filterElements.size();
    List filters = new ArrayList(filterCount);
    for (int j = 0; j < filterCount; j++) {
        Element filterElement = (Element) filterElements.get(j);
        if (filterElement.getName().equals("colormatrix")) {
            List valElements = filterElement.elements("val");
            int valCount = valElements.size();
            if (valCount != 20) {
                throw new MalformedElementException("colormatrix must contain 20 values!");
            }/*w  ww  .  ja v  a 2s  .  c om*/
            float[] matrix = new float[20];
            for (int i = 0; i < 20; i++) {
                Element valElement = (Element) valElements.get(i);
                matrix[i] = Float.parseFloat(valElement.getText());
            }
            ColorMatrixFilter colorMatrixFilter = new ColorMatrixFilter(matrix);
            filters.add(colorMatrixFilter);
        } else if (filterElement.getName().equals("convolution")) {
            Element matrixElement = filterElement.element("matrix");
            List valElements = matrixElement.elements("val");
            float[] matrix = new float[valElements.size()];
            for (int i = 0; i < matrix.length; i++) {
                Element valElement = (Element) valElements.get(i);
                matrix[i] = Float.parseFloat(valElement.getText());
            }
            int matrixRows = getIntAttribute("rows", matrixElement);
            ConvolutionFilter convolutionFilter = new ConvolutionFilter(matrix, matrixRows);
            convolutionFilter.setColor(readRGBA(getElement("color", filterElement)));
            convolutionFilter.setDivisor(getFloatAttribute("divisor", filterElement));
            convolutionFilter.setBias(getFloatAttribute("bias", filterElement));
            convolutionFilter.setClamp(getBooleanAttribute("clamp", filterElement));
            convolutionFilter.setPreserveAlpha(getBooleanAttribute("preservealpha", filterElement));
            filters.add(convolutionFilter);
        } else if (filterElement.getName().equals("blur")) {
            BlurFilter blurFilter = new BlurFilter(getDoubleAttribute("x", filterElement),
                    getDoubleAttribute("y", filterElement));
            blurFilter.setQuality(getIntAttribute("quality", filterElement));
            filters.add(blurFilter);
        } else if (filterElement.getName().equals("dropshadow")) {
            DropShadowFilter dropShadowFilter = new DropShadowFilter();
            dropShadowFilter.setColor(readRGBA(getElement("color", filterElement)));
            dropShadowFilter.setX(getDoubleAttribute("x", filterElement));
            dropShadowFilter.setY(getDoubleAttribute("y", filterElement));
            dropShadowFilter.setAngle(getDoubleAttribute("angle", filterElement));
            dropShadowFilter.setDistance(getDoubleAttribute("distance", filterElement));
            dropShadowFilter.setStrength(getDoubleAttribute("strength", filterElement));
            dropShadowFilter.setQuality(getIntAttribute("quality", filterElement));
            dropShadowFilter.setInner(getBooleanAttribute("inner", filterElement));
            dropShadowFilter.setKnockout(getBooleanAttribute("knockout", filterElement));
            dropShadowFilter.setHideObject(getBooleanAttribute("hideobject", filterElement));
            filters.add(dropShadowFilter);
        } else if (filterElement.getName().equals("glow")) {
            GlowFilter glowFilter = new GlowFilter();
            glowFilter.setColor(readRGBA(getElement("color", filterElement)));
            glowFilter.setX(getDoubleAttribute("x", filterElement));
            glowFilter.setY(getDoubleAttribute("y", filterElement));
            glowFilter.setStrength(getDoubleAttribute("strength", filterElement));
            glowFilter.setQuality(getIntAttribute("quality", filterElement));
            glowFilter.setInner(getBooleanAttribute("inner", filterElement));
            glowFilter.setKnockout(getBooleanAttribute("knockout", filterElement));
            filters.add(glowFilter);
        } else if (filterElement.getName().equals("bevel")) {
            BevelFilter bevelFilter = new BevelFilter();
            bevelFilter.setHighlightColor(readRGBA(getElement("highlightcolor", filterElement)));
            bevelFilter.setShadowColor(readRGBA(getElement("shadowcolor", filterElement)));
            bevelFilter.setX(getDoubleAttribute("x", filterElement));
            bevelFilter.setY(getDoubleAttribute("y", filterElement));
            bevelFilter.setAngle(getDoubleAttribute("angle", filterElement));
            bevelFilter.setDistance(getDoubleAttribute("distance", filterElement));
            bevelFilter.setStrength(getDoubleAttribute("strength", filterElement));
            bevelFilter.setQuality(getIntAttribute("quality", filterElement));
            bevelFilter.setInner(getBooleanAttribute("inner", filterElement));
            bevelFilter.setKnockout(getBooleanAttribute("knockout", filterElement));
            bevelFilter.setOnTop(getBooleanAttribute("ontop", filterElement));
            filters.add(bevelFilter);
        } else if (filterElement.getName().equals("gradientglow")) {
            Element pointsElement = getElement("controlpoints", filterElement);
            List pointElements = pointsElement.elements("controlpoint");
            int controlPointsCount = pointElements.size();
            RGBA[] colors = new RGBA[controlPointsCount];
            short[] ratios = new short[controlPointsCount];
            for (int i = 0; i < controlPointsCount; i++) {
                Element pointElement = (Element) pointElements.get(i);
                colors[i] = readRGBA(getElement("color", pointElement));
                ratios[i] = getShortAttribute("ratio", pointElement);
            }
            GradientGlowFilter gradientGlowFilter = new GradientGlowFilter(colors, ratios);
            gradientGlowFilter.setX(getDoubleAttribute("x", filterElement));
            gradientGlowFilter.setY(getDoubleAttribute("y", filterElement));
            gradientGlowFilter.setAngle(getDoubleAttribute("angle", filterElement));
            gradientGlowFilter.setDistance(getDoubleAttribute("distance", filterElement));
            gradientGlowFilter.setStrength(getDoubleAttribute("strength", filterElement));
            gradientGlowFilter.setQuality(getIntAttribute("quality", filterElement));
            gradientGlowFilter.setInner(getBooleanAttribute("inner", filterElement));
            gradientGlowFilter.setKnockout(getBooleanAttribute("knockout", filterElement));
            gradientGlowFilter.setOnTop(getBooleanAttribute("ontop", filterElement));
            filters.add(gradientGlowFilter);
        } else if (filterElement.getName().equals("gradientbevel")) {
            Element pointsElement = getElement("controlpoints", filterElement);
            List pointElements = pointsElement.elements("controlpoint");
            int controlPointsCount = pointElements.size();
            RGBA[] colors = new RGBA[controlPointsCount];
            short[] ratios = new short[controlPointsCount];
            for (int i = 0; i < controlPointsCount; i++) {
                Element pointElement = (Element) pointElements.get(i);
                colors[i] = readRGBA(getElement("color", pointElement));
                ratios[i] = getShortAttribute("ratio", pointElement);
            }
            GradientBevelFilter gradientBevelFilter = new GradientBevelFilter(colors, ratios);
            gradientBevelFilter.setX(getDoubleAttribute("x", filterElement));
            gradientBevelFilter.setY(getDoubleAttribute("y", filterElement));
            gradientBevelFilter.setAngle(getDoubleAttribute("angle", filterElement));
            gradientBevelFilter.setDistance(getDoubleAttribute("distance", filterElement));
            gradientBevelFilter.setStrength(getDoubleAttribute("strength", filterElement));
            gradientBevelFilter.setQuality(getIntAttribute("quality", filterElement));
            gradientBevelFilter.setInner(getBooleanAttribute("inner", filterElement));
            gradientBevelFilter.setKnockout(getBooleanAttribute("knockout", filterElement));
            gradientBevelFilter.setOnTop(getBooleanAttribute("ontop", filterElement));
            filters.add(gradientBevelFilter);
        }
    }
    return filters;
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static MorphFillStyles readMorphFillStyles(Element parentElement) {
    Element element = getElement("morphfillstyles", parentElement);
    List styleElements = element.elements();
    MorphFillStyles styles = new MorphFillStyles();
    for (Iterator it = styleElements.iterator(); it.hasNext();) {
        Element styleElement = (Element) it.next();
        styles.addStyle(readMorphFillStyle(styleElement));
    }//from w w w . jav  a2 s  .  c o  m
    return styles;
}

From source file:com.jswiff.xml.RecordXMLReader.java

License:Open Source License

static MorphLineStyles readMorphLineStyles(Element parentElement) {
    Element element = getElement("morphlinestyles", parentElement);
    MorphLineStyles styles = new MorphLineStyles();
    List styleElements = element.elements();
    for (Iterator it = styleElements.iterator(); it.hasNext();) {
        Element styleElement = (Element) it.next();
        if (styleElement.getName().equals("morphlinestyle")) {
            styles.addStyle(readMorphLineStyle(styleElement));
        } else if (styleElement.getName().equals("morphlinestyle2")) {
            styles.addStyle(readMorphLineStyle2(styleElement));
        }//ww w  .j  av a2  s  . c o m
    }
    return styles;
}