Example usage for javax.xml.stream XMLStreamWriter writeStartElement

List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeStartElement.

Prototype

public void writeStartElement(String localName) throws XMLStreamException;

Source Link

Document

Writes a start tag to the output.

Usage

From source file:gdv.xport.util.HtmlFormatter.java

private static void writeTo(final XMLStreamWriter xmlStreamWriter, final Satz satz, final int zeile)
        throws XMLStreamException {
    xmlStreamWriter.writeStartElement("div");
    xmlStreamWriter.writeAttribute("class", "Satz");
    if (satz instanceof Datensatz) {
        Datensatz datensatz = (Datensatz) satz;
        xmlStreamWriter.writeAttribute("title", "Satzart " + datensatz.getSatzartFeld().getInhalt() + "."
                + datensatz.getSparteFeld().getInhalt());
    } else {//  w w w. jav  a 2  s .co m
        xmlStreamWriter.writeAttribute("title", "Satzart " + satz.getSatzartFeld().getInhalt());
    }
    int n = zeile;
    for (Iterator<Teildatensatz> iterator = satz.getTeildatensaetze().iterator(); iterator.hasNext();) {
        Teildatensatz teildatensatz = iterator.next();
        writeTo(xmlStreamWriter, teildatensatz, n);
        if (iterator.hasNext()) {
            xmlStreamWriter.writeCharacters("\n");
        }
        n++;
    }
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    xmlStreamWriter.flush();
}

From source file:gdv.xport.util.HtmlFormatter.java

private static void writeTo(final XMLStreamWriter xmlStreamWriter, final Teildatensatz teildatensatz,
        final int zeile) throws XMLStreamException {
    xmlStreamWriter.writeStartElement("span");
    xmlStreamWriter.writeAttribute("class", "Teildatensatz");
    xmlStreamWriter.writeAttribute("title", "Nr. " + teildatensatz.getNummer().getInhalt());
    int endAdresse = 1;
    for (Iterator<Feld> iterator = teildatensatz.getFelder().iterator(); iterator.hasNext();) {
        Feld feld = iterator.next();/*from w  ww .j  a  v  a 2  s  .c  om*/
        int gap = feld.getByteAdresse() - endAdresse;
        if (gap > 1) {
            Feld undefiniert = new Undefiniert(gap - 1, endAdresse + 1);
            writeTo(xmlStreamWriter, undefiniert, zeile);
        }
        writeTo(xmlStreamWriter, feld, zeile);
        endAdresse = feld.getEndAdresse();
    }
    if (endAdresse < 256) {
        Feld undefiniert = new Undefiniert(256 - endAdresse, endAdresse + 1);
        writeTo(xmlStreamWriter, undefiniert, zeile);
    }
    xmlStreamWriter.writeEndElement();
}

From source file:gdv.xport.util.HtmlFormatter.java

private static void writeDetailsTo(final XMLStreamWriter xmlStreamWriter, final Teildatensatz teildatensatz,
        final int zeile) throws XMLStreamException {
    xmlStreamWriter.writeStartElement("h4");
    xmlStreamWriter//ww w. ja v  a  2  s.co m
            .writeCharacters("Zeile " + zeile + ": Teildatensatz " + teildatensatz.getNummer().getInhalt());
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    xmlStreamWriter.writeStartElement("table");
    xmlStreamWriter.writeStartElement("thead");
    xmlStreamWriter.writeStartElement("tr");
    writeTagTo(xmlStreamWriter, "th", "Nr");
    writeTagTo(xmlStreamWriter, "th", "Byte");
    writeTagTo(xmlStreamWriter, "th", "Bezeichner");
    writeTagTo(xmlStreamWriter, "th", "Datentyp");
    writeTagTo(xmlStreamWriter, "th", "Inhalt");
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    xmlStreamWriter.writeStartElement("tbody");
    int nr = 1;
    for (Iterator<Feld> iterator = teildatensatz.getFelder().iterator(); iterator.hasNext();) {
        Feld feld = iterator.next();
        writeDetailsTo(xmlStreamWriter, feld, zeile, nr);
        nr++;
    }
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    xmlStreamWriter.flush();
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static XMLStreamWriter CreateTagListTuplet(XMLStreamWriter serializer, int i, String key, String value)
        throws XMLStreamException {
    if (value == null || key == null)
        return serializer;
    serializer.writeCharacters("\t");
    serializer.writeStartElement("tag");
    serializer.writeAttribute("id", String.valueOf(i));
    serializer.writeCharacters("\n\t\t");
    serializer = XMLHelper.CreateSimpleTagAndContent(serializer, "name", key);
    serializer.writeCharacters("\n\t\t");
    serializer = XMLHelper.CreateSimpleTagAndContent(serializer, "value", value);
    serializer.writeCharacters("\n\t");
    serializer.writeEndElement(); //end tag
    serializer.writeCharacters("\n");
    return serializer;
}

From source file:gdv.xport.util.HtmlFormatter.java

private static void writeTo(final XMLStreamWriter xmlStreamWriter, final Feld feld, final int zeile)
        throws XMLStreamException {
    String feldType = feld.getClass().getSimpleName();
    xmlStreamWriter.writeStartElement("a");
    xmlStreamWriter.writeAttribute("class", feldType);
    xmlStreamWriter.writeAttribute("title",
            "Byte " + feld.getByteAdresse() + "-" + feld.getEndAdresse() + ": " + feld.getBezeichnung());
    xmlStreamWriter.writeAttribute("href", "#" + getAnchorFor(zeile, feld));
    writeInhaltTo(xmlStreamWriter, feld);
    xmlStreamWriter.writeEndElement();//from   w  w  w. ja  v  a2s.  co  m
}

From source file:gdv.xport.util.HtmlFormatter.java

private static void writeDetailsTo(final XMLStreamWriter xmlStreamWriter, final Feld feld, final int zeile,
        final int nr) throws XMLStreamException {
    String typ = feld.getClass().getSimpleName();
    xmlStreamWriter.writeStartElement("tr");
    xmlStreamWriter.writeAttribute("class", typ);
    writeTagTo(xmlStreamWriter, "td", Integer.toString(nr));
    xmlStreamWriter.writeStartElement("td");
    xmlStreamWriter.writeStartElement("a");
    xmlStreamWriter.writeAttribute("name", getAnchorFor(zeile, feld));
    xmlStreamWriter.writeEndElement();/*from w  w  w .  j a  v  a 2  s .com*/
    if (feld.getAnzahlBytes() == 1) {
        xmlStreamWriter.writeCharacters(Integer.toString(feld.getByteAdresse()));
    } else {
        xmlStreamWriter.writeCharacters(feld.getByteAdresse() + " - " + feld.getEndAdresse());
    }
    xmlStreamWriter.writeEndElement();
    writeTagTo(xmlStreamWriter, "td", feld.getBezeichnung());
    writeTagTo(xmlStreamWriter, "td", typ);
    xmlStreamWriter.writeStartElement("td");
    writeInhaltTo(xmlStreamWriter, feld);
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
}

From source file:eu.scape_project.planning.xml.ProjectExportAction.java

/**
 * Dumps binary data to provided file. It results in an XML file with a
 * single element: data.//from www.jav  a2 s  .  c  o m
 * 
 * @param id
 * @param data
 * @param f
 * @param encoder
 * @throws IOException
 */
private static void writeBinaryData(int id, InputStream data, File f) throws IOException {

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter(f));

        writer.writeStartDocument(PlanXMLConstants.ENCODING, "1.0");
        writer.writeStartElement("data");
        writer.writeAttribute("id", "" + id);

        Base64InputStream base64EncodingIn = new Base64InputStream(data, true,
                PlanXMLConstants.BASE64_LINE_LENGTH, PlanXMLConstants.BASE64_LINE_BREAK);

        OutputStream out = new WriterOutputStream(new XMLStreamContentWriter(writer),
                PlanXMLConstants.ENCODING);
        // read the binary data and encode it on the fly
        IOUtils.copy(base64EncodingIn, out);
        out.flush();

        // all data is written - end 
        writer.writeEndElement();
        writer.writeEndDocument();

        writer.flush();
        writer.close();

    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.flexive.shared.media.impl.FxMediaImageMagickEngine.java

/**
 * Parse an identify stdOut result (from in) and convert it to an XML content
 *
 * @param in identify response/*ww  w .  j a  va  2s.  c om*/
 * @return XML content
 * @throws XMLStreamException on errors
 * @throws IOException        on errors
 */
public static String parse(InputStream in) throws XMLStreamException, IOException {
    StringWriter sw = new StringWriter(2000);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
    writer.writeStartDocument();

    int lastLevel = 0, level, lastNonValueLevel = 1;
    boolean valueEntry;
    String curr = null;
    String[] entry;
    try {
        while ((curr = br.readLine()) != null) {
            level = getLevel(curr);
            if (level == 0 && curr.startsWith("Image:")) {
                writer.writeStartElement("Image");
                entry = curr.split(": ");
                if (entry.length >= 2)
                    writer.writeAttribute("source", entry[1]);
                lastLevel = level;
                continue;
            }
            if (!(valueEntry = pNumeric.matcher(curr).matches())) {
                while (level < lastLevel--)
                    writer.writeEndElement();
                lastNonValueLevel = level;
            } else
                level = lastNonValueLevel + 1;
            if (curr.endsWith(":")) {
                writer.writeStartElement(
                        curr.substring(0, curr.lastIndexOf(':')).trim().replaceAll("[ :]", "-"));
                lastLevel = level + 1;
                continue;
            } else if (pColormap.matcher(curr).matches()) {
                writer.writeStartElement(
                        curr.substring(0, curr.lastIndexOf(':')).trim().replaceAll("[ :]", "-"));
                writer.writeAttribute("colors", curr.split(": ")[1].trim());
                lastLevel = level + 1;
                continue;
            }
            entry = curr.split(": ");
            if (entry.length == 2) {
                if (!valueEntry) {
                    writer.writeStartElement(entry[0].trim().replaceAll("[ :]", "-"));
                    writer.writeCharacters(entry[1]);
                    writer.writeEndElement();
                } else {
                    writer.writeEmptyElement("value");
                    writer.writeAttribute("key", entry[0].trim().replaceAll("[ :]", "-"));
                    writer.writeAttribute("data", entry[1]);
                    //                        writer.writeEndElement();
                }
            } else {
                //                    System.out.println("unknown line: "+curr);
            }
            lastLevel = level;
        }
    } catch (Exception e) {
        LOG.error("Error at [" + curr + "]:" + e.getMessage(), e);
    }
    writer.writeEndDocument();
    writer.flush();
    writer.close();
    return sw.getBuffer().toString();
}

From source file:com.predic8.membrane.annot.bean.MCUtil.java

private static void addXML(Object object, String id, XMLStreamWriter xew, SerializationContext sc)
        throws XMLStreamException {
    if (object == null)
        throw new InvalidParameterException("'object' must not be null.");

    Class<? extends Object> clazz = object.getClass();

    MCElement e = clazz.getAnnotation(MCElement.class);
    if (e == null)
        throw new IllegalArgumentException("'object' must be @MCElement-annotated.");

    BeanWrapperImpl src = new BeanWrapperImpl(object);

    xew.writeStartElement(e.name());

    if (id != null)
        xew.writeAttribute("id", id);

    HashSet<String> attributes = new HashSet<String>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCAttribute a = m.getAnnotation(MCAttribute.class);
        if (a != null) {
            Object value = src.getPropertyValue(propertyName);
            String str;//  w  w  w  .  j a v a2 s.  com
            if (value == null)
                continue;
            else if (value instanceof String)
                str = (String) value;
            else if (value instanceof Boolean)
                str = ((Boolean) value).toString();
            else if (value instanceof Integer)
                str = ((Integer) value).toString();
            else if (value instanceof Long)
                str = ((Long) value).toString();
            else if (value instanceof Enum<?>)
                str = value.toString();
            else {
                MCElement el = value.getClass().getAnnotation(MCElement.class);
                if (el != null) {
                    str = defineBean(sc, value, null, true);
                } else {
                    str = "?";
                    sc.incomplete = true;
                }
            }

            if (a.attributeName().length() > 0)
                propertyName = a.attributeName();

            attributes.add(propertyName);
            xew.writeAttribute(propertyName, str);
        }
    }
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
        if (o != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value instanceof Map<?, ?>) {
                Map<?, ?> map = (Map<?, ?>) value;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    Object key = entry.getKey();
                    Object val = entry.getValue();
                    if (!(key instanceof String) || !(val instanceof String)) {
                        sc.incomplete = true;
                        key = "incompleteAttributes";
                        val = "?";
                    }
                    if (attributes.contains(key))
                        continue;
                    attributes.add((String) key);
                    xew.writeAttribute((String) key, (String) val);
                }
            } else {
                xew.writeAttribute("incompleteAttributes", "?");
                sc.incomplete = true;
            }
        }
    }

    List<Method> childElements = new ArrayList<Method>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));

        MCChildElement c = m.getAnnotation(MCChildElement.class);
        if (c != null) {
            childElements.add(m);
        }
        MCTextContent t = m.getAnnotation(MCTextContent.class);
        if (t != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value == null) {
                continue;
            } else if (value instanceof String) {
                xew.writeCharacters((String) value);
            } else {
                xew.writeCharacters("?");
                sc.incomplete = true;
            }
        }
    }

    Collections.sort(childElements, new Comparator<Method>() {

        @Override
        public int compare(Method o1, Method o2) {
            MCChildElement c1 = o1.getAnnotation(MCChildElement.class);
            MCChildElement c2 = o2.getAnnotation(MCChildElement.class);
            return c1.order() - c2.order();
        }
    });

    for (Method m : childElements) {
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));

        Object value = src.getPropertyValue(propertyName);
        if (value != null) {
            if (value instanceof Collection<?>) {
                for (Object item : (Collection<?>) value)
                    addXML(item, null, xew, sc);
            } else {
                addXML(value, null, xew, sc);
            }
        }
    }

    xew.writeEndElement();
}

From source file:com.flexive.core.IMParser.java

/**
 * Parse an identify stdOut result (from in) and convert it to an XML content
 *
 * @param in identify response//from   w w w .  j av  a  2  s. co m
 * @return XML content
 * @throws XMLStreamException on errors
 * @throws IOException        on errors
 */
public static String parse(InputStream in) throws XMLStreamException, IOException {
    StringWriter sw = new StringWriter(2000);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
    writer.writeStartDocument();

    int lastLevel = 0, level, lastNonValueLevel = 1;
    boolean valueEntry;
    String curr = null;
    String[] entry;
    try {
        while ((curr = br.readLine()) != null) {
            if (curr.indexOf(':') == -1 || curr.trim().length() == 0) {
                System.out.println("skipping: [" + curr + "]");
                continue; //ignore lines without ':'
            }
            level = getLevel(curr);
            curr = curr.trim();
            if (level == 0 && curr.startsWith("Image:")) {
                writer.writeStartElement("Image");
                entry = curr.split(": ");
                if (entry.length >= 2)
                    writer.writeAttribute("source", entry[1]);
                lastLevel = level;
                continue;
            }
            if (!(valueEntry = pNumeric.matcher(curr).matches())) {
                while (level < lastLevel--)
                    writer.writeEndElement();
                lastNonValueLevel = level;
            } else
                level = lastNonValueLevel + 1;
            if (curr.endsWith(":")) {
                writer.writeStartElement(cleanElement(curr.substring(0, curr.lastIndexOf(':'))));
                lastLevel = level + 1;
                continue;
            } else if (pColormap.matcher(curr).matches()) {
                writer.writeStartElement(cleanElement(curr.substring(0, curr.lastIndexOf(':'))));
                writer.writeAttribute("colors", curr.split(": ")[1].trim());
                lastLevel = level + 1;
                continue;
            }
            entry = curr.split(": ");
            if (entry.length == 2) {
                if (!valueEntry) {
                    writer.writeStartElement(cleanElement(entry[0]));
                    writer.writeCharacters(entry[1]);
                    writer.writeEndElement();
                } else {
                    writer.writeEmptyElement("value");
                    writer.writeAttribute("key", cleanElement(entry[0]));
                    writer.writeAttribute("data", entry[1]);
                    //                        writer.writeEndElement();
                }
            } else {
                //                    System.out.println("unknown line: "+curr);
            }
            lastLevel = level;
        }
    } catch (Exception e) {
        System.err.println("Error at [" + curr + "]:" + e.getMessage());
        e.printStackTrace();
    }

    writer.writeEndDocument();
    writer.flush();
    writer.close();
    return sw.getBuffer().toString();
}