Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

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

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:cz.muni.fi.pb138.scxml2voicexmlj.voicexml.XsltStateStackConverter.java

@Override
public String convert(InputStream scxmlContent, GrammarReference srgsReferences) {
    Document vxml = emptyVxmlDocument();
    Document scxml = helper.parseStream(scxmlContent);
    Element form = vxml.getRootElement().getChild("form", NS_VXML);
    appendGrammarFile(form, srgsReferences);
    List<Element> states = new ArrayList<>(scxml.getRootElement().getChildren("state", NS_SCXML));
    states.addAll(scxml.getRootElement().getChildren("final", NS_SCXML));
    String initialName = helper.extractAttribute(scxml.getRootElement(), "initial");
    List<Element> transformedVxmlStates = transformStates(assignTransformationsToStates(states, initialName),
            srgsReferences);//from ww  w.  ja  v a  2  s. c  om
    form.addContent(transformedVxmlStates);
    return helper.render(vxml);
}

From source file:cz.muni.fi.pb138.scxml2voicexmlj.voicexml.XsltStateStackConverter.java

/**
 * Append element pointing to file with srgs grammar to the form
 */// ww  w  .  j  av  a  2 s. c o  m
public void appendGrammarFile(Element form, GrammarReference srgsReferences) {
    if (srgsReferences.grammarFile() != null) {
        Element grammar = new Element("grammar", NS_VXML);
        grammar.setAttribute("type", "application/srgs+xml");
        grammar.setAttribute("src", srgsReferences.grammarFile());
        form.addContent(grammar);
    }
}

From source file:cz.muni.fi.pb138.scxml2voicexmlj.voicexml.XsltStateStackConverter.java

/**
 * Append element pointing to reference inside the grammar file to the field
 *///from www. j  av  a 2s.  c  om
public void appendGrammarField(Element field, GrammarReference srgsReferences) {
    String name = helper.extractAttribute(field, "name");
    if (!srgsReferences.stateHasGrammarReference(name)) {
        return;
    }
    String reference = srgsReferences.referenceForState(name);
    Element grammar = new Element("grammar", NS_VXML);
    grammar.setAttribute("src", reference);
    grammar.setAttribute("type", "application/srgs+xml");
    field.addContent(grammar);
}

From source file:cz.muni.fi.pb138.scxml2voicexmlj.voicexml.XsltStateStackConverter.java

/**
 * Performs transformations on every state and adds grammar reference.
 * Remembers which states were already visited so that clear elements for backward navigation can be created.
 *//*ww w. j a va  2s . co  m*/
public List<Element> transformStates(LinkedHashMap<Element, String> statesWithTransforms,
        GrammarReference grammarReference) {
    List<Element> transformed = new ArrayList<>();
    Stack<String> visitedStates = new Stack<>();
    for (Entry<Element, String> stateTransformPair : statesWithTransforms.entrySet()) {
        Element state = stateTransformPair.getKey();
        String transform = stateTransformPair.getValue();

        visitedStates.add(helper.extractAttribute(state, "id"));
        Element field = helper.transformElement(state, transform);
        AssemblerResult<Element> transitionsAssembler = assembleClearsForBackwardTransitions(state,
                visitedStates);
        if (transitionsAssembler.isAvailable()) {
            Element filled = getFilledElementAppendLazyly(field);
            filled.addContent(transitionsAssembler.result());
        }
        appendGrammarField(field, grammarReference);
        transformed.add(field);
    }
    return transformed;
}

From source file:cz.muni.fi.pb138.scxml2voicexmlj.voicexml.XsltStateStackConverter.java

/**
 * Return the {@code <filled>} element of {@code field}.
 * If it didnt exists, create it first// w  ww  .  ja va2  s  .c  o  m
 */
public Element getFilledElementAppendLazyly(Element field) {
    if (field.getChild("filled", NS_VXML) == null) {
        Element filled = new Element("filled", NS_VXML);
        field.addContent(filled);
    }
    return field.getChild("filled", NS_VXML);
}

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

License:Open Source License

/**
 * Gets a representation of the {@code Device} in 
 * a JDOM {@code Element}.//from  w w w  .  j a v  a  2  s  .c o  m
 *
 * @return {@code Element} representing the {@code Device}
 */
public Element marshal() {
    log.fine("Marshalling device: " + name);
    preMarshal();
    final Element device = new Element("device");
    device.setAttribute("name", name);
    for (Descriptor descriptor : this) {
        log.finest("Marshalling '" + descriptor.getName() + "'");
        device.addContent(descriptor.marshal());
    }
    postMarshal();
    return device;
}

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

License:Open Source License

/**
 * Gets a representation of the {@code Hardware} object in 
 * a JDOM {@code Element}.//from   w  w  w  .j ava 2s. co m
 *
 * @param hardware {@code Element} representing the
 * {@code Hardware} object
 */
public void marshal(final Element hardware) {
    log.fine("Marshalling hardware");
    hardware.setAttribute("name", name);
    for (Device device : this) {
        hardware.addContent(device.marshal());
    }
}

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

License:Open Source License

@Override
public Element marshal() {
    final Element register = new Element(tagName);
    register.setAttribute("name", name);
    final String value = getValue();
    register.addContent(value);
    log.fine("Register '" + name + "' holding value '" + value + "' marshalled");
    return register;
}

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

License:Open Source License

/**
 * Builds a block tag.//from  www .  j a va 2  s .com
 *
 * @param memory       memory array
 * @param tag          tag to build
 * @param startAddress starting address
 * @param number       number of bytes
 */
public static void buildBlockElement(final byte[] memory, final Element tag, final int startAddress,
        final int number) {
    log.finer(String.format("Method buildMemoryElement called: start address: %04x," + " number of bytes: %d",
            startAddress, number));
    final int size = memory.length;
    boolean inSequence = false;
    StringBuilder data = new StringBuilder();
    Element bytes;
    for (int i = 0, j = 0; i < number;) {
        final int memoryI = memory[(startAddress + i) % size] & 0xff;
        final int remain = number - i;
        boolean compress = false;
        if (remain >= COUNT_LIMIT) {
            compress = true;
            for (j = 0; j < COUNT_LIMIT; j++) {
                if (memoryI != (memory[(startAddress + i + j) % size] & 0xff)) {
                    compress = false;
                    break;
                }
            }
        }
        if (compress) {
            for (; j < remain; j++) {
                if (memoryI != (memory[(startAddress + i + j) % size] & 0xff)) {
                    break;
                }
            }
            if (inSequence) {
                bytes = new Element(subtagName);
                bytes.addContent(data.toString());
                tag.addContent(bytes);
                data = new StringBuilder();
                log.finest("Data sequence closed");
            }
            inSequence = false;
            bytes = new Element(subtagName);
            bytes.setAttribute("count", String.valueOf(j));
            bytes.addContent(String.format("%02x", memoryI));
            tag.addContent(bytes);
            log.finest(String.format("Repeated data sequence written, count: %d, data: %02x", j, memoryI));
            i += j;
        } else {
            if (!inSequence) {
                data = new StringBuilder();
                log.finest("Unique data sequence started");
            }
            data.append(String.format("%02x", memoryI));
            log.finest(String.format("One byte written: %02x", memoryI));
            inSequence = true;
            i++;
        }
    }
    if (inSequence) {
        bytes = new Element(subtagName);
        bytes.addContent(data.toString());
        tag.addContent(bytes);
        log.finest("Final data sequence closed");
    }
    log.finer("Method buildMemoryElement finished");
}

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

License:Open Source License

/**
 * Writes the tape to an XML file./*from  w  w w .  j a v  a 2s .c  o m*/
 *
 * @param file output file
 */
public void write(final File file) {
    log.fine("Writing tape data to an XML file, file: " + file);
    final Element tag = new Element("tape");
    final Namespace namespace = Namespace.getNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    tag.addNamespaceDeclaration(namespace);
    tag.setAttribute("noNamespaceSchemaLocation",
            Application.XSD_PREFIX + "tape-" + TAPE_XML_FILE_VERSION + ".xsd", namespace);
    tag.setAttribute("version", TAPE_XML_FILE_VERSION);
    tag.setAttribute("rate", String.valueOf(tapeRecorderInterface.tapeSampleRate));
    tag.setAttribute("unit", "per sec");
    try {
        long currPos = -1;
        for (long start : tape.navigableKeySet()) {
            final long duration = tape.get(start);
            log.finest(String.format("Fetched: (%d, %d)", start, duration));
            if ((start > currPos) && (duration > 0)) {
                final Element pulse = new Element("pulse");
                pulse.setAttribute("start", String.valueOf(start));
                pulse.setAttribute("duration", String.valueOf(duration));
                tag.addContent(pulse);
                log.finest(String.format("Write: (%d, %d)", start, duration));
                currPos = start + duration;
            }
        }
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    final Document doc = new Document(tag);
    try (final PrintWriter writer = new PrintWriter(file)) {
        new XMLOutputter(Format.getPrettyFormat()).output(doc, writer);
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    log.fine("Writing completed");
}