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:com.github.lindenb.jvarkit.util.command.Command.java

protected void writeHtmlDoc(final XMLStreamWriter w) throws XMLStreamException {
    w.writeStartElement("div");
    w.writeComment("no doc available for " + getName());
    w.writeEndElement();//from ww  w.  ja  va  2  s . c om
}

From source file:com.norconex.committer.core.AbstractMappedCommitter.java

@SuppressWarnings("deprecation")
@Override/*w  ww.  j a  v a2s .c  o m*/
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("committer");
        writer.writeAttribute("class", getClass().getCanonicalName());

        if (sourceReferenceField != null) {
            writer.writeStartElement("sourceReferenceField");
            writer.writeAttribute("keep", Boolean.toString(keepSourceReferenceField));
            writer.writeCharacters(sourceReferenceField);
            writer.writeEndElement();
        }
        if (targetReferenceField != null) {
            writer.writeStartElement("targetReferenceField");
            writer.writeCharacters(targetReferenceField);
            writer.writeEndElement();
        }
        if (sourceContentField != null) {
            writer.writeStartElement("sourceContentField");
            writer.writeAttribute("keep", Boolean.toString(keepSourceContentField));
            writer.writeCharacters(sourceContentField);
            writer.writeEndElement();
        }
        if (targetContentField != null) {
            writer.writeStartElement("targetContentField");
            writer.writeCharacters(targetContentField);
            writer.writeEndElement();
        }
        if (getQueueDir() != null) {
            writer.writeStartElement("queueDir");
            writer.writeCharacters(getQueueDir());
            writer.writeEndElement();
        }
        writer.writeStartElement("queueSize");
        writer.writeCharacters(ObjectUtils.toString(getQueueSize()));
        writer.writeEndElement();

        writer.writeStartElement("commitBatchSize");
        writer.writeCharacters(ObjectUtils.toString(getCommitBatchSize()));
        writer.writeEndElement();

        writer.writeStartElement("maxRetries");
        writer.writeCharacters(ObjectUtils.toString(getMaxRetries()));
        writer.writeEndElement();

        writer.writeStartElement("maxRetryWait");
        writer.writeCharacters(ObjectUtils.toString(getMaxRetryWait()));
        writer.writeEndElement();

        saveToXML(writer);

        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:de.qucosa.webapi.v1.SearchResource.java

private ResponseEntity<String> scrollAndBuildResultList(SearchResponse searchResponse)
        throws XMLStreamException, ParseException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);

    SearchHits searchHits = searchResponse.getHits();

    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    {//w  w  w .  ja  v  a  2s .  co m
        w.writeStartElement("SearchResult");
        {
            w.writeStartElement("Search");
            w.writeAttribute("hits", String.valueOf(searchHits.getTotalHits()));
            w.writeEndElement();
            w.writeStartElement("ResultList");
            w.writeNamespace(XLINK_NAMESPACE_PREFIX, XLINK_NAMESPACE);

            SearchResponse scrollResponse = searchResponse;
            int hitcount = 0;
            while (true) {
                hitcount = writeResultElements(w, searchHits, hitcount);
                scrollResponse = elasticSearchClient.prepareSearchScroll(scrollResponse.getScrollId())
                        .setScroll(new TimeValue(60, TimeUnit.SECONDS)).execute().actionGet();
                searchHits = scrollResponse.getHits();
                if (searchHits.getHits().length == 0) {
                    log.debug("Stop scrolling at hitcount: {}", hitcount);
                    break;
                }
            }

            w.writeEndElement();
        }
        w.writeEndElement();
    }
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();

    log.debug(sw.toString());

    return new ResponseEntity<>(sw.toString(), HttpStatus.OK);
}

From source file:de.qucosa.webapi.v1.SearchResource.java

private int writeResultElements(XMLStreamWriter w, SearchHits searchHits, int starthitcount)
        throws XMLStreamException, ParseException {
    int i = starthitcount;
    for (SearchHit hit : searchHits) {
        w.writeStartElement("Result");
        w.writeAttribute("number", String.valueOf(i++));

        String docid = mapToQucosaId(head(values(hit.field(searchFieldnameMap.get("docid")))));

        w.writeAttribute("docid", docid);
        w.writeAttribute(XLINK_NAMESPACE, "href", getHrefLink(docid));
        w.writeAttribute("title", head(values(hit.field(searchFieldnameMap.get("title")))));
        w.writeAttribute("author", join(values(hit.field(searchFieldnameMap.get("author")))));
        w.writeAttribute("year", "");
        w.writeAttribute("completeddate",
                mapToQucosaDate(head(values(hit.field(searchFieldnameMap.get("completeddate"))))));
        w.writeAttribute("doctype", head(values(hit.field(searchFieldnameMap.get("doctype")))));
        w.writeAttribute("issue", "");
        w.writeEndElement();// w ww . jav a2s  . c o  m
    }
    return i;
}

From source file:com.fiorano.openesb.application.aps.InPortInst.java

public void toJXMLString(XMLStreamWriter writer) throws XMLStreamException {
    //Start InPortInst
    writer.writeStartElement("InPortInst");

    writer.writeAttribute("isSyncRequestType", "" + isSyncRequestType());
    writer.writeAttribute("isDisabled", "" + isDisabled());

    FioranoStackSerializer.writeElement("Name", m_strPortName, writer);

    if (!StringUtils.isEmpty(m_strDscription)) {
        FioranoStackSerializer.writeElement("Description", m_strDscription, writer);
    }// www .ja v a 2  s  .c o  m
    if (m_strXSDRef != null) {
        FioranoStackSerializer.writeElement(PortInstConstants.PORT_XSDREF, m_strXSDRef, writer);

    }

    if (!StringUtils.isEmpty(m_strJavaClass)) {
        FioranoStackSerializer.writeElement("JavaClass", m_strJavaClass, writer);

    }
    if (m_params != null && m_params.size() > 0) {
        Enumeration enums = m_params.elements();

        while (enums.hasMoreElements()) {
            Param param = (Param) enums.nextElement();
            if (!StringUtils.isEmpty(param.getParamName()) && !StringUtils.isEmpty(param.getParamValue())) {
                if (!checkIfDefaultValue(param.getParamName(), param.getParamValue())) {
                    param.toJXMLString(writer);

                }
            }
        }
    }
    //End InPortInst
    writer.writeEndElement();

}

From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.XMLTabularDataConverter.java

@Override
public StreamingOutput createStream(final Result result) {
    StreamingOutput stream = new StreamingOutput() {
        @Override/*from   w  w  w  .ja v  a2 s  . com*/
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            ResultSet rs = null;
            XMLStreamWriter xtw = null;
            try {
                rs = (ResultSet) result.getData();
                rs.load(result.getResultSetLocation());
                XMLOutputFactory xof = XMLOutputFactory.newInstance();
                xtw = xof.createXMLStreamWriter(new OutputStreamWriter(outputStream));
                xtw.writeStartDocument("utf-8", "1.0");
                xtw.writeStartElement("results");
                rs.beforeFirst();
                while (rs.next()) {
                    xtw.writeStartElement("result");
                    for (int i = 0; i < rs.getColumnSize(); i++) {
                        xtw.writeStartElement(rs.getColumn(i).getName().replace(" ", "_"));
                        xtw.writeCharacters(rs.getString(i));
                        xtw.writeEndElement();
                    }
                    xtw.writeEndElement();
                }
                xtw.writeEndElement();
                xtw.writeEndDocument();

                xtw.flush();

            } catch (ResultSetException | PersistableException | XMLStreamException e) {
                log.info("Error creating XML Stream: " + e.getMessage());
            } finally {
                if (xtw != null) {
                    try {
                        xtw.close();
                    } catch (XMLStreamException e) {
                        e.printStackTrace();
                    }
                }
                if (rs != null && !rs.isClosed()) {
                    try {
                        rs.close();
                    } catch (ResultSetException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }

        }
    };
    return stream;
}

From source file:net.solarnetwork.web.support.SimpleXmlHttpMessageConverter.java

private void outputMap(Map<?, ?> map, String name, XMLStreamWriter writer)
        throws IOException, XMLStreamException {
    writeElement(name, null, writer, false);

    // for each entry, write an <entry> element
    for (Map.Entry<?, ?> me : map.entrySet()) {
        String entryName = me.getKey().toString();
        writer.writeStartElement("entry");
        writer.writeAttribute("key", entryName);

        Object value = me.getValue();
        if (value instanceof Collection) {
            // special collection case, we don't add nested element
            for (Object o : (Collection<?>) value) {
                outputObject(o, "value", writer);
            }/*w  w w  .j  a  va2s . c o m*/
        } else {
            outputObject(value, null, writer);
        }

        writer.writeEndElement();
    }

    writer.writeEndElement();
}

From source file:net.landora.video.info.file.FileInfoManager.java

private synchronized void writeCacheFile(File file, Map<String, FileInfo> infoMap) {
    OutputStream os = null;// w w w  . jav a  2  s . c  om
    try {
        os = new BufferedOutputStream(new FileOutputStream(file));
        if (COMPRESS_INFO_FILE) {
            os = new GZIPOutputStream(os);
        }

        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(os);
        writer.writeStartDocument();

        writer.writeStartElement("files");
        writer.writeCharacters("\n");

        for (Map.Entry<String, FileInfo> entry : infoMap.entrySet()) {
            FileInfo info = entry.getValue();

            writer.writeStartElement("file");
            writer.writeAttribute("filename", entry.getKey());

            writer.writeAttribute("ed2k", info.getE2dkHash());
            writer.writeAttribute("length", String.valueOf(info.getFileSize()));
            writer.writeAttribute("lastmodified", String.valueOf(info.getLastModified()));

            if (info.getMetadataSource() != null) {
                writer.writeAttribute("metadatasource", info.getMetadataSource());
            }
            if (info.getMetadataId() != null) {
                writer.writeAttribute("metadataid", info.getMetadataId());
            }
            if (info.getVideoId() != null) {
                writer.writeAttribute("videoid", info.getVideoId());
            }

            writer.writeEndElement();
            writer.writeCharacters("\n");
        }

        writer.writeEndElement();
        writer.writeEndDocument();
        writer.close();

    } catch (Exception e) {
        log.error("Error writing file cache.", e);
    } finally {
        if (os != null) {
            IOUtils.closeQuietly(os);
        }
    }
}

From source file:net.solarnetwork.util.JavaBeanXmlSerializer.java

private void writeElement(String name, Map<?, ?> props, XMLStreamWriter out, boolean close)
        throws XMLStreamException {
    out.writeStartElement(name);
    Map<String, Object> nested = null;
    if (props != null) {
        for (Map.Entry<?, ?> me : props.entrySet()) {
            String key = me.getKey().toString();
            Object val = me.getValue();
            if (propertySerializerRegistrar != null) {
                val = propertySerializerRegistrar.serializeProperty(name, val.getClass(), props, val);
            }/*w ww .j  ava  2 s.  c  o  m*/
            if (val instanceof Date) {
                SimpleDateFormat sdf = SDF.get();
                // SimpleDateFormat has no way to create xs:dateTime with tz,
                // so use trick here to insert required colon for non GMT dates
                Date date = (Date) val;
                StringBuilder buf = new StringBuilder(sdf.format(date));
                if (buf.charAt(buf.length() - 1) != 'Z') {
                    buf.insert(buf.length() - 2, ':');
                }
                val = buf.toString();
            } else if (val instanceof Collection) {
                if (nested == null) {
                    nested = new LinkedHashMap<String, Object>(5);
                }
                nested.put(key, val);
                val = null;
            } else if (val instanceof Map<?, ?>) {
                if (nested == null) {
                    nested = new LinkedHashMap<String, Object>(5);
                }
                nested.put(key, val);
                val = null;
            } else if (classNamesAllowedForNesting != null && !(val instanceof Enum<?>)) {
                for (String prefix : classNamesAllowedForNesting) {
                    if (val.getClass().getName().startsWith(prefix)) {
                        if (nested == null) {
                            nested = new LinkedHashMap<String, Object>(5);
                        }
                        nested.put(key, val);
                        val = null;
                        break;
                    }
                }
            }

            if (val != null) {
                String attVal = val.toString();
                out.writeAttribute(key, attVal);
            }
        }
    }
    if (nested != null) {
        for (Map.Entry<String, Object> me : nested.entrySet()) {
            outputObject(me.getValue(), me.getKey(), out);
        }
        if (close) {
            out.writeEndElement();
        }
    }
}

From source file:DDTReporter.java

private void writeStartElement(XMLStreamWriter writer, String name) throws XMLStreamException {
    writer.writeStartElement(name);
    writer.writeCharacters("\n");
}