Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

In this page you can find the example usage for org.dom4j Node selectSingleNode.

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:threeLayerNetwork.ThreeLayerNetwork.java

License:Open Source License

@Override
public AbstractRepresentation loadFromXML(Node nd) {
    try {//from   www  .  ja v a 2  s. com
        String fitnessString = nd.valueOf("./@fitness");
        if (!fitnessString.isEmpty()) {
            this.setFitness(Double.parseDouble(fitnessString));
        }

        this.input_nodes = Integer.parseInt(nd.valueOf("./@input_nodes"));
        this.output_nodes = Integer.parseInt(nd.valueOf("./@output_nodes"));
        this.nodes = Integer.parseInt(nd.valueOf("./@nodes"));
        this.weight_range = Float.parseFloat(nd.valueOf("./@weight_range"));
        this.bias_range = Float.parseFloat(nd.valueOf("./@bias_range"));
        // this.iteration =
        // Integer.parseInt(nd.valueOf("./simulation/@iteration"));
        // this.score = Integer.parseInt(nd.valueOf("./simulation/@score"));
        this.activation = new float[this.nodes];
        this.output = new float[this.nodes];
        this.bias = new float[this.nodes];
        this.weight = new float[this.nodes][this.nodes];
        Node dnodes = nd.selectSingleNode("./nodes");
        for (int nr = this.input_nodes; nr < this.nodes; nr++) {
            Node curnode = dnodes.selectSingleNode("./node[@nr='" + nr + "']");
            if (curnode == null)
                throw new IllegalArgumentException("ThreeLayerNetwork: node tags inconsistent!"
                        + "\ncheck 'nr' attributes and nodes count in nnetwork!");
            this.bias[nr] = Float.parseFloat(curnode.valueOf("./bias"));
            Node dweights = curnode.selectSingleNode("./weights");
            for (int from = 0; from < this.nodes; from++) {
                String ws = dweights.valueOf("./weight[@from='" + from + "']");
                if (ws.length() == 0)
                    throw new IllegalArgumentException("ThreeLayerNetwork: weight tags inconsistent!"
                            + "\ncheck 'from' attributes and nodes count in nnetwork!");
                float val = Float.parseFloat(ws);
                this.weight[from][nr] = val;
            }
        }
        // this.gahist = new GAHistory(this.config, nd);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("ThreeLayerNetwork: NumberFormatException! Check XML File");
    }
    return this;
}

From source file:us.wthr.jdem846.project.XmlProjectFileReader.java

License:Apache License

@SuppressWarnings("rawtypes")
public static ProjectModel parseProject(Document doc) throws ProjectParseException {
    ProjectModel projectModel = new ProjectModel();

    Node projectTypeNode = doc.selectSingleNode("//jdem846/project/type");
    if (projectTypeNode != null) {
        ProjectTypeEnum projectType = ProjectTypeEnum.getProjectTypeFromIdentifier(projectTypeNode.getText());
        projectModel.setProjectType(projectType);
    }/* ww  w  . ja  va 2s.c om*/

    List optionsList = doc.selectNodes("//jdem846/project/options/option");
    for (Iterator iter = optionsList.iterator(); iter.hasNext();) {
        Node inputNode = (Node) iter.next();
        Node keyAttribute = inputNode.selectSingleNode("@key");
        log.info("Key: " + keyAttribute.getText() + ", value: " + inputNode.getText());

        String key = keyAttribute.getText();
        String value = inputNode.getText();

        if (key != null && value != null) {
            projectModel.setOption(key, value);
        } else if (key == null) {
            log.warn("Option has no key!");
        }
        //projectModel.getInputFiles().add(inputNode.getText());
    }

    List elevationList = doc.selectNodes("//jdem846/project/input-files/input");
    for (Iterator iter = elevationList.iterator(); iter.hasNext();) {
        Node inputNode = (Node) iter.next();
        projectModel.getInputFiles().add(inputNode.getText());
    }

    Node scriptLanguageNode = doc.selectSingleNode("//jdem846/project/scripting/language");
    Node scriptContentNode = doc.selectSingleNode("//jdem846/project/scripting/script");

    if (scriptLanguageNode != null) {
        projectModel.setScriptLanguage(scriptLanguageNode.getText());
    }

    if (scriptContentNode != null) {
        projectModel.setUserScript(scriptContentNode.getText());
    }

    List shapeList = doc.selectNodes("//jdem846/project/input-files/shapefile");
    for (Iterator iter = shapeList.iterator(); iter.hasNext();) {
        Node shapeNode = (Node) iter.next();

        Node pathNode = (Node) shapeNode.selectSingleNode("path");
        Node shapeDataDefinitionIdNode = (Node) shapeNode.selectSingleNode("data-definition-id");

        String path = pathNode.getText();
        String shapeDataDefinitionId = null;
        if (shapeDataDefinitionIdNode != null) {
            shapeDataDefinitionId = shapeDataDefinitionIdNode.getText();
        }

        try {
            projectModel.getShapeFiles().add(new ShapeFileReference(path, shapeDataDefinitionId, false));
        } catch (ShapeFileException ex) {
            throw new ProjectParseException("Failed to load shapefile data when parsing project file", ex);
        }

    }

    return projectModel;
}

From source file:us.wthr.jdem846.shapefile.modeling.FeatureTypesDefinitionLoader.java

License:Apache License

public static List<FeatureTypesDefinition> loadFeatureTypesDefinition(
        FeatureTypeStrokeLoader featureTypeStrokeLoader) throws Exception {
    List<FeatureTypesDefinition> featureTypesDefinitions = new LinkedList<FeatureTypesDefinition>();

    log.info("Loading feature type definitions from " + DEFAULT_XML_FILE);
    Document doc = loadDocument(DEFAULT_XML_FILE);

    FeatureTypesDefinition featureTypeDefinition = null;
    //Node node = null;

    List<?> featureTypeDefinitionList = doc.selectNodes("//jdem846/feature-types/feature-type-definition");
    for (Iterator<?> iter = featureTypeDefinitionList.iterator(); iter.hasNext();) {
        Node featureTypeDefinitionNode = (Node) iter.next();
        Node nameAttribute = featureTypeDefinitionNode.selectSingleNode("@name");
        Node idAttribute = featureTypeDefinitionNode.selectSingleNode("@id");

        featureTypeDefinition = new FeatureTypesDefinition(nameAttribute.getText(), idAttribute.getText());

        Node defaultStrokeNode = (Node) featureTypeDefinitionNode.selectSingleNode("default-stroke");
        if (defaultStrokeNode != null) {
            featureTypeDefinition.setDefaultStroke(
                    featureTypeStrokeLoader.getFeatureTypeStroke(defaultStrokeNode.getText()));
        }// w  w  w  .j  a v a  2s.  c  om

        List<?> featureTypeGroupList = featureTypeDefinitionNode.selectNodes("type-groups/type-group");
        for (Iterator<?> featureTypeGroupIter = featureTypeGroupList.iterator(); featureTypeGroupIter
                .hasNext();) {
            Node featureTypeGroupNode = (Node) featureTypeGroupIter.next();

            Node featureTypeGroupNameAttribute = featureTypeGroupNode.selectSingleNode("@name");
            Node featureTypeGroupIdAttribute = featureTypeGroupNode.selectSingleNode("@id");
            Node featureTypeGroupDefaultStrokeAttribute = featureTypeGroupNode
                    .selectSingleNode("@default-stroke");

            FeatureTypeStroke featureTypeStroke = featureTypeStrokeLoader
                    .getFeatureTypeStroke(featureTypeGroupDefaultStrokeAttribute.getText());

            FeatureTypeGroup featureTypeGroup = new FeatureTypeGroup(featureTypeGroupNameAttribute.getText(),
                    featureTypeGroupIdAttribute.getText(), featureTypeStroke);

            featureTypeDefinition.addFeatureTypeGroup(featureTypeGroup);
        }

        List<?> featureTypeList = featureTypeDefinitionNode.selectNodes("type");
        for (Iterator<?> featureTypeIter = featureTypeList.iterator(); featureTypeIter.hasNext();) {
            Node featureTypeNode = (Node) featureTypeIter.next();

            Node featureTypeCodeAttribute = featureTypeNode.selectSingleNode("@code");
            Node featureTypeGroupIdAttribute = featureTypeNode.selectSingleNode("@group");
            Node featureTypeStrokeAttribute = featureTypeNode.selectSingleNode("@stroke");
            String definition = featureTypeNode.getText();

            FeatureTypeGroup featureTypeGroup = featureTypeDefinition
                    .getFeatureTypeGroup(featureTypeGroupIdAttribute.getText());

            FeatureTypeStroke featureTypeStroke = null;
            if (featureTypeStrokeAttribute != null) {
                featureTypeStroke = featureTypeStrokeLoader
                        .getFeatureTypeStroke(featureTypeStrokeAttribute.getText());
            } else {
                featureTypeStroke = featureTypeGroup.getDefaultStroke();
            }

            // TODO: Get FeatureTypeStroke

            FeatureType featureType = new FeatureType(featureTypeCodeAttribute.getText(), featureTypeGroup,
                    definition, featureTypeStroke);
            featureTypeDefinition.addFeatureType(featureType);

        }

        featureTypesDefinitions.add(featureTypeDefinition);
    }

    return featureTypesDefinitions;
}

From source file:us.wthr.jdem846.shapefile.modeling.FeatureTypeStrokeLoader.java

License:Apache License

public static List<FeatureTypeStroke> loadFeatureTypeStrokes() throws Exception {
    List<FeatureTypeStroke> featureTypeStrokes = new LinkedList<FeatureTypeStroke>();

    log.info("Loading feature type line strokes from " + DEFAULT_XML_FILE);
    Document doc = loadDocument(DEFAULT_XML_FILE);

    FeatureTypeStroke featureTypeStroke = null;

    List<?> lineStrokeList = doc.selectNodes("//jdem846/line-strokes/line-stroke");
    for (Iterator<?> iter = lineStrokeList.iterator(); iter.hasNext();) {
        Node lineStrokeNode = (Node) iter.next();
        Node nameAttribute = lineStrokeNode.selectSingleNode("@name");

        featureTypeStroke = new FeatureTypeStroke(nameAttribute.getText());

        List<?> strokeList = lineStrokeNode.selectNodes("stroke");

        for (Iterator<?> strokeIter = strokeList.iterator(); strokeIter.hasNext();) {
            Node strokeNode = (Node) strokeIter.next();

            float width = 1.0f;
            int cap = BasicStroke.CAP_SQUARE;
            int join = BasicStroke.JOIN_ROUND;
            float miterLimit = 1.0f;
            float[] dash = null;
            float dashPhase = 0.0f;
            int red = 0;
            int green = 0;
            int blue = 0;
            int alpha = 255;

            Node widthNode = strokeNode.selectSingleNode("@width");
            if (widthNode != null)
                width = Float.parseFloat(widthNode.getText());

            Node capNode = strokeNode.selectSingleNode("@cap");
            if (capNode != null)
                cap = getStrokeConstant(capNode.getText());

            Node joinNode = strokeNode.selectSingleNode("@join");
            if (joinNode != null)
                join = getStrokeConstant(joinNode.getText());

            Node miterLimitNode = strokeNode.selectSingleNode("@miterLimit");
            if (miterLimitNode != null)
                miterLimit = Float.parseFloat(miterLimitNode.getText());

            Node dashNode = strokeNode.selectSingleNode("@dash");
            if (dashNode != null) {
                String[] split = dashNode.getText().split(",");
                dash = new float[split.length];

                for (int i = 0; i < split.length; i++) {
                    dash[i] = Float.parseFloat(split[i]);
                }//from w  ww  . j  a v  a  2s  . c o m
            }

            Node dashPhaseNode = strokeNode.selectSingleNode("@dashPhase");
            if (dashPhaseNode != null)
                dashPhase = Float.parseFloat(dashPhaseNode.getText());

            Node redNode = strokeNode.selectSingleNode("@red");
            if (redNode != null)
                red = Integer.parseInt(redNode.getText());

            Node greenNode = strokeNode.selectSingleNode("@green");
            if (greenNode != null)
                green = Integer.parseInt(greenNode.getText());

            Node blueNode = strokeNode.selectSingleNode("@blue");
            if (blueNode != null)
                blue = Integer.parseInt(blueNode.getText());

            Node alphaNode = strokeNode.selectSingleNode("@alpha");
            if (alphaNode != null)
                alpha = Integer.parseInt(alphaNode.getText());

            Color lineColor = new Color(red, green, blue, alpha);
            LineStroke lineStroke = new LineStroke(width, cap, join, miterLimit, dash, dashPhase, lineColor);
            featureTypeStroke.addLineStroke(lineStroke);

        }

        featureTypeStrokes.add(featureTypeStroke);

    }

    return featureTypeStrokes;

}

From source file:us.wthr.jdem846.shapefile.modeling.ShapeDataDefinitionLoader.java

License:Apache License

public static List<ShapeDataDefinition> loadShapeDataDefinition() throws Exception {
    List<ShapeDataDefinition> shapeDataDefinitions = new LinkedList<ShapeDataDefinition>();

    log.info("Loading shape data definitions from " + DEFAULT_XML_FILE);
    Document doc = loadDocument(DEFAULT_XML_FILE);

    ShapeDataDefinition shapeDataDefinition = null;

    List<?> shapeDataDefinitionList = doc.selectNodes("//jdem846/shape-types/shape-type");
    for (Iterator<?> iter = shapeDataDefinitionList.iterator(); iter.hasNext();) {
        Node shapeDataDefinitionNode = (Node) iter.next();
        Node nameAttribute = shapeDataDefinitionNode.selectSingleNode("@name");
        Node idAttribute = shapeDataDefinitionNode.selectSingleNode("@id");

        shapeDataDefinition = new ShapeDataDefinition(nameAttribute.getText(), idAttribute.getText());

        Node featureTypeColumnNode = shapeDataDefinitionNode.selectSingleNode("featureTypeColumn");
        if (featureTypeColumnNode != null) {

            Node featureTypeDefinitionIdNode = featureTypeColumnNode.selectSingleNode("@definition");
            String featureTypeColumn = featureTypeColumnNode.getText();

            shapeDataDefinition.setFeatureTypeDefinitionId(featureTypeDefinitionIdNode.getText());
            shapeDataDefinition.setFeatureTypeColumn(featureTypeColumn);
        }//from w ww . ja va2 s . c  o m

        List<?> columnList = shapeDataDefinitionNode.selectNodes("columns/column");
        for (Iterator<?> columnIter = columnList.iterator(); columnIter.hasNext();) {
            Node columnNode = (Node) columnIter.next();
            Node columnNameNode = columnNode.selectSingleNode("@name");

            shapeDataDefinition.addColumn(columnNameNode.getText(), columnNode.getText());
        }

        shapeDataDefinitions.add(shapeDataDefinition);

    }

    return shapeDataDefinitions;
}