Example usage for org.jdom2 Element getName

List of usage examples for org.jdom2 Element getName

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the (local) name of the element (without any namespace prefix).

Usage

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.ScriptNormalizer.java

License:Apache License

private void normalizeMsubsup(final Element element, Collection<String> firstChildren) {
    assert element != null && firstChildren != null;
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i);
        if (actual.getName().equals(SUBSUP)) {
            final List<Element> actualChildren = actual.getChildren();
            if (actualChildren.size() != 3) {
                LOGGER.info("Invalid msubsup, skipped");
                continue;
            }/*from w w  w  .ja v  a  2 s  .c  o m*/
            if (!firstChildren.contains(actualChildren.get(0).getName())) {
                continue;
            }
            final Element newMsub = new Element(SUBSCRIPT);
            newMsub.addContent(actualChildren.get(0).detach());
            newMsub.addContent(actualChildren.get(0).detach());
            final Element newMsup = new Element(SUPERSCRIPT);
            newMsup.addContent(newMsub);
            newMsup.addContent(actualChildren.get(0).detach());
            children.set(i, newMsup);
            i--; // move back to check the children of the new transformation
            LOGGER.fine("Msubsup converted to nested msub and msup");
        } else {
            normalizeMsubsup(actual, firstChildren);
        }
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.ScriptNormalizer.java

License:Apache License

private void replaceDescendants(final Element ancestor, final Map<String, String> map) {
    assert ancestor != null && map != null;
    final List<Element> toReplace = new ArrayList<Element>();
    for (Element element : ancestor.getDescendants(new ElementFilter())) {
        if (map.containsKey(element.getName())) {
            toReplace.add(element);//from w  w w  . j a va 2s  .  c  o m
        }
    }
    for (Element element : toReplace) {
        replaceElement(element, map.get(element.getName()));
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.UnaryOperatorRemover.java

License:Apache License

private void removeUnaryOperator(final Element rootElem) {

    assert rootElem != null;

    /* Presentation MathML */
    final Set<String> pmCharsToRemove = getPropertySet(PM_UNARY_OPERATORS_TO_REMOVE);

    if (!pmCharsToRemove.isEmpty()) {

        // Unary operators
        List<Element> pmElemsToRemove = xpPMUnaryOperators.evaluate(rootElem);
        for (Element toRemove : pmElemsToRemove) {
            if (pmCharsToRemove.contains(toRemove.getValue())) {
                LOGGER.finest("Removing element '" + toRemove.getQualifiedName() + "' with value '"
                        + toRemove.getValue() + "'.");
                toRemove.detach();//w  w w  . j a v  a  2s.co  m
            } else {
                LOGGER.finest("Skipping element '" + toRemove.getQualifiedName() + "' with value '"
                        + toRemove.getValue() + "'.");
            }
        }

        // Second of the double operators
        pmElemsToRemove = xpPMSecondOperatorInDoubleOperators.evaluate(rootElem);
        for (Element toRemove : pmElemsToRemove) {
            if (pmCharsToRemove.contains(toRemove.getValue())) {
                LOGGER.finest("Removing the second element out of double elements '"
                        + toRemove.getQualifiedName() + "' with value '" + toRemove.getValue() + "'.");
                toRemove.detach();
            } else {
                LOGGER.finest("Skipping the second element out of double elements '"
                        + toRemove.getQualifiedName() + "' with value '" + toRemove.getValue() + "'.");
            }
        }

    }

    LOGGER.finer("RemoveUnaryOperator Presentation MathML finished");

    /* Content MathML */
    List<Element> applyWithTwoChildrens = xpCMApplyWithTwoChildrens.evaluate(rootElem);
    final Set<String> cmOperatorsToRemove = getPropertySet(CM_UNARY_OPERATORS_TO_REMOVE);

    for (Element applyElem : applyWithTwoChildrens) {
        Element operator = applyElem.getChildren().get(0);
        if (cmOperatorsToRemove.contains(operator.getName())) {
            Element operand = applyElem.getChildren().get(1);
            LOGGER.finest("Removing operator '" + operator.getQualifiedName() + "' for operand '"
                    + operand.getQualifiedName() + "'.");
            operand.detach();
            Element parent = applyElem.getParentElement();
            int applyElemIndex = parent.indexOf(applyElem);
            parent.setContent(applyElemIndex, operand);
            applyElem.detach();
        }
    }

    LOGGER.finer("RemoveUnaryOperator Content MathML finished");

}

From source file:cz.natur.cuni.mirai.math.meta.MetaModel.java

License:Open Source License

private static String getStringAttribute(String attrName, Element element) throws Exception {
    String attrValue = element.getAttributeValue(attrName);
    if (attrValue == null)
        throw new Exception(element.getName() + " is null.");
    return attrValue;
}

From source file:cz.natur.cuni.mirai.math.meta.MetaModel.java

License:Open Source License

private static int getIntAttribute(String attrName, Element element) throws Exception {
    String attrValue = getStringAttribute(attrName, element);
    int res = 0;/*from   w  w  w. ja v  a2  s. c o m*/
    try {
        res = Integer.parseInt(attrValue);
    } catch (NumberFormatException e) {
        throw new Exception(element.getName() + " has invalid value.");
    }
    return res;
}

From source file:cz.natur.cuni.mirai.math.meta.MetaModel.java

License:Open Source License

private static char getCharAttribute(String attrName, Element element) throws Exception {
    String attrValue = getStringAttribute(attrName, element);
    char res = 0;
    try {/*ww w .  ja  va  2s  .c o  m*/
        res = attrValue.length() > 0 ? attrValue.charAt(0) : 0;
    } catch (NumberFormatException e) {
        throw new Exception(element.getName() + " has invalid value.");
    }
    return res;
}

From source file:cz.pecina.retro.cpu.Block.java

License:Open Source License

@Override
public void unmarshal(final Element block) {
    assert block.getName().equals(tagName);
    processContent(block);//  w  ww  .j  a  va  2 s .c o  m
    log.fine("Block '" + name + "' processed");
}

From source file:cz.pecina.retro.cpu.Register.java

License:Open Source License

@Override
public void unmarshal(final Element descriptor) {
    assert descriptor.getName().equals(tagName);
    final String value = descriptor.getTextTrim();
    processValue(value);/*from   www  . ja  v  a 2s  .  co m*/
    log.fine("Register '" + name + "' unmarshalled to value '" + value + "'");
}

From source file:cz.pecina.retro.memory.Snapshot.java

License:Open Source License

/**
 * Reads snapshot from a file and sets hardware accordingly.
 *
 * @param file input file//from  w  w  w.  j  a  v a2s  . c  o m
 */
public void read(final File file) {
    log.fine("Reading snapshot from a file, file: " + file.getName());
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(
                        getClass().getResourceAsStream("snapshot-" + SNAPSHOT_XML_FILE_VERSION + ".xsd")))
                .newValidator().validate(new StreamSource(file));
    } catch (final Exception exception) {
        log.fine("Error, validation failed, exception: " + exception.getMessage());
        throw Application.createError(this, "validation");
    }
    Document doc;
    Element snapshot;
    try {
        doc = new SAXBuilder().build(file);
    } catch (final JDOMException exception) {
        log.fine("Error, parsing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "parsing");
    } catch (final Exception exception) {
        log.fine("Error, reading failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLRead");
    }
    try {
        snapshot = doc.getRootElement();
    } catch (final Exception exception) {
        log.fine("Error, parsing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "parsing");
    }
    if (!snapshot.getName().equals("snapshot")) {
        log.fine("Error, parsing failed, no <snapshot> tag");
        throw Application.createError(this, "parsing");
    }
    if (!SNAPSHOT_XML_FILE_VERSION.equals(snapshot.getAttributeValue("version"))) {
        log.fine("Version mismatch");
        throw Application.createError(this, "version");
    }
    hardware.unmarshal(snapshot);
    log.fine("Reading completed");
}

From source file:cz.pecina.retro.memory.XML.java

License:Open Source License

/**
 * Reads XML data from a file and stores it in memory.
 *
 * @param  file               input file
 * @param  destinationAddress destination address ({@code -1} = none)
 * @return info record/* w w w  . j av  a2s .  c om*/
 */
public Info read(final File file, final int destinationAddress) {
    log.fine(String.format("Reading XML data from a file, file: %s, destination address: %04x", file.getName(),
            destinationAddress));
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(
                        getClass().getResourceAsStream("memory-" + MEMORY_XML_FILE_VERSION + ".xsd")))
                .newValidator().validate(new StreamSource(file));
    } catch (final Exception exception) {
        log.fine("Error, validation failed, exception: " + exception.getMessage());
        throw Application.createError(this, "validation");
    }
    Document doc;
    try {
        doc = new SAXBuilder().build(file);
    } catch (final JDOMException exception) {
        log.fine("Error, parsing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "parsing");
    } catch (final Exception exception) {
        log.fine("Error, reading failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLRead");
    }
    Element tag;
    try {
        tag = doc.getRootElement();
    } catch (final Exception exception) {
        log.fine("Error, parsing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "parsing");
    }
    if (!tag.getName().equals("memory")) {
        log.fine("Error, parsing failed, no <memory> tag");
        throw Application.createError(this, "parsing");
    }
    if (!MEMORY_XML_FILE_VERSION.equals(tag.getAttributeValue("version"))) {
        log.fine("Version mismatch");
        throw Application.createError(this, "version");
    }
    final Info info = Snapshot.processBlockElement(destinationMemory, tag, destinationAddress);
    if (info.number == 0) {
        log.fine("Reading completed, with info: number: 0");
    } else {
        log.fine(String.format("Reading completed, with info: number: %d, min: %04x, max: %04x", info.number,
                info.minAddress, info.maxAddress));
    }
    return info;
}