Example usage for org.dom4j Node valueOf

List of usage examples for org.dom4j Node valueOf

Introduction

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

Prototype

String valueOf(String xpathExpression);

Source Link

Document

valueOf evaluates an XPath expression and returns the textual representation of the results the XPath string-value of this node.

Usage

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * {@inheritDoc}. <br>/* w ww  . ja v a2  s . c o  m*/
 * The example bellow shows how to get an instance of {@link Schema} from an existing catalog : <br>
 * <pre>
 *
 *     {@code
 *          CatalogParser parser = new Dom4jBasedCatalogParser("my-data-warehouse-mapping.xml");
 *          // can use another implementation
 *          try {
 *              Schema schema = parser.parseCatalog();
 *              // use schema to reach cubes, dimensions, hierarchies, levels ...
 *          } catch(Rolap4jException eX) {
 *              eX.printStackTrace();
 *          }
 *     }
 * </pre>
 *
 * @return A {@link Schema} corresponding to the filePath
 * @throws FileNotFoundException The specified file was not found
 * @throws ParsingException      An error occurred when parsing the file
 */
@Override
public Schema parseCatalog() throws FileNotFoundException, ParsingException {

    final Document document = createDocument();

    Node schemaNode = document.selectSingleNode("//Schema");
    if (null == schemaNode) {
        throw new ParsingException("The root element (Schema) was not found.");
    }

    final String schemaName = schemaNode.valueOf("@name");
    final Schema schema = new Schema();
    schema.setName(StringUtil.isEmpty(schemaName) ? Schema.DEFAULT_NAME : schemaName);

    // Loading dimensions ...
    schema.addDimensions(loadDimensions(document));

    // Loading cubes ...
    schema.addCubes(loadCubes(document, schema));

    return schema;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * Get all cubes mapped in the data-warehouse mapping file
 *
 * @param document An instance of {@link Document}
 * @return/*from   w  w w .j  a  v a2 s  .c om*/
 * @see Cube
 */
private List<Cube> loadCubes(final Document document, Schema schema) throws ParsingException {

    log.debug("Loading all cubes from the schema ...");
    List<Cube> cubes = new ArrayList<>();

    List<Node> cubeNodes = document.selectNodes("//Schema/Cube");
    Set<String> notAvailableCubeNames = new HashSet<>();
    String worker;
    int sprinter = 1;
    for (Node cubeNode : cubeNodes) {

        // Set the cube name
        worker = cubeNode.valueOf("@name");
        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("The cube number " + sprinter + " must have a name attribute defined");
        }
        checkCubeAvailability(worker, notAvailableCubeNames);
        final Cube cube = new Cube();
        cube.setName(worker);

        // Set the table name
        cube.setTableName(getCubeTableName(cubeNode, cube.getName()));

        // Set dimensions
        cube.addDimensions(getDimensions(cubeNode, schema));

        // Set measures
        cube.addMeasures(getMeasures(cubeNode, schema));

        cubes.add(cube);

        log.debug("\t{} cube was found", cube.getName());
        sprinter++;
    }
    return cubes;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * @param cubeNode//  w w w .j a  v  a  2 s.  c om
 * @param schema
 * @return
 * @throws ParsingException
 */
private Collection<Measure> getMeasures(Node cubeNode, Schema schema) throws ParsingException {

    List<Measure> measures = new ArrayList<>();
    List<Node> measureNodes = cubeNode.selectNodes("Measure");
    String worker;
    for (Node measureNode : measureNodes) {

        worker = measureNode.valueOf("@name");
        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("The name of the measure attribute must be specified");
        }
        Measure measure = new Measure();
        measure.setName(worker);
        measures.add(measure);
    }
    return measures;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * @param cubeNode//from   www.j a  v  a 2 s  .  com
 * @param schema
 * @return
 * @throws ParsingException
 */
private Collection<Dimension> getDimensions(Node cubeNode, final Schema schema) throws ParsingException {

    List<Dimension> dimensions = new ArrayList<>();
    List<Node> dimensionNodes = cubeNode.selectNodes("DimensionUsage");
    String worker;

    for (Node dimensionNode : dimensionNodes) {

        worker = dimensionNode.valueOf("@name");
        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("The name of the dimensionUsage attribute must be specified");
        }

        if (!schema.containsDimension(worker)) {
            StringBuilder names = new StringBuilder();
            for (Iterator<String> iter = schema.getDimensions().keySet().iterator(); iter.hasNext();) {
                names.append(iter.next()).append(", ");
            }
            if (names.length() > 0) {
                names.setLength(names.length() - 1);
            }
            throw new ParsingException("Unexpected dimension found : " + worker
                    + " when one of the following dimensions is expected : " + names);

        }
        dimensions.add(schema.getDimension(worker));
    }
    return dimensions;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * Get the table name that corresponds to the cube
 *
 * @param cubeNode/*from  w  w  w.  j  a  v a 2 s  . c  o m*/
 * @param cubeName
 * @return table name that corresponds to the cube
 * @throws ParsingException
 * @see Cube
 */
private String getCubeTableName(final Node cubeNode, final String cubeName) throws ParsingException {

    final Node tableNode = cubeNode.selectSingleNode("Table");
    final String exceptionMessage = "The cube " + cubeName + " must specify a corresponding table";

    if (null == tableNode) {
        throw new ParsingException(exceptionMessage);
    }

    final String name = tableNode.valueOf("@name");
    if (StringUtil.isEmpty(name)) {
        throw new ParsingException(exceptionMessage);
    }
    return name;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * Get all dimensions mapped in the data-warehouse mapping file
 *
 * @param document An instance of {@link Document}
 * @return All dimensions//from w  w w . j a v  a 2  s .  com
 * @throws ParsingException
 * @see Cube
 */
private List<Dimension> loadDimensions(final Document document) throws ParsingException {

    log.debug("Loading all dimensions from the schema ...");
    List<Dimension> dimensions = new ArrayList<>();

    List<Node> dimensionNodes = document.selectNodes("//Schema/Dimension");
    Set<String> notAvailableDimensionNames = new HashSet<>();
    int sprinter = 1;
    String dimensionName;

    for (Node node : dimensionNodes) {

        dimensionName = node.valueOf("@name");
        if (StringUtil.isEmpty(dimensionName)) {
            throw new ParsingException("A dimension name is missing. The dimension index is : " + sprinter);
        }

        checkDimensionAvailability(dimensionName, notAvailableDimensionNames);
        final Dimension dimension = new Dimension();
        dimension.setName(dimensionName);
        dimension.addHierarchies(loadHierarchies(node, dimensionName));
        dimensions.add(dimension);

        log.debug("\t{} dimension was found", dimension.getName());
        sprinter++;
    }

    log.debug("{} dimension(s) successfully loaded", (sprinter - 1));
    return dimensions;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * Load the current dimension hierachies
 *
 * @param dimensionNode/*from  ww w. ja  v  a 2s  .  co  m*/
 * @param dimensionName
 * @return list of hierarchies that correspond to the dimension
 * @throws ParsingException an error occurred when parsing the file
 */
private List<Hierarchy> loadHierarchies(final Node dimensionNode, final String dimensionName)
        throws ParsingException {

    log.debug("Loading all {} hierarchies", dimensionName);
    List<Node> hierarchiesNodes = dimensionNode.selectNodes("Hierarchy");
    List<Hierarchy> hierarchies = new ArrayList<>();
    Set<String> notAvailableHierarchyNames = new HashSet<>();
    String worker;
    int sprinter = 1;
    boolean defaultHierarchyDefined = false;

    for (Node hierarchyNode : hierarchiesNodes) {

        worker = hierarchyNode.valueOf("@hasAll");
        checkHasAllValidity(worker, dimensionName, sprinter);
        Hierarchy hierarchy = new Hierarchy();
        hierarchy.setHasAll("true".equalsIgnoreCase(worker));

        // get the required name ...
        worker = hierarchyNode.valueOf("@name");
        if (StringUtil.isEmpty(worker) && !defaultHierarchyDefined) {
            throw new ParsingException("An hierarchy name is missing. The hierarchy index is : " + sprinter);
        }
        defaultHierarchyDefined = true;

        // set the name if available ...
        checkHiearchyAvailability(worker, notAvailableHierarchyNames);
        hierarchy.setName(worker);

        // set the hierarchy primary key attribute ...
        worker = hierarchyNode.valueOf("@primaryKey");
        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("The following attribute must be specified for the " + dimensionName
                    + " dimension : primaryKey");
        }
        hierarchy.setPrimaryKey(worker);
        hierarchy.addLevels(loadLevels(hierarchyNode, hierarchy.getName(), dimensionName));

        hierarchies.add(hierarchy);
        log.debug("\tHierarchy {} added to {} hierarchies", hierarchy.getName(), dimensionName);
    }

    return hierarchies;
}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * Load all levels from an hierarchy node
 *
 * @param hierarchyNode The hierarchy node
 * @param hierarchyName The name of the concerned hierarchy
 * @param dimensionName The name of the concerned dimension
 * @return All the levels of the specified hierarchy
 * @throws ParsingException/*from ww  w  .  ja v a  2  s  .co m*/
 * @see Level
 */
private List<Level> loadLevels(final Node hierarchyNode, final String hierarchyName, final String dimensionName)
        throws ParsingException {

    log.debug("Loading all {} levels ...", hierarchyName);
    List<Level> levels = new ArrayList<>();
    final List<Node> levelNodes = hierarchyNode.selectNodes("Level");
    String worker;
    int sprinter = 1;

    for (Node levelNode : levelNodes) {

        worker = levelNode.valueOf("@name");
        if (StringUtil.isEmpty(worker)) {

            StringBuilder exceptionMessage = new StringBuilder();
            exceptionMessage.append("The name attribute is missing for the #").append(sprinter)
                    .append(" level of the ").append(hierarchyName).append(" hierarchy of the ")
                    .append(dimensionName).append(" dimension ");

            throw new ParsingException(exceptionMessage.toString());
        }
        Level level = new Level();
        level.setName(worker);

        worker = levelNode.valueOf("@column");
        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("A column attribute is required for the " + level.getName());
        }
        level.setColumn(worker);

        worker = levelNode.valueOf("@uniqueMembers");
        checkUniqueMembersValidity(worker, level.getName());
        level.setUniqueMembers("true".equalsIgnoreCase(worker));

        level.addProperties(loadProperties(levelNode, level.getName(), dimensionName));
        log.debug("\tLevel {} added to {} hierarchy", level.getName(), hierarchyName);
        sprinter++;
    }
    return levels;

}

From source file:org.rolap4j.parsers.Dom4jBasedCatalogParser.java

License:Apache License

/**
 * @param levelNode// w  w  w . j  a  va2s.  c o m
 * @param levelName
 * @param dimensionName
 * @return
 * @throws ParsingException
 */
private Set<Property> loadProperties(final Node levelNode, final String levelName, final String dimensionName)
        throws ParsingException {

    log.debug("Loading all {} level properties ...", levelName);
    Set<Property> properties = new HashSet<>();
    List<Node> propertyNodes = levelNode.selectNodes("Property");
    String worker;
    Set<String> notAvailablePropertyNames = new HashSet<>();

    for (Node propertyNode : propertyNodes) {

        worker = propertyNode.valueOf("@name");

        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException(
                    "The name attribute is missing for one or some properties of " + levelName + " level");
        }

        checkPropertyAvailability(worker, notAvailablePropertyNames);
        final Property property = new Property();
        property.setName(worker);
        worker = propertyNode.valueOf("@column");

        if (StringUtil.isEmpty(worker)) {
            throw new ParsingException("A column attribute is required for " + property.getName()
                    + " property of " + levelName + " level");
        }

        property.setColumn(worker);
        property.setType(PropertyUtil.getPropertyType(propertyNode.valueOf("@type")));
        properties.add(property);
    }
    return properties;
}

From source file:org.rundeck.api.parser.AbortParser.java

License:Apache License

@Override
public RundeckAbort parse(Node abortNode) {
    RundeckAbort abort = new RundeckAbort();

    try {//from  w  w w . ja v a2 s.  c  o m
        abort.setStatus(AbortStatus.valueOf(StringUtils.upperCase(abortNode.valueOf("@status"))));
    } catch (IllegalArgumentException e) {
        abort.setStatus(null);
    }

    Node execNode = abortNode.selectSingleNode("execution");
    if (execNode != null) {
        RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode);
        abort.setExecution(execution);
    }

    return abort;
}