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:cz.lbenda.dbapp.rc.SessionConfiguration.java

License:Apache License

/** Load schemas which will be showed */
private void loadSchemas(final Element element) {
    shownSchemas.clear();/*from   ww w .  j av  a2 s .c  o m*/
    for (Element sch : element.getChildren("schema")) {
        String catalog = sch.getAttributeValue("catalog");
        String schema = sch.getAttributeValue("schema");
        List<String> list = shownSchemas.get(catalog);
        if (list == null) {
            list = new ArrayList<>();
            shownSchemas.put(catalog, list);
        }
        list.add(schema);
    }
}

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.pecina.retro.cpu.Device.java

License:Open Source License

/**
 * Loads a representation of the {@code Device} from 
 * a JDOM {@code Element}./*from www . j  ava2  s.co  m*/
 * <p>
 * Note: The current implementation ignores devices the computer
 * does not have.  This is a controversial design decision, which
 * may be revised in the future.
 *
 * @param hardware {@code Element} to be loaded
 */
public void unmarshal(final Element hardware) {
    log.fine("Unmarshalling device: " + name);
    for (Element device : hardware.getChildren()) {
        if (device.getAttributeValue("name").equals(name)) {
            preUnmarshal();
            for (Element descriptorTag : device.getChildren()) {
                for (Descriptor descriptor : this) {
                    if (descriptor.getName().equals(descriptorTag.getAttributeValue("name"))) {
                        log.finest("Unmarshalling '" + descriptor.getName() + "'");
                        descriptor.unmarshal(descriptorTag);
                        break;
                    }
                }
            }
            postUnmarshal();
            break;
        }
    }
}

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  a 2 s  . co  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.Snapshot.java

License:Open Source License

/**
 * Processes a block tag.// w ww.j  a va2  s  .  c  om
 *
 * @param  memory             memory array
 * @param  tag                tag to process
 * @param  destinationAddress destination address ({@code -1} = none)
 * @return info               info record
 */
public static Info processBlockElement(final byte[] memory, final Element tag, final int destinationAddress) {
    log.finer(
            String.format("Method processMemoryElement called: destination address: %04x", destinationAddress));
    assert (destinationAddress >= -1) && (destinationAddress <= 0xffff);
    final int size = memory.length;
    final Info info = new Info();
    int startAddress = 0;
    if (destinationAddress == -1) {
        try {
            startAddress = Integer.parseInt(tag.getAttributeValue("start"), 16);
        } catch (final Exception exception) {
            log.fine("Error in starting address, exception: " + exception.getMessage());
            throw Application.createError(Snapshot.class, "parsing");
        }
        log.finer(String.format("Starting address: %04x", startAddress));
    } else {
        startAddress = destinationAddress;
        log.finer(String.format("Destination address used instead: %04x", startAddress));
    }
    for (Element bytes : tag.getChildren(subtagName)) {
        int count;
        String string;
        if ((string = bytes.getAttributeValue("count")) != null) {
            try {
                count = Integer.parseInt(string);
            } catch (final Exception exception) {
                log.fine("Error in count, exception: " + exception.getMessage());
                throw Application.createError(Snapshot.class, "parsing");
            }
        } else {
            count = 1;
        }
        string = bytes.getTextTrim();
        try {
            for (; count > 0; count--) {
                for (int i = 0; i < (string.length() / 2); i++) {
                    startAddress &= 0xffff;
                    if (startAddress < info.minAddress) {
                        info.minAddress = startAddress;
                    }
                    if (startAddress > info.maxAddress) {
                        info.maxAddress = startAddress;
                    }
                    final int dataByte = Integer.parseInt(string.substring(i * 2, (i + 1) * 2), 16);
                    memory[startAddress % size] = (byte) dataByte;
                    log.finest(String.format("Read: %02x -> (%04x)", dataByte, startAddress));
                    startAddress++;
                    info.number++;
                }
            }
        } catch (final Exception exception) {
            log.fine("Error, parsing failed, exception: " + exception.getMessage());
            throw Application.createError(Snapshot.class, "parsing");
        }
    }
    log.finer("Method processMemoryElement finished");
    return info;
}

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/*from  www .j a  v  a 2  s. c o m*/
 */
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;
}

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

License:Open Source License

/**
 * Reads the tape from an XML file.//from  www  .  ja  v  a 2  s  . co  m
 *
 * @param file input file
 */
public void read(final File file) {
    log.fine("Reading tape data from an XML file, file: " + file);
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(
                        getClass().getResourceAsStream("tape-" + TAPE_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("tape")) {
        log.fine("Error, parsing failed, no <tape> tag");
        throw Application.createError(this, "noTape");
    }
    if (!TAPE_XML_FILE_VERSION.equals(tag.getAttributeValue("version"))) {
        log.fine("Version mismatch");
        throw Application.createError(this, "version");
    }
    if (!"per sec".equals(tag.getAttributeValue("unit"))) {
        log.fine("Unsupported sample rate");
        throw Application.createError(this, "XMLSampleRate");
    }
    tape.clear();
    try {
        long currPos = -1;
        for (Element pulse : tag.getChildren("pulse")) {
            final long start = Long.parseLong(pulse.getAttributeValue("start"));
            final long duration = Long.parseLong(pulse.getAttributeValue("duration"));
            if ((start <= currPos) || (duration <= 0)) {
                log.fine("Error in XML file");
                throw Application.createError(this, "XML");
            }
            tape.put(start, duration);
            log.finest(String.format("Read: (%d, %d)", start, duration));
            currPos = start;
        }
    } catch (final Exception exception) {
        log.fine("Error, parsing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "parsing");
    }
    log.fine("Reading completed");
}

From source file:DataWeb.Code.java

License:Open Source License

public Code(Element e, Namespace ns) {
    id = e.getAttributeValue("id");
    idProfesor = e.getChildText("idProfesor", ns);
    nombre = e.getChildText("nombre", ns);
    lenguaje = e.getChildText("lenguaje", ns);
    resaltar = e.getChildText("resaltar", ns);

    linea = new ArrayList<String>();
    comentario = new ArrayList<String>();
    for (Element line : e.getChildren("linea", ns))
        linea.add(line.getText());//  ww w  .  ja v a  2s.  c o  m
    for (Element line : e.getChildren("idComentario", ns))
        comentario.add(line.getText());
}

From source file:DataWeb.Comment.java

License:Open Source License

public Comment(Element e, Namespace ns) {
    id = e.getAttributeValue("id");
    idUsuario = e.getChildText("idUsuario", ns);
    texto = e.getChildText("texto", ns);
    calificacion = Integer.parseInt(e.getChildText("calificacion", ns));
    fechaMod = e.getChildText("fechaMod", ns);
}

From source file:DataWeb.Course.java

License:Open Source License

public Course(Element e, Namespace ns) {
    id = e.getAttributeValue("id");
    idProfesor = e.getChildText("idProfesor", ns);
    nombre = e.getChildText("nombre", ns);

    idAlumno = new ArrayList<String>();
    idCodigo = new ArrayList<String>();
    for (Element alumno : e.getChildren("idAlumno", ns))
        idAlumno.add(alumno.getText());//from  ww  w  .j a v  a 2  s. com
    for (Element codigo : e.getChildren("idCodigo", ns))
        idCodigo.add(codigo.getText());
}