Example usage for javax.xml.stream XMLStreamWriter writeAttribute

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

Introduction

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

Prototype

public void writeAttribute(String localName, String value) throws XMLStreamException;

Source Link

Document

Writes an attribute to the output stream without a prefix.

Usage

From source file:com.norconex.collector.http.delay.impl.DefaultDelayResolver.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/* www  .j av a  2  s.com*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("delay");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeAttribute("default", Long.toString(defaultDelay));
        writer.writeAttribute("scope", scope);
        writer.writeAttribute("ignoreRobotsCrawlDelay", Boolean.toString(ignoreRobotsCrawlDelay));

        for (DelaySchedule schedule : schedules) {
            writer.writeStartElement("schedule");
            if (schedule.getDayOfWeekRange() != null) {
                writer.writeAttribute("dayOfWeek", "from " + schedule.getDayOfWeekRange().getMinimum() + " to "
                        + schedule.getDayOfWeekRange().getMaximum());
            }
            if (schedule.getDayOfMonthRange() != null) {
                writer.writeAttribute("dayOfMonth", "from " + schedule.getDayOfMonthRange().getMinimum()
                        + " to " + schedule.getDayOfMonthRange().getMaximum());
            }
            if (schedule.getTimeRange() != null) {
                writer.writeAttribute("time", "from " + schedule.getTimeRange().getLeft().toString("HH:MM")
                        + " to " + schedule.getTimeRange().getRight().toString("HH:MM"));
            }
            writer.writeEndElement();
        }
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.github.fritaly.graphml4j.LabelStyle.java

void writeTo(XMLStreamWriter writer, String label) throws XMLStreamException {
    Validate.notNull(writer, "The given stream writer is null");

    // y:NodeLabel
    writer.writeStartElement("y:NodeLabel");
    writer.writeAttribute("alignement", textAlignment.getValue());
    writer.writeAttribute("autoSizePolicy", sizePolicy.getValue());
    writer.writeAttribute("fontFamily", fontFamily);
    writer.writeAttribute("fontSize", Integer.toString(fontSize));
    writer.writeAttribute("fontStyle", fontStyle.getValue());
    writer.writeAttribute("modelName", placement.getValue());
    writer.writeAttribute("modelPosition", position.getValue());

    if (borderDistance != 0.0f) {
        writer.writeAttribute("borderDistance", String.format("%.1f", borderDistance));
    }// ww w . j a v a2s  .  c o m
    if (rotationAngle != 0.0f) {
        writer.writeAttribute("rotationAngle", String.format("%.1f", rotationAngle));
    }

    if (backgroundColor != null) {
        writer.writeAttribute("backgroundColor", Utils.encode(backgroundColor));
    } else {
        writer.writeAttribute("hasBackgroundColor", "false");
    }
    if (lineColor != null) {
        writer.writeAttribute("lineColor", Utils.encode(lineColor));
    } else {
        writer.writeAttribute("hasLineColor", "false");
    }
    if (hasInsets()) {
        writer.writeAttribute("bottomInset", Integer.toString(bottomInset));
        writer.writeAttribute("topInset", Integer.toString(topInset));
        writer.writeAttribute("leftInset", Integer.toString(leftInset));
        writer.writeAttribute("rightInset", Integer.toString(rightInset));
    }

    writer.writeAttribute("textColor", Utils.encode(textColor));
    writer.writeAttribute("visible", Boolean.toString(visible));

    if (underlinedText) {
        writer.writeAttribute("underlinedText", Boolean.toString(underlinedText));
    }

    writer.writeCharacters(label);
    writer.writeEndElement(); // </y:NodeLabel>
}

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

private ResponseEntity<String> errorResponse(String message, HttpStatus status) throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeEmptyElement("SearchResult");
    w.writeStartElement("Error");
    w.writeAttribute("message", message);
    w.writeEndElement();// w  ww. j a v  a2s .c  o  m
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return new ResponseEntity<>(sw.toString(), status);
}

From source file:com.norconex.collector.http.client.impl.DefaultHttpClientInitializer.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from   w  w w  .j  av a  2  s.  co m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("httpClientInitializer");
        writer.writeAttribute("class", getClass().getCanonicalName());

        writeSimpleElement(writer, "cookiesDisabled", Boolean.toString(cookiesDisabled));
        writeSimpleElement(writer, "userAgent", userAgent);
        writeSimpleElement(writer, "authMethod", authMethod);
        writeSimpleElement(writer, "authUsername", authUsername);
        writeSimpleElement(writer, "authPassword", authPassword);
        writeSimpleElement(writer, "authUsernameField", authUsernameField);
        writeSimpleElement(writer, "authPasswordField", authPasswordField);
        writeSimpleElement(writer, "authURL", authURL);
        writeSimpleElement(writer, "authHostname", authHostname);
        writeSimpleElement(writer, "authPort", Integer.toString(authPort));
        writeSimpleElement(writer, "authRealm", authRealm);
        writeSimpleElement(writer, "proxyHost", proxyHost);
        writeSimpleElement(writer, "proxyPort", Integer.toString(proxyPort));
        writeSimpleElement(writer, "proxyUsername", proxyUsername);
        writeSimpleElement(writer, "proxyPassword", proxyPassword);
        writeSimpleElement(writer, "proxyRealm", proxyRealm);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

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

@SuppressWarnings("deprecation")
@Override/*from w  w w .  j a  v a  2 s .  co 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 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();//  ww w .j a  v  a  2s  .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);
    }//  ww  w.  j  a  v  a 2s .co  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: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  v a2s  .com
        } else {
            outputObject(value, null, writer);
        }

        writer.writeEndElement();
    }

    writer.writeEndElement();
}

From source file:com.norconex.committer.solr.SolrCommitter.java

@Override
protected void saveToXML(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("solrURL");
    writer.writeCharacters(solrURL);//from  w ww . ja  v  a2 s. com
    writer.writeEndElement();

    writer.writeStartElement("solrUpdateURLParams");
    for (String name : updateUrlParams.keySet()) {
        writer.writeStartElement("param");
        writer.writeAttribute("name", name);
        writer.writeCharacters(updateUrlParams.get(name));
        writer.writeEndElement();
    }
    writer.writeEndElement();
}

From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.java

/**
 * Reserved for internal use. Writes an entity to the stream as an AtomPub Entry Resource, leaving the stream open
 * for additional writing./* w  w  w  . ja  v a  2 s .  c  om*/
 * 
 * @param entity
 *            The instance implementing {@link TableEntity} to write to the output stream.
 * @param isTableEntry
 *            A flag indicating the entity is a reference to a table at the top level of the storage service when
 *            <code>true<code> and a reference to an entity within a table when <code>false</code>.
 * @param xmlw
 *            The <code>XMLStreamWriter</code> to write the entity to.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * 
 * @throws XMLStreamException
 *             if an error occurs accessing the stream.
 * @throws StorageException
 *             if a Storage service error occurs.
 */
protected static void writeEntityToStream(final TableEntity entity, final boolean isTableEntry,
        final XMLStreamWriter xmlw, final OperationContext opContext)
        throws XMLStreamException, StorageException {
    final HashMap<String, EntityProperty> properties = entity.writeEntity(opContext);
    if (properties == null) {
        throw new IllegalArgumentException("Entity did not produce properties to serialize");
    }

    if (!isTableEntry) {
        Utility.assertNotNullOrEmpty(TableConstants.PARTITION_KEY, entity.getPartitionKey());
        Utility.assertNotNullOrEmpty(TableConstants.ROW_KEY, entity.getRowKey());
        Utility.assertNotNull(TableConstants.TIMESTAMP, entity.getTimestamp());
    }

    // Begin entry
    xmlw.writeStartElement("entry");
    xmlw.writeNamespace("d", ODataConstants.DATA_SERVICES_NS);
    xmlw.writeNamespace("m", ODataConstants.DATA_SERVICES_METADATA_NS);

    // default namespace
    xmlw.writeNamespace(null, ODataConstants.ATOM_NS);

    // Content
    xmlw.writeStartElement(ODataConstants.CONTENT);
    xmlw.writeAttribute(ODataConstants.TYPE, ODataConstants.ODATA_CONTENT_TYPE);

    // m:properties
    xmlw.writeStartElement("m", ODataConstants.PROPERTIES, ODataConstants.DATA_SERVICES_METADATA_NS);

    if (!isTableEntry) {
        // d:PartitionKey
        xmlw.writeStartElement("d", TableConstants.PARTITION_KEY, ODataConstants.DATA_SERVICES_NS);
        xmlw.writeAttribute("xml", "xml", "space", "preserve");
        xmlw.writeCharacters(entity.getPartitionKey());
        xmlw.writeEndElement();

        // d:RowKey
        xmlw.writeStartElement("d", TableConstants.ROW_KEY, ODataConstants.DATA_SERVICES_NS);
        xmlw.writeAttribute("xml", "xml", "space", "preserve");
        xmlw.writeCharacters(entity.getRowKey());
        xmlw.writeEndElement();

        // d:Timestamp
        if (entity.getTimestamp() == null) {
            entity.setTimestamp(new Date());
        }

        xmlw.writeStartElement("d", TableConstants.TIMESTAMP, ODataConstants.DATA_SERVICES_NS);
        xmlw.writeAttribute("m", ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.TYPE,
                EdmType.DATE_TIME.toString());
        xmlw.writeCharacters(Utility.getTimeByZoneAndFormat(entity.getTimestamp(), Utility.UTC_ZONE,
                Utility.ISO8061_LONG_PATTERN));
        xmlw.writeEndElement();
    }

    for (final Entry<String, EntityProperty> ent : properties.entrySet()) {
        if (ent.getKey().equals(TableConstants.PARTITION_KEY) || ent.getKey().equals(TableConstants.ROW_KEY)
                || ent.getKey().equals(TableConstants.TIMESTAMP) || ent.getKey().equals("Etag")) {
            continue;
        }

        EntityProperty currProp = ent.getValue();

        // d:PropName
        xmlw.writeStartElement("d", ent.getKey(), ODataConstants.DATA_SERVICES_NS);

        if (currProp.getEdmType() == EdmType.STRING) {
            xmlw.writeAttribute("xml", "xml", "space", "preserve");
        } else if (currProp.getEdmType().toString().length() != 0) {
            String edmTypeString = currProp.getEdmType().toString();
            if (edmTypeString.length() != 0) {
                xmlw.writeAttribute("m", ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.TYPE,
                        edmTypeString);
            }
        }

        if (currProp.getIsNull()) {
            xmlw.writeAttribute("m", ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.NULL,
                    Constants.TRUE);
        }

        // Write Value
        xmlw.writeCharacters(currProp.getValueAsString());
        // End d:PropName
        xmlw.writeEndElement();
    }

    // End m:properties
    xmlw.writeEndElement();

    // End content
    xmlw.writeEndElement();

    // End entry
    xmlw.writeEndElement();
}