Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

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

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

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

License:Open Source License

private boolean validateColumnOccurrence(ValidationContext validationContext) throws Exception {
    boolean validTable;
    boolean validDatabase;
    Properties properties = validationContext.getValidationProperties();
    List<SiardTable> siardTables = validationContext.getSiardTables();
    StringBuilder namesOfInvalidTables = new StringBuilder();
    StringBuilder namesOfInvalidColumns = new StringBuilder();
    validDatabase = true;/*w  w w.j a  v  a2 s  .c o m*/
    // Iterate over the SIARD tables and verify the nullable attribute
    for (SiardTable siardTable : siardTables) {
        String nullable = new String();
        String minOccurs = new String();
        validTable = true;
        // Number of attributes in metadata.xml
        int metadataXMLColumnsCount = siardTable.getMetadataXMLElements().size();
        // Number of attributes in schema.xsd
        int tableXSDColumnsCount = siardTable.getTableXSDElements().size();
        // Counter. In order to prevent indexOutOfBorder errors
        // columnsCounter is assigned to the smaller
        // number of columns
        int columnCount = (metadataXMLColumnsCount >= tableXSDColumnsCount) ? tableXSDColumnsCount
                : metadataXMLColumnsCount;
        // Element/Attributes of the actual SIARD table
        List<Element> xmlElements = siardTable.getMetadataXMLElements();
        // Elements/Attributes of the according XML schema
        List<Element> xsdElements = siardTable.getTableXSDElements();
        Namespace xmlNamespace = validationContext.getXmlNamespace();
        for (int i = 0; i < columnCount; i++) {
            // for ( int i = 0; i < metadataXMLColumnsCount; i++) {
            // Actual Element of the metadata.xml
            Element xmlElement = xmlElements.get(i);
            // Actual Element of the according XML schema
            Element xsdElement = xsdElements.get(i);
            String nullableElementDescription = properties
                    .getProperty("module.e.siard.metadata.xml.nullable.element.name");
            String minuOccursAttributeDescription = properties
                    .getProperty("module.e.siard.table.xsd.attribute.minOccurs.name");
            String nameAttributeDescription = properties.getProperty("module.e.siard.table.xsd.attribute.name");
            // Value of the nullable Element in metadata.xml
            nullable = xmlElement.getChild(nullableElementDescription, xmlNamespace).getValue();
            // Value of the minOccurs attribute in the according XML schema
            minOccurs = xsdElement.getAttributeValue(minuOccursAttributeDescription);
            // If the nullable Element is set to true and the minOccurs
            // attribute is null
            if (nullable.equalsIgnoreCase("true") && minOccurs == null) {
                validTable = false;
                validDatabase = false;
                namesOfInvalidColumns.append((namesOfInvalidColumns.length() > 0) ? ", " : "");
                namesOfInvalidColumns.append(xsdElement.getAttributeValue(nameAttributeDescription));
                // If the nullable Element is set to true and the minOccurs
                // attribute is set to zero
            } else if (nullable.equalsIgnoreCase("true") && minOccurs.equalsIgnoreCase("0")) {
            } else if (nullable.equalsIgnoreCase("false") && minOccurs == null) {
            } else if (nullable.equalsIgnoreCase("false") && minOccurs.equalsIgnoreCase("0")) {
                validTable = false;
                validDatabase = false;
                namesOfInvalidColumns.append((namesOfInvalidColumns.length() > 0) ? ", " : "");
                namesOfInvalidColumns.append(xsdElement.getAttributeValue(nameAttributeDescription));
            }
        }
        if (validTable == false) {
            namesOfInvalidTables.append((namesOfInvalidTables.length() > 0) ? ", " : "");
            namesOfInvalidTables.append(siardTable.getTableName());
            namesOfInvalidTables.append(properties.getProperty("module.e.siard.table.xsd.file.extension"));
            namesOfInvalidTables.append("(");
            namesOfInvalidTables.append(namesOfInvalidColumns);
            namesOfInvalidTables.append(")");
            namesOfInvalidColumns = null;
            namesOfInvalidColumns = new StringBuilder();
        }
    }
    // Writing back error log
    this.setIncongruentColumnOccurrence(namesOfInvalidTables);
    return validDatabase;
}

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

License:Open Source License

private boolean validateColumnType(ValidationContext validationContext) throws Exception {
    boolean validTable;
    boolean validDatabase;
    Properties properties = validationContext.getValidationProperties();
    List<SiardTable> siardTables = validationContext.getSiardTables();
    StringBuilder namesOfInvalidTables = new StringBuilder();
    StringBuilder namesOfInvalidColumns = new StringBuilder();
    // List of all XML column elements to verify the allover sequence
    List<String> xmlElementSequence = new ArrayList<String>();
    // List of all XSD column elements
    List<String> xsdElementSequence = new ArrayList<String>();
    // Iterate over the SIARD tables and verify the column types
    validDatabase = true;//from   w  w  w  .j av a2 s  .c  om
    for (SiardTable siardTable : siardTables) {
        // Elements of the actual SIARD table
        List<Element> xmlElements = siardTable.getMetadataXMLElements();
        // Elements of the according XML schema
        List<Element> xsdElements = siardTable.getTableXSDElements();
        // Number of attributes in metadata.xml
        int metadataXMLColumnsCount = xmlElements.size();
        // Number of attributes in schema.xsd
        int tableXSDColumnsCount = xsdElements.size();
        // Counter. In order to prevent indexOutOfBorder errors
        // columnsCounter is assigned to the smaller
        // number of columns
        int columnCount = (metadataXMLColumnsCount >= tableXSDColumnsCount) ? tableXSDColumnsCount
                : metadataXMLColumnsCount;
        validTable = true;
        // Verify whether the number of column elements in XML and XSD are
        // equal
        for (int i = 0; i < columnCount; i++) {
            Element xmlElement = xmlElements.get(i);
            Element xsdElement = xsdElements.get(i);
            // Retrieve the Elements name
            String xmlTypeElementName = properties.getProperty("module.e.siard.metadata.xml.type.element.name");
            String xsdTypeAttribute = properties.getProperty("module.e.siard.table.xsd.type.attribute.name");
            String xsdAttribute = properties.getProperty("module.e.siard.table.xsd.attribute.name");
            String columnName = xsdElement.getAttributeValue(xsdAttribute);
            // Retrieve the original column type from metadata.xml
            String leftSide = xmlElement.getChild(xmlTypeElementName, validationContext.getXmlNamespace())
                    .getValue();
            // Retrieve the original column type from table.xsd
            String rightSide = xsdElement.getAttributeValue(xsdTypeAttribute);
            String delimiter = properties
                    .getProperty("module.e.attribute.type.validator.original.type.delimiter");
            // Trim the column types - eliminates the brackets and specific
            // numeric parameters
            String trimmedExpectedType = trimLeftSideType(leftSide, delimiter);
            // Designing expected column type in table.xsd, called
            // "rightSide"
            String expectedType = properties.getProperty(trimmedExpectedType);
            // In case the expected type does not exist
            if (expectedType == null) {
                expectedType = properties.getProperty("module.e.type.validator.unknown.type");
                validTable = false;
                validDatabase = false;
                namesOfInvalidColumns.append((namesOfInvalidColumns.length() > 0) ? ", " : "");
                namesOfInvalidColumns.append(columnName + "{" + expectedType + "}");
            } else if (!expectedType.equalsIgnoreCase(rightSide) && expectedType != null) {
                validTable = false;
                validDatabase = false;
                namesOfInvalidColumns.append((namesOfInvalidColumns.length() > 0) ? ", " : "");
                namesOfInvalidColumns.append(columnName);
            }
            // Convey the column types for the all over sequence test [E.4]
            xmlElementSequence.add(expectedType);
            xsdElementSequence.add(rightSide);
        }
        if (validTable == false) {
            namesOfInvalidTables.append((namesOfInvalidTables.length() > 0) ? ", " : "");
            namesOfInvalidTables.append(siardTable.getTableName());
            namesOfInvalidTables.append(properties.getProperty("module.e.siard.table.xsd.file.extension"));
            namesOfInvalidTables.append("(");
            namesOfInvalidTables.append(namesOfInvalidColumns);
            namesOfInvalidTables.append(")");
            namesOfInvalidColumns = null;
            namesOfInvalidColumns = new StringBuilder();
        }
    }
    this.setIncongruentColumnType(namesOfInvalidTables);
    // Save the allover column elements for [E.4]
    validationContext.setXmlElementsSequence(xmlElementSequence);
    validationContext.setXsdElementsSequence(xsdElementSequence);
    this.setValidationContext(validationContext);
    // Return the current validation state
    return validDatabase;
}

From source file:ch.unifr.diuf.diva.did.commands.AbstractCommand.java

License:Open Source License

protected String getAttribute(Element e, String argName) {
    String arg = e.getAttributeValue(argName);
    if (arg == null) {
        illegalArgument("argument " + argName + " needed");
    }//  w  w w.j  av a 2s . c  o  m
    return script.preprocess(arg);
}

From source file:ch.unifr.diuf.diva.did.commands.AbstractGradientManipulation.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    // Read the noise level
    float boost = 1;

    String imgName = script.preprocess(task.getAttributeValue("ref"));
    if (imgName == null) {
        throw new IllegalArgumentException("\n" + commandName + ": requires a ref");
    }/*from  ww w  . j a  va  2  s. c  o  m*/
    if (!script.getImages().containsKey(imgName)) {
        throw new IllegalArgumentException("\n" + commandName + ": cannot find image " + imgName);
    }
    Image image = script.getImages().get(imgName);

    // Read additional parameters
    int nbSteps = 500;
    float density = 1;
    final int SINGLE_CORE = 0;
    final int MULTI_CORES = 1;
    final int GPU = 2;
    int algoType = MULTI_CORES;
    int currentGPU = 0;
    for (Element child : task.getChildren()) {
        if (child.getName().equals("iterations")) {
            nbSteps = Integer.parseInt(script.preprocess(child.getText()));
            continue;
        }

        if (child.getName().equals("multi-core")) {
            algoType = MULTI_CORES;
            continue;
        }

        if (child.getName().equals("single-core")) {
            algoType = SINGLE_CORE;
            continue;
        }

        if (child.getName().equalsIgnoreCase("gpu")) {
            algoType = GPU;
            if (child.getText() != null && !child.getText().equals("")) {
                currentGPU = Integer.parseInt(child.getText());
            }
            continue;
        }
    }

    GradientMap[] grad = { new GradientMap(image, 0), new GradientMap(image, 1), new GradientMap(image, 2) };

    modifyGradient(task, grad, image);

    System.out.println("Starting reconstruction");
    for (int lvl = 0; lvl < 3; lvl++) {
        switch (algoType) {
        case SINGLE_CORE:
            grad[lvl].CPUReconstruct(nbSteps);
            break;
        case MULTI_CORES:
            grad[lvl].MultiCPUReconstruct(nbSteps);
            break;
        case GPU:
            grad[lvl].GPUReconstruct(currentGPU, nbSteps);
            break;
        }
    }
    for (int lvl = 0; lvl < 3; lvl++) {
        grad[lvl].pasteValues(image, lvl);
        grad[lvl] = null;
        System.gc();
        Thread.yield();
    }
    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.Alias.java

License:Open Source License

@Override
public float execute(Element e) throws IOException, JDOMException {
    String id = e.getAttributeValue("id");
    if (id == null) {
        throw new IllegalArgumentException("<alias> needs an id");
    }/*from ww  w.j  av a2 s.  c  o m*/

    if (e.getAttributeValue("value") == null) {
        if (!script.aliasExists(id)) {
            throw new IllegalStateException("The alias " + id + " has not been defined");
        }
    } else {
        String val = getAttribute(e, "value");
        script.setAlias(id, val);
    }

    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.Calc.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    float val = getAttributeFloat(task, "start");

    for (Element child : task.getChildren()) {
        if (child.getName().equals("add")) {
            val += getFloat(child);
            continue;
        }//  w  ww.j  a va 2 s.com

        if (child.getName().equals("sub")) {
            val -= getFloat(child);
            continue;
        }

        if (child.getName().equals("multiply-by")) {
            val *= getFloat(child);
            continue;
        }

        if (child.getName().equals("divide-by")) {
            val /= getFloat(child);
            continue;
        }

        if (child.getName().equals("sqrt")) {
            val = (float) Math.sqrt(val);
            continue;
        }

        if (child.getName().equals("log")) {
            float base = (float) Math.exp(1);
            if (child.getAttributeValue("base") != null) {
                base = Float.parseFloat(child.getAttributeValue("base"));
            }
            val = (float) (Math.log(val) / Math.log(base));
            continue;
        }

        if (child.getName().equals("exp")) {
            val = (float) Math.exp(val);
            continue;
        }

        if (child.getName().equals("pow")) {
            float pow = getFloat(child);
            val = (float) Math.pow(val, pow);
            continue;
        }
    }

    return val;
}

From source file:ch.unifr.diuf.diva.did.commands.DecreaseInkContrast.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    String refName = task.getAttributeValue("ref");
    if (refName == null) {
        throw new IllegalArgumentException("\n" + commandName + ": ref is required");
    }//  w  w w.  j  av a 2s. co m
    refName = script.preprocess(refName);

    if (!script.getImages().containsKey(refName)) {
        throw new IllegalArgumentException("\n" + commandName + ": cannot find ref image " + refName);
    }
    Image img = script.getImages().get(refName);

    float strength = this.getChildFloat(task, "strength");
    float threshold = this.getChildFloat(task, "threshold");
    float randomness = this.getChildFloat(task, "random");

    int heightMapSize = 2;
    while (heightMapSize < img.getWidth() && heightMapSize < img.getHeight()) {
        heightMapSize *= 2;
    }
    Heightmap heightmap = new Heightmap(heightMapSize);
    heightmap.diamondSquare();

    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            float grey = 0;
            for (int c = 0; c < img.getDepth(); c++) {
                grey += img.get(c, x, y);
            }
            grey /= img.getDepth();
            if (grey > threshold) {
                continue;
            }
            float dist = threshold - grey;
            float dist2 = dist * (float) Math.exp(-dist * dist * strength)
                    * (1 - randomness * heightmap.get(x, y));
            float targ = threshold - dist2;
            float ratio = targ / grey;
            for (int c = 0; c < img.getDepth(); c++) {
                float v = ratio * img.get(c, x, y);
                img.set(c, x, y, v);
            }
        }
    }

    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.DeleteImage.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    String refName = task.getAttributeValue("ref");
    if (refName == null) {
        throw new IllegalArgumentException("\n" + commandName + ": ref is required");
    }//from  w  w  w  .j a v a 2 s . c o  m
    refName = script.preprocess(refName);

    if (!script.getImages().containsKey(refName)) {
        throw new IllegalArgumentException("\n" + commandName + ": cannot find ref image " + refName);
    }

    script.getImages().remove(refName);

    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.GaussianNoise.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException, InterruptedException {
    String refName = task.getAttributeValue("ref");
    if (refName == null) {
        throw new IllegalArgumentException("\n" + commandName + ", <gaussian-noise>: ref is required");
    }/* w  ww.  ja  v a  2 s .c  om*/
    refName = script.preprocess(refName);

    if (!script.getImages().containsKey(refName)) {
        throw new IllegalArgumentException(
                "\n" + commandName + ", <gaussian-noise>: cannot find ref image " + refName);
    }
    Image img = script.getImages().get(refName);

    float noiseQuantity = this.getChildFloat(task, "strength");

    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            for (int d = 0; d < img.getDepth(); d++) {
                float offset = (float) (noiseQuantity * Math.sqrt(2 * Math.log(1 / Math.random())));
                float sign = (Math.random() < 0.5) ? -1 : 1;
                float color = img.get(d, x, y) + offset * sign;
                img.set(d, x, y, color);
            }
        }
    }
    return 0;
}

From source file:ch.unifr.diuf.diva.did.commands.ImageCreator.java

License:Open Source License

@Override
public float execute(Element task) throws IOException, JDOMException {
    String id = getAttribute(task, "id");
    if (id == null) {
        throw new IllegalArgumentException("\n" + commandName + ": requires an id");
    }/*from www . j a  v  a 2  s. co m*/

    boolean alreadyInitialized = false;
    Image img = null;
    for (Element param : task.getChildren()) {
        if (param.getName().equals("load")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ": cannot be created twice");
            }
            String fname = param.getAttributeValue("file");
            img = loadImage(fname);
            alreadyInitialized = true;
            continue;
        }

        if (param.getName().equals("copy")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice");
            }
            String otherName = param.getAttributeValue("ref");
            img = copyImage(otherName);
            alreadyInitialized = true;
            continue;
        }

        if (param.getName().equals("diff")) {
            if (alreadyInitialized) {
                throw new IllegalArgumentException("\n" + commandName + ", <image> cannot be created twice");
            }
            String a = getChildString(param, "a");
            String b = getChildString(param, "b");
            Image ia = script.getImages().get(a);
            Image ib = script.getImages().get(b);
            img = new Image(ia.getWidth(), ia.getHeight());
            for (int x = 0; x < ia.getWidth(); x++) {
                for (int y = 0; y < ia.getHeight(); y++) {
                    img.set(0, x, y, ia.get(0, x, y) - ib.get(0, x, y));
                    img.set(1, x, y, ia.get(1, x, y) - ib.get(1, x, y));
                    img.set(2, x, y, ia.get(2, x, y) - ib.get(2, x, y));
                }
            }
            alreadyInitialized = true;
            continue;
        }
    }
    script.getImages().put(id, img);
    return 0;
}