Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

In this page you can find the example usage for org.jdom2 Element getChildren.

Prototype

public List<Element> getChildren(final String cname, final Namespace ns) 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned as Element objects.

Usage

From source file:ca.nrc.cadc.vos.NodeReader.java

License:Open Source License

/**
 * Builds a List of NodeProperty objects from the Document property Elements.
 *
 * @param el a node Element of the Document.
 * @param namespace Document Namespace./*www.  ja  v a  2s.c o m*/
 * @return List of NodeProperty objects.
 * @throws NodeParsingException if there is an error parsing the XML.
 */
protected List<NodeProperty> getProperties(Element el, Namespace namespace) throws NodeParsingException {
    // properties element
    Element properties = el.getChild("properties", namespace);
    if (properties == null) {
        String error = "properties element not found";
        throw new NodeParsingException(error);
    }

    // new NodeProperty List
    List<NodeProperty> set = new ArrayList<NodeProperty>();

    // properties property elements
    List<Element> propertyList = properties.getChildren("property", namespace);
    for (Element property : propertyList) {
        String propertyUri = property.getAttributeValue("uri");
        if (propertyUri == null) {
            String error = "uri attribute not found in property element " + property;
            throw new NodeParsingException(error);
        }

        // xsi:nil set to true indicates Property is to be deleted
        String xsiNil = property.getAttributeValue("nil", xsiNamespace);
        boolean markedForDeletion = false;
        if (xsiNil != null)
            markedForDeletion = xsiNil.equalsIgnoreCase("true") ? true : false;

        // if marked for deletetion, property can not contain text content
        String text = property.getText();
        if (markedForDeletion)
            text = "";

        // create new NodeProperty
        NodeProperty nodeProperty = new NodeProperty(propertyUri, text);

        // set readOnly attribute
        String readOnly = property.getAttributeValue("readOnly");
        if (readOnly != null)
            nodeProperty.setReadOnly((readOnly.equalsIgnoreCase("true") ? true : false));

        // markedForDeletion attribute
        nodeProperty.setMarkedForDeletion(markedForDeletion);
        set.add(nodeProperty);
    }

    return set;
}

From source file:ca.nrc.cadc.vos.NodeReader.java

License:Open Source License

protected List<URI> getViewURIs(Element root, Namespace namespace, String parent)
        throws NodeParsingException, URISyntaxException {

    // new View List
    List<URI> list = new ArrayList<URI>();

    // view parent element
    Element parentElement = root.getChild(parent, namespace);
    if (parentElement == null) {
        return list;
    }/* w  w w . j a va 2 s  .c  o  m*/

    // view elements
    List<Element> uriList = parentElement.getChildren("view", namespace);
    for (Element view : uriList) {
        // view uri attribute
        String viewUri = view.getAttributeValue("uri");
        if (viewUri == null) {
            String error = "uri attribute not found in " + parent + " view element";
            throw new NodeParsingException(error);
        }
        log.debug(parent + "view uri: " + viewUri);
        list.add(new URI(viewUri));
    }

    return list;
}

From source file:ca.nrc.cadc.vos.NodeReader.java

License:Open Source License

/**
 * Builds a List of View objects from the view elements contained within
 * the given parent element.//  w  w  w .java  2  s.  c  o  m
 *
 * @param root Root Element of the Document.
 * @param namespace Document Namespace.
 * @param parent View Parent Node.
 * @return List of View objects.
 * @throws NodeParsingException if there is an error parsing the XML.
 */
protected List<View> getViews(Element root, Namespace namespace, String parent) throws NodeParsingException {
    // view parent element
    Element parentElement = root.getChild(parent, namespace);
    if (parentElement == null) {
        String error = parent + " element not found in node";
        throw new NodeParsingException(error);
    }

    // new View List
    List<View> list = new ArrayList<View>();

    // view elements
    List<Element> viewList = parentElement.getChildren("view", namespace);
    for (Element view : viewList) {
        // view uri attribute
        String viewUri = view.getAttributeValue("uri");
        if (viewUri == null) {
            String error = "uri attribute not found in " + parent + " view element";
            throw new NodeParsingException(error);
        }
        log.debug(parent + "view uri: " + viewUri);

        // new View
        //                View acceptsView = new View(viewUri, node);

        // view original attribute
        String original = view.getAttributeValue("original");
        if (original != null) {
            boolean isOriginal = original.equalsIgnoreCase("true") ? true : false;
            //                    view.setOriginal(isOriginal);
            log.debug(parent + " view original: " + isOriginal);
        }

        List<Element> paramList = view.getChildren("param", namespace);
        for (Element param : paramList) {
            String paramUri = param.getAttributeValue("uri");
            if (paramUri == null) {
                String error = "param uri attribute not found in accepts view element";
                throw new NodeParsingException(error);
            }
            log.debug("accepts view param uri: " + paramUri);
            // TODO: what are params???
        }
    }

    return list;
}

From source file:ca.nrc.cadc.vos.TransferReader.java

License:Open Source License

private Transfer parseTransfer(Document document) throws URISyntaxException {
    Element root = document.getRootElement();

    Direction direction = parseDirection(root);
    // String serviceUrl; // not in XML yet
    VOSURI target = new VOSURI(root.getChildText("target", VOS_NS));

    // TODO: get view nodes and uri attribute
    View view = null;/*from  ww w. j a v a 2s .c  o  m*/
    Parameter param = null;
    List views = root.getChildren("view", VOS_NS);
    if (views != null && views.size() > 0) {
        Element v = (Element) views.get(0);
        view = new View(new URI(v.getAttributeValue("uri")));
        List params = v.getChildren("param", VOS_NS);
        if (params != null) {
            for (Object o : params) {
                Element p = (Element) o;
                param = new Parameter(new URI(p.getAttributeValue("uri")), p.getText());
                view.getParameters().add(param);
            }
        }
    }
    List<Protocol> protocols = parseProtocols(root);
    String keepBytesStr = root.getChildText("keepBytes", VOS_NS);

    if (keepBytesStr != null) {
        boolean keepBytes = true;
        keepBytes = keepBytesStr.equalsIgnoreCase("true");
        return new Transfer(target, direction, view, protocols, keepBytes);
    }
    return new Transfer(target, direction, view, protocols);
}

From source file:ca.nrc.cadc.vos.TransferReader.java

License:Open Source License

private List<Protocol> parseProtocols(Element root) {
    List<Protocol> rtn = null;
    //Element e = root.getChild("protocols", NS);
    List prots = root.getChildren("protocol", VOS_NS);
    if (prots != null && prots.size() > 0) {
        rtn = new ArrayList<Protocol>(prots.size());
        for (Object obj : prots) {
            Element eProtocol = (Element) obj;
            String uri = eProtocol.getAttributeValue("uri");
            String endpoint = eProtocol.getChildText("endpoint", VOS_NS);
            rtn.add(new Protocol(uri, endpoint, null));
        }/*from  w w w  .  j a v  a2  s  .  c  om*/
    }
    return rtn;
}

From source file:ch.kostceco.tools.kostval.validation.modulesiard.impl.ValidationEcolumnModuleImpl.java

License:Open Source License

private boolean prepareValidationData(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    int onWork = 41;
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    // Gets the tables to be validated
    List<SiardTable> siardTables = new ArrayList<SiardTable>();
    Document document = validationContext.getMetadataXMLDocument();
    Element rootElement = document.getRootElement();
    String workingDirectory = validationContext.getConfigurationService().getPathToWorkDir();
    String siardSchemasElementsName = properties.getProperty("module.e.siard.metadata.xml.schemas.name");
    // Gets the list of <schemas> elements from metadata.xml
    List<Element> siardSchemasElements = rootElement.getChildren(siardSchemasElementsName,
            validationContext.getXmlNamespace());
    for (Element siardSchemasElement : siardSchemasElements) {
        // Gets the list of <schema> elements from metadata.xml
        List<Element> siardSchemaElements = siardSchemasElement.getChildren(
                properties.getProperty("module.e.siard.metadata.xml.schema.name"),
                validationContext.getXmlNamespace());
        // Iterating over all <schema> elements
        for (Element siardSchemaElement : siardSchemaElements) {
            String schemaFolderName = siardSchemaElement
                    .getChild(properties.getProperty("module.e.siard.metadata.xml.schema.folder.name"),
                            validationContext.getXmlNamespace())
                    .getValue();//www .j  av  a 2 s. c  o  m
            Element siardTablesElement = siardSchemaElement.getChild(
                    properties.getProperty("module.e.siard.metadata.xml.tables.name"),
                    validationContext.getXmlNamespace());
            List<Element> siardTableElements = siardTablesElement.getChildren(
                    properties.getProperty("module.e.siard.metadata.xml.table.name"),
                    validationContext.getXmlNamespace());
            // Iterating over all containing table elements
            for (Element siardTableElement : siardTableElements) {
                Element siardColumnsElement = siardTableElement.getChild(
                        properties.getProperty("module.e.siard.metadata.xml.columns.name"),
                        validationContext.getXmlNamespace());
                List<Element> siardColumnElements = siardColumnsElement.getChildren(
                        properties.getProperty("module.e.siard.metadata.xml.column.name"),
                        validationContext.getXmlNamespace());
                String tableName = siardTableElement
                        .getChild(properties.getProperty("module.e.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                SiardTable siardTable = new SiardTable();
                siardTable.setMetadataXMLElements(siardColumnElements);
                siardTable.setTableName(tableName);
                String siardTableFolderName = siardTableElement
                        .getChild(properties.getProperty("module.e.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                StringBuilder pathToTableSchema = new StringBuilder();
                // Preparing access to the according XML schema file
                pathToTableSchema.append(workingDirectory);
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(properties.getProperty("module.e.siard.path.to.content"));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(properties.getProperty("module.e.siard.table.xsd.file.extension"));
                // Retrieve the according XML schema
                File tableSchema = validationContext.getSiardFiles().get(pathToTableSchema.toString());
                SAXBuilder builder = new SAXBuilder();
                Document tableSchemaDocument = builder.build(tableSchema);
                Element tableSchemaRootElement = tableSchemaDocument.getRootElement();
                Namespace namespace = tableSchemaRootElement.getNamespace();
                // Getting the tags from XML schema to be validated
                Element tableSchemaComplexType = tableSchemaRootElement
                        .getChild(properties.getProperty("module.e.siard.table.xsd.complexType"), namespace);
                Element tableSchemaComplexTypeSequence = tableSchemaComplexType
                        .getChild(properties.getProperty("module.e.siard.table.xsd.sequence"), namespace);
                List<Element> tableSchemaComplexTypeElements = tableSchemaComplexTypeSequence
                        .getChildren(properties.getProperty("module.e.siard.table.xsd.element"), namespace);
                siardTable.setTableXSDElements(tableSchemaComplexTypeElements);
                siardTables.add(siardTable);
                // Writing back the List off all SIARD tables to the validation context
                validationContext.setSiardTables(siardTables);
            }
        }
        if (onWork == 41) {
            onWork = 2;
            System.out.print("E-   ");
            System.out.print("\r");
        } else if (onWork == 11) {
            onWork = 12;
            System.out.print("E\\   ");
            System.out.print("\r");
        } else if (onWork == 21) {
            onWork = 22;
            System.out.print("E|   ");
            System.out.print("\r");
        } else if (onWork == 31) {
            onWork = 32;
            System.out.print("E/   ");
            System.out.print("\r");
        } else {
            onWork = onWork + 1;
        }
    }
    if (validationContext.getSiardTables().size() > 0) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;
    } else {
        this.setValidationContext(null);
        successfullyCommitted = false;
        throw new Exception();
    }
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationFrowModuleImpl.java

License:Open Source License

private boolean prepareValidationData(Properties properties, File metadataXML)
        throws JDOMException, IOException {
    boolean successfullyCommitted = false;
    String me = "[F.0.6] prepareValidationData (Properties properties, File metadataXML) ";
    //Initializing validation Logging
    StringBuilder validationLog = new StringBuilder();
    //Gets the tables to be validated
    List<SiardTable> siardTables = new ArrayList<SiardTable>();
    Document document = this.getMetadataXMLDocument();
    Element rootElement = document.getRootElement();
    String workingDirectory = this.getConfigurationService().getPathToWorkDir();
    String siardSchemasElementsName = properties.getProperty("module.f.siard.metadata.xml.schemas.name");
    //Gets the list of <schemas> elements from metadata.xml
    List<Element> siardSchemasElements = rootElement.getChildren(siardSchemasElementsName,
            this.getXmlNamespace());
    for (Element siardSchemasElement : siardSchemasElements) {
        //Gets the list of <schema> elements from metadata.xml
        List<Element> siardSchemaElements = siardSchemasElement.getChildren(
                properties.getProperty("module.f.siard.metadata.xml.schema.name"), this.getXmlNamespace());
        //Iterating over all <schema> elements
        for (Element siardSchemaElement : siardSchemaElements) {
            String schemaFolderName = siardSchemaElement
                    .getChild(properties.getProperty("module.f.siard.metadata.xml.schema.folder.name"),
                            this.getXmlNamespace())
                    .getValue();/*from  w  w w. j av  a  2s. c  om*/
            Element siardTablesElement = siardSchemaElement.getChild(
                    properties.getProperty("module.f.siard.metadata.xml.tables.name"), this.getXmlNamespace());
            List<Element> siardTableElements = siardTablesElement.getChildren(
                    properties.getProperty("module.f.siard.metadata.xml.table.name"), this.getXmlNamespace());
            //Iterating over all containing table elements
            for (Element siardTableElement : siardTableElements) {
                Element siardColumnsElement = siardTableElement.getChild(
                        properties.getProperty("module.f.siard.metadata.xml.columns.name"),
                        this.getXmlNamespace());
                List<Element> siardColumnElements = siardColumnsElement.getChildren(
                        properties.getProperty("module.f.siard.metadata.xml.column.name"),
                        this.getXmlNamespace());
                String tableName = siardTableElement
                        .getChild(properties.getProperty("module.f.siard.metadata.xml.table.folder.name"),
                                this.getXmlNamespace())
                        .getValue();
                //SiardTable siardTable = new SiardTable();
                //Decoupling dependency to SiardTable Bean by injecting it via Spring
                SiardTable siardTable = this.getSiardTable();
                siardTable.setMetadataXMLElements(siardColumnElements);
                siardTable.setTableName(tableName);
                String siardTableFolderName = siardTableElement
                        .getChild(properties.getProperty("module.f.siard.metadata.xml.table.folder.name"),
                                this.getXmlNamespace())
                        .getValue();
                StringBuilder pathToTableSchema = new StringBuilder();
                StringBuilder pathToTableData = new StringBuilder();
                //Preparing access to the according XML schema file
                pathToTableSchema.append(workingDirectory);
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(properties.getProperty("module.f.siard.path.to.content"));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(properties.getProperty("module.f.siard.table.xsd.file.extension"));
                //Preparing access to the table XML data file
                pathToTableData.append(workingDirectory);
                pathToTableData.append(File.separator);
                pathToTableData.append(properties.getProperty("module.f.siard.path.to.content"));
                pathToTableData.append(File.separator);
                pathToTableData.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableData.append(File.separator);
                pathToTableData.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableData.append(File.separator);
                pathToTableData.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableData.append(properties.getProperty("module.f.siard.table.xml.file.extension"));
                //Retrieve the according XML schema
                File tableSchema = this.getSiardFiles().get(pathToTableSchema.toString());
                File tableData = this.getSiardFiles().get(pathToTableData.toString());
                SAXBuilder builder = new SAXBuilder();
                Document tableSchemaDocument = builder.build(tableSchema);
                Document tableDataDocument = builder.build(tableData);
                Element tableSchemaRootElement = tableSchemaDocument.getRootElement();
                Element tableDataRootElement = tableDataDocument.getRootElement();
                Namespace xsdNamespace = tableSchemaRootElement.getNamespace();
                Namespace xmlNamespace = tableDataRootElement.getNamespace();
                //Getting the tags from XML schema to be validated
                Element tableSchemaComplexType = tableSchemaRootElement
                        .getChild(properties.getProperty("module.f.siard.table.xsd.complexType"), xsdNamespace);
                Element tableSchemaComplexTypeSequence = tableSchemaComplexType
                        .getChild(properties.getProperty("module.f.siard.table.xsd.sequence"), xsdNamespace);
                Element tableDataComplexType = tableDataRootElement
                        .getChild(properties.getProperty("module.f.siard.table.xml.complexType"), xmlNamespace);
                Element tableDataComplexTypeSequence = tableDataComplexType
                        .getChild(properties.getProperty("module.f.siard.table.xml.sequence"), xmlNamespace);
                List<Element> tableSchemaComplexTypeElements = tableSchemaComplexTypeSequence
                        .getChildren(properties.getProperty("module.f.siard.table.xsd.element"), xsdNamespace);
                List<Element> tableDataComplexTypeElements = tableDataComplexTypeSequence
                        .getChildren(properties.getProperty("module.f.siard.table.xml.element"), xmlNamespace);
                siardTable.setTableXSDElements(tableSchemaComplexTypeElements);
                siardTable.setTableXMLElements(tableDataComplexTypeElements);
                siardTables.add(siardTable);
                //Writing back the List off all SIARD tables to the validation context
                this.setSiardTables(siardTables);
            }
        }
    }
    if (this.getSiardTables() != null && properties != null && metadataXML != null) {
        //Updating the validation log
        String message = properties.getProperty("successfully.executed");
        String newline = properties.getProperty("newline");
        validationLog.append(me + message);
        validationLog.append(newline);
        successfullyCommitted = true;
    } else {
        String message = "has failed";
        validationLog.append(me + message);
        validationLog.append('\n');
    }
    this.getValidationLog().append(validationLog);
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationEcolumnModuleImpl.java

License:Open Source License

private boolean prepareValidationData(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    // Gets the tables to be validated
    List<SiardTable> siardTables = new ArrayList<SiardTable>();
    Document document = validationContext.getMetadataXMLDocument();
    Element rootElement = document.getRootElement();
    String workingDirectory = validationContext.getConfigurationService().getPathToWorkDir();
    String siardSchemasElementsName = properties.getProperty("module.e.siard.metadata.xml.schemas.name");
    // Gets the list of <schemas> elements from metadata.xml
    List<Element> siardSchemasElements = rootElement.getChildren(siardSchemasElementsName,
            validationContext.getXmlNamespace());
    for (Element siardSchemasElement : siardSchemasElements) {
        // Gets the list of <schema> elements from metadata.xml
        List<Element> siardSchemaElements = siardSchemasElement.getChildren(
                properties.getProperty("module.e.siard.metadata.xml.schema.name"),
                validationContext.getXmlNamespace());
        // Iterating over all <schema> elements
        for (Element siardSchemaElement : siardSchemaElements) {
            String schemaFolderName = siardSchemaElement
                    .getChild(properties.getProperty("module.e.siard.metadata.xml.schema.folder.name"),
                            validationContext.getXmlNamespace())
                    .getValue();/*from  w  w w  .j a v a 2  s  .c  om*/
            Element siardTablesElement = siardSchemaElement.getChild(
                    properties.getProperty("module.e.siard.metadata.xml.tables.name"),
                    validationContext.getXmlNamespace());
            List<Element> siardTableElements = siardTablesElement.getChildren(
                    properties.getProperty("module.e.siard.metadata.xml.table.name"),
                    validationContext.getXmlNamespace());
            // Iterating over all containing table elements
            for (Element siardTableElement : siardTableElements) {
                Element siardColumnsElement = siardTableElement.getChild(
                        properties.getProperty("module.e.siard.metadata.xml.columns.name"),
                        validationContext.getXmlNamespace());
                List<Element> siardColumnElements = siardColumnsElement.getChildren(
                        properties.getProperty("module.e.siard.metadata.xml.column.name"),
                        validationContext.getXmlNamespace());
                String tableName = siardTableElement
                        .getChild(properties.getProperty("module.e.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                SiardTable siardTable = new SiardTable();
                siardTable.setMetadataXMLElements(siardColumnElements);
                siardTable.setTableName(tableName);
                String siardTableFolderName = siardTableElement
                        .getChild(properties.getProperty("module.e.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                StringBuilder pathToTableSchema = new StringBuilder();
                // Preparing access to the according XML schema file
                pathToTableSchema.append(workingDirectory);
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(properties.getProperty("module.e.siard.path.to.content"));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(properties.getProperty("module.e.siard.table.xsd.file.extension"));
                // Retrieve the according XML schema
                File tableSchema = validationContext.getSiardFiles().get(pathToTableSchema.toString());
                SAXBuilder builder = new SAXBuilder();
                Document tableSchemaDocument = builder.build(tableSchema);
                Element tableSchemaRootElement = tableSchemaDocument.getRootElement();
                Namespace namespace = tableSchemaRootElement.getNamespace();
                // Getting the tags from XML schema to be validated
                Element tableSchemaComplexType = tableSchemaRootElement
                        .getChild(properties.getProperty("module.e.siard.table.xsd.complexType"), namespace);
                Element tableSchemaComplexTypeSequence = tableSchemaComplexType
                        .getChild(properties.getProperty("module.e.siard.table.xsd.sequence"), namespace);
                List<Element> tableSchemaComplexTypeElements = tableSchemaComplexTypeSequence
                        .getChildren(properties.getProperty("module.e.siard.table.xsd.element"), namespace);
                siardTable.setTableXSDElements(tableSchemaComplexTypeElements);
                siardTables.add(siardTable);
                // Writing back the List off all SIARD tables to the
                // validation context
                validationContext.setSiardTables(siardTables);
            }
        }
    }
    if (validationContext.getSiardTables().size() > 0) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;
    } else {
        this.setValidationContext(null);
        successfullyCommitted = false;
        throw new Exception();
    }
    return successfullyCommitted;
}

From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationFrowModuleImpl.java

License:Open Source License

private boolean prepareValidationData(ValidationContext validationContext)
        throws JDOMException, IOException, Exception {
    boolean successfullyCommitted = false;
    Properties properties = validationContext.getValidationProperties();
    // Gets the tables to be validated
    List<SiardTable> siardTables = new ArrayList<SiardTable>();
    Document document = validationContext.getMetadataXMLDocument();
    Element rootElement = document.getRootElement();
    String workingDirectory = validationContext.getConfigurationService().getPathToWorkDir();
    String siardSchemasElementsName = properties.getProperty("module.f.siard.metadata.xml.schemas.name");
    // Gets the list of <schemas> elements from metadata.xml
    List<Element> siardSchemasElements = rootElement.getChildren(siardSchemasElementsName,
            validationContext.getXmlNamespace());
    for (Element siardSchemasElement : siardSchemasElements) {
        // Gets the list of <schema> elements from metadata.xml
        List<Element> siardSchemaElements = siardSchemasElement.getChildren(
                properties.getProperty("module.f.siard.metadata.xml.schema.name"),
                validationContext.getXmlNamespace());
        // Iterating over all <schema> elements
        for (Element siardSchemaElement : siardSchemaElements) {
            String schemaFolderName = siardSchemaElement
                    .getChild(properties.getProperty("module.f.siard.metadata.xml.schema.folder.name"),
                            validationContext.getXmlNamespace())
                    .getValue();/* w w  w.  ja v a 2s .  co m*/
            Element siardTablesElement = siardSchemaElement.getChild(
                    properties.getProperty("module.f.siard.metadata.xml.tables.name"),
                    validationContext.getXmlNamespace());
            List<Element> siardTableElements = siardTablesElement.getChildren(
                    properties.getProperty("module.f.siard.metadata.xml.table.name"),
                    validationContext.getXmlNamespace());
            // Iterating over all containing table elements
            for (Element siardTableElement : siardTableElements) {
                Element siardColumnsElement = siardTableElement.getChild(
                        properties.getProperty("module.f.siard.metadata.xml.columns.name"),
                        validationContext.getXmlNamespace());
                List<Element> siardColumnElements = siardColumnsElement.getChildren(
                        properties.getProperty("module.f.siard.metadata.xml.column.name"),
                        validationContext.getXmlNamespace());
                String tableName = siardTableElement
                        .getChild(properties.getProperty("module.f.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                SiardTable siardTable = new SiardTable();
                // Add Table Root Element
                siardTable.setTableRootElement(siardTableElement);
                siardTable.setMetadataXMLElements(siardColumnElements);
                siardTable.setTableName(tableName);
                String siardTableFolderName = siardTableElement
                        .getChild(properties.getProperty("module.f.siard.metadata.xml.table.folder.name"),
                                validationContext.getXmlNamespace())
                        .getValue();
                StringBuilder pathToTableSchema = new StringBuilder();
                // Preparing access to the according XML schema file
                pathToTableSchema.append(workingDirectory);
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(properties.getProperty("module.f.siard.path.to.content"));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(File.separator);
                pathToTableSchema.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableSchema.append(properties.getProperty("module.f.siard.table.xsd.file.extension"));
                // Retrieve the according XML schema
                File tableSchema = validationContext.getSiardFiles().get(pathToTableSchema.toString());

                // --> Hier
                StringBuilder pathToTableXML = new StringBuilder();
                pathToTableXML.append(workingDirectory);
                pathToTableXML.append(File.separator);
                pathToTableXML.append(properties.getProperty("module.f.siard.path.to.content"));
                pathToTableXML.append(File.separator);
                pathToTableXML.append(schemaFolderName.replaceAll(" ", ""));
                pathToTableXML.append(File.separator);
                pathToTableXML.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableXML.append(File.separator);
                pathToTableXML.append(siardTableFolderName.replaceAll(" ", ""));
                pathToTableXML.append(properties.getProperty("module.f.siard.table.xml.file.extension"));
                File tableXML = validationContext.getSiardFiles().get(pathToTableXML.toString());

                SAXBuilder schemaBuilder = new SAXBuilder();
                Document tableSchemaDocument = schemaBuilder.build(tableSchema);
                Element tableSchemaRootElement = tableSchemaDocument.getRootElement();

                // Getting the tags from XML schema to be validated

                siardTable.setTableXSDRootElement(tableSchemaRootElement);

                SAXBuilder xmlBuilder = new SAXBuilder();
                Document tableXMLDocument = xmlBuilder.build(tableXML);
                Element tableXMLRootElement = tableXMLDocument.getRootElement();
                Namespace xMLNamespace = tableXMLRootElement.getNamespace();
                List<Element> tableXMLElements = tableXMLRootElement.getChildren(
                        properties.getProperty("module.f.siard.table.xml.row.element.name"), xMLNamespace);
                siardTable.setTableXMLElements(tableXMLElements);
                siardTables.add(siardTable);
                // Writing back the List off all SIARD tables to the
                // validation context
                validationContext.setSiardTables(siardTables);
            }
        }
    }
    if (validationContext.getSiardTables().size() > 0) {
        this.setValidationContext(validationContext);
        successfullyCommitted = true;
    } else {
        this.setValidationContext(null);
        successfullyCommitted = false;
        throw new Exception();
    }
    return successfullyCommitted;
}

From source file:com.bc.ceres.binio.binx.BinX.java

License:Open Source License

private List getChildren(Element element, String name, boolean require) throws BinXException {
    final List children = element.getChildren(name, namespace);
    if (require && children.isEmpty()) {
        if (name != null) {
            throw new BinXException(
                    MessageFormat.format("Element ''{0}'': Expected to have at least one child of ''{1}''",
                            element.getName(), name));
        } else {/*  www . j  a  va2  s .c o m*/
            throw new BinXException(MessageFormat.format("Element ''{0}'': Expected to have at least one child",
                    element.getName()));
        }
    }
    return children;
}