Example usage for javax.xml.stream XMLStreamWriter writeEmptyElement

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

Introduction

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

Prototype

public void writeEmptyElement(String localName) throws XMLStreamException;

Source Link

Document

Writes an empty element tag to the output

Usage

From source file:org.intermine.pathquery.PathQueryBinding.java

/**
 * Create XML for the join style in a PathQuery.
 *///from   w  ww .  java  2  s  .c  o m
private static void marshalPathQueryJoinStyle(PathQuery query, XMLStreamWriter writer)
        throws XMLStreamException {
    for (Map.Entry<String, OuterJoinStatus> entry : query.getOuterJoinStatus().entrySet()) {
        writer.writeEmptyElement("join");
        writer.writeAttribute("path", entry.getKey());
        writer.writeAttribute("style", entry.getValue().toString());
        //writer.writeEndElement();
    }
}

From source file:org.intermine.pathquery.PathQueryBinding.java

/**
 * Create XML for the path descriptions in a PathQuery.
 */// w  w w . j a  v a2 s  .  co m
private static void marshalPathQueryDescriptions(PathQuery query, XMLStreamWriter writer)
        throws XMLStreamException {
    for (Map.Entry<String, String> entry : query.getDescriptions().entrySet()) {
        String path = entry.getKey();
        String description = entry.getValue();
        writer.writeEmptyElement("pathDescription");
        writer.writeAttribute("pathString", path);
        writer.writeAttribute("description", description);
    }
}

From source file:org.intermine.pathquery.PathQueryBinding.java

/**
 * Create XML for the constraints in a PathQuery.
 *//* w  ww  . ja  v  a 2  s  .c  om*/
private void marshalPathQueryConstraints(PathQuery query, XMLStreamWriter writer,
        boolean hasMultipleConstraints) throws XMLStreamException {
    for (Map.Entry<PathConstraint, String> constraint : query.getConstraints().entrySet()) {
        boolean emptyElement = true;
        if (constraint.getKey() instanceof PathConstraintMultiValue) {
            emptyElement = false;
        }

        if (emptyElement) {
            writer.writeEmptyElement("constraint");
        } else {
            writer.writeStartElement("constraint");
        }
        writer.writeAttribute("path", constraint.getKey().getPath());
        if ((constraint.getValue() != null) && (hasMultipleConstraints)) {
            writer.writeAttribute("code", constraint.getValue());
        }
        doAdditionalConstraintStuff(query, constraint.getKey(), writer);
        if (constraint.getKey() instanceof PathConstraintAttribute) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
            String outputValue = ((PathConstraintAttribute) constraint.getKey()).getValue();
            writer.writeAttribute("value", "" + outputValue);
        } else if (constraint.getKey() instanceof PathConstraintNull) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
        } else if (constraint.getKey() instanceof PathConstraintSubclass) {
            writer.writeAttribute("type", ((PathConstraintSubclass) constraint.getKey()).getType());
        } else if (constraint.getKey() instanceof PathConstraintBag) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
            writer.writeAttribute("value", ((PathConstraintBag) constraint.getKey()).getBag());
        } else if (constraint.getKey() instanceof PathConstraintIds) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
            StringBuilder sb = new StringBuilder();
            boolean needComma = false;
            for (Integer id : ((PathConstraintIds) constraint.getKey()).getIds()) {
                if (needComma) {
                    sb.append(", ");
                }
                needComma = true;
                sb.append("" + id);
            }
            writer.writeAttribute("ids", sb.toString());
        } else if (constraint.getKey() instanceof PathConstraintMultiValue) {
            // Includes PathConstraintRange, which is serialised in the exact same manner.
            writer.writeAttribute("op", "" + constraint.getKey().getOp());

            for (String value : ((PathConstraintMultiValue) constraint.getKey()).getValues()) {
                if (value == null) {
                    writer.writeStartElement("nullValue");
                    writer.writeEndElement();
                } else {
                    if (!value.equals(value.trim())) {
                        throw new XMLStreamException("Value in MultiValue starts or ends with "
                                + "whitespace - this query cannot be represented in XML");
                    }
                    writer.writeStartElement("value");
                    writer.writeCharacters(value);
                    writer.writeEndElement();
                }
            }
        } else if (constraint.getKey() instanceof PathConstraintLoop) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
            writer.writeAttribute("loopPath", ((PathConstraintLoop) constraint.getKey()).getLoopPath());
        } else if (constraint.getKey() instanceof PathConstraintLookup) {
            writer.writeAttribute("op", "" + constraint.getKey().getOp());
            writer.writeAttribute("value", ((PathConstraintLookup) constraint.getKey()).getValue());
            String extraValue = ((PathConstraintLookup) constraint.getKey()).getExtraValue();
            if (extraValue != null) {
                writer.writeAttribute("extraValue", extraValue);
            }
        } else {
            throw new IllegalStateException(
                    "Unrecognised constraint type " + constraint.getKey().getClass().getName());
        }
        if (!emptyElement) {
            writer.writeEndElement();
        }
    }
}

From source file:org.opentox.toxotis.ontology.impl.MetaInfoImpl.java

private void writeMetaDatumResourceToStAX(String metaDatumNS, String metaDatumName, ResourceValue value,
        javax.xml.stream.XMLStreamWriter writer) throws XMLStreamException {
    if (value != null && value.getUri() != null && value.getUri().toString() != null
            && !(value.getUri().toString().isEmpty())) {
        String stringVal = value.getUri().toString();
        writer.writeEmptyElement(metaDatumNS + ":" + metaDatumName);
        writer.writeAttribute("rdf:resource", stringVal);
    }//from   w  w w . j a  v a 2s  .co  m
}

From source file:org.opentox.toxotis.ontology.impl.MetaInfoImpl.java

private void writeMetaDatumToStAX(String metaDatumNS, String metaDatumName, LiteralValue<?> value,
        javax.xml.stream.XMLStreamWriter writer) throws XMLStreamException {
    if (value != null) {
        String propertyTag = metaDatumNS + ":" + metaDatumName;
        if (value.getValue().toString().isEmpty()) {
            writer.writeEmptyElement(propertyTag);
        }// w w  w  .j  a v a  2 s .c  o  m
        writer.writeStartElement(propertyTag);
        writer.writeCharacters(value.getValue().toString());
        writer.writeEndElement();
    }
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingWriter.java

private static final void writeMapping(final XmiMapping mapping, final XMLStreamWriter xsw)
        throws XMLStreamException {
    xsw.writeStartElement(XmiMappingConstants.MAPPING.getNamespaceURI(),
            XmiMappingConstants.MAPPING.getLocalPart());
    try {//  w  w  w  .j a va2  s.  c o  m
        final XmiFeature lhsFeature = mapping.getLhsFeature();
        if (lhsFeature != null) {
            writeFeature(lhsFeature, XmiMappingConstants.LHS_FEATURE, xsw);
        } else {
            xsw.writeEmptyElement(XmiMappingConstants.LHS_MISSING.getLocalPart());
        }
        final XmiFeature rhsFeature = mapping.getRhsFeature();
        if (rhsFeature != null) {
            writeFeature(rhsFeature, XmiMappingConstants.RHS_FEATURE, xsw);
        } else {
            xsw.writeEmptyElement(XmiMappingConstants.RHS_MISSING.getLocalPart());
        }
        writeStatus(mapping.getStatus(), xsw);
        writeTracking(mapping.getTracking(), xsw);
        writeComment(mapping.getComment(), xsw);
    } finally {
        xsw.writeEndElement();
    }
}

From source file:org.tolven.assembler.facesconfig.FacesConfigAssembler.java

protected boolean addNavigationRuleTemplates(PluginDescriptor warPluginDescriptor,
        XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    boolean added = false;
    xmlStreamWriter.writeStartElement("xsl:template");
    xmlStreamWriter.writeAttribute("name", "addNavigationRules");
    xmlStreamWriter.writeCharacters("\n");
    Map<String, Map<String, Object>> navigationRuleMap = new HashMap<String, Map<String, Object>>();
    for (Extension defaultExtension : getNavigationRuleExtensions(warPluginDescriptor, true)) {
        String fromViewId = defaultExtension.getParameter("from-view-id").valueAsString();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("navigationRuleExtension", defaultExtension);
        navigationRuleMap.put(fromViewId, map);
        added = true;//w w w. j  a v a 2s . com
    }
    for (Extension nonDefaultExtension : getNavigationRuleExtensions(warPluginDescriptor, false)) {
        String fromViewId = nonDefaultExtension.getParameter("from-view-id").valueAsString();
        Extension navigationRuleExtension = null;
        Map<String, Object> map = (Map<String, Object>) navigationRuleMap.get(fromViewId);
        if (map == null) {
            map = new HashMap<String, Object>();
            navigationRuleMap.put(fromViewId, map);
        } else {
            navigationRuleExtension = (Extension) map.get("navigationRuleExtension");
            if (nonDefaultExtension.getParameter("override") == null) {
                throw new RuntimeException(
                        nonDefaultExtension.getUniqueId() + " requires an override parameter to override "
                                + navigationRuleExtension.getUniqueId());
            }
            logger.debug(
                    nonDefaultExtension.getUniqueId() + " overrides " + navigationRuleExtension.getUniqueId());
        }
        navigationRuleMap.get(fromViewId).put("navigationRuleExtension", nonDefaultExtension);
        added = true;
    }
    for (String fromViewId : navigationRuleMap.keySet()) {
        Map<String, Object> map = navigationRuleMap.get(fromViewId);
        Extension navigationRuleExtension = (Extension) map.get("navigationRuleExtension");
        Map<String, Parameter> caseParameterMap = new HashMap<String, Parameter>();
        for (Parameter parameter : navigationRuleExtension.getParameters("navigation-case")) {
            String fromAction = null;
            Parameter fromActionParameter = parameter.getSubParameter("from-action");
            if (fromActionParameter == null) {
                fromAction = null;
            } else {
                fromAction = fromActionParameter.valueAsString();
            }
            String fromOutcome = null;
            Parameter fromOutcomeParameter = parameter.getSubParameter("from-outcome");
            if (fromOutcomeParameter == null) {
                fromOutcome = null;
            } else {
                fromOutcome = fromOutcomeParameter.valueAsString();
            }
            Parameter toViewIdParameter = parameter.getSubParameter("to-view-id");
            //caseParameter map has key which is concatentation of fromAction, fromOutcomeParameter and toViewIdParameter
            caseParameterMap.put(fromAction + fromOutcome + toViewIdParameter.valueAsString(), parameter);
        }
        map.put("caseParameterMap", caseParameterMap);
        added = true;
    }
    for (Extension navigationRuleContributionExtension : getNavigationRuleContributionExtensions(
            warPluginDescriptor)) {
        String fromViewId = navigationRuleContributionExtension.getParameter("from-view-id").valueAsString();
        Map<String, Object> map = navigationRuleMap.get(fromViewId);
        Extension navigationRuleExtension = (Extension) map.get("navigationRuleExtension");
        Map<String, Parameter> caseParameterMap = (Map<String, Parameter>) map.get("caseParameterMap");
        if (navigationRuleExtension == null) {
            throw new RuntimeException(navigationRuleContributionExtension.getUniqueId()
                    + " refers to a navigation-rule that does not exist with from-view-id: " + fromViewId);
        }
        Parameter contributingCaseParameter = navigationRuleContributionExtension
                .getParameter("navigation-case");
        Parameter contributingFromActionParameter = contributingCaseParameter.getSubParameter("from-action");
        String contributingFromAction = null;
        if (contributingFromActionParameter == null) {
            contributingFromAction = "";
        } else {
            contributingFromAction = contributingFromActionParameter.valueAsString();
        }
        Parameter contributingFromOutcomeParameter = contributingCaseParameter.getSubParameter("from-outcome");
        String contributingFromOutcome = null;
        if (contributingFromOutcomeParameter == null) {
            contributingFromOutcome = "";
        } else {
            contributingFromOutcome = contributingFromOutcomeParameter.valueAsString();
        }
        String key = contributingFromAction + fromViewId + contributingFromOutcome;
        Parameter caseParameter = caseParameterMap.get(key);
        if (caseParameter != null) {
            if (navigationRuleContributionExtension.getParameter("override") == null) {
                throw new RuntimeException(navigationRuleContributionExtension.getUniqueId()
                        + " requires an override parameter to override navigation-case in: "
                        + navigationRuleExtension.getUniqueId());
            }
            logger.debug(navigationRuleContributionExtension.getUniqueId() + " overrides navigation-case in: "
                    + navigationRuleExtension.getUniqueId());
        }
        caseParameterMap.put(key, contributingCaseParameter);
        added = true;
    }
    Comparator<String> navigationRuleComparator = new Comparator<String>() {
        public int compare(String string1, String string2) {
            return string1.compareTo(string2);
        };
    };
    List<String> fromViewIdList = new ArrayList<String>();
    fromViewIdList.addAll(navigationRuleMap.keySet());
    Collections.sort(fromViewIdList, navigationRuleComparator);
    for (String fromViewId : fromViewIdList) {
        Map<String, Object> map = navigationRuleMap.get(fromViewId);
        Extension navigationRuleExtension = (Extension) map.get("navigationRuleExtension");
        xmlStreamWriter.writeStartElement("xsl:if");
        xmlStreamWriter.writeAttribute("test",
                "count(tp:navigation-rule[tp:from-view-id = '" + fromViewId + "']) = 0");
        xmlStreamWriter.writeStartElement("navigation-rule");
        xmlStreamWriter.writeCharacters("\n");
        Parameter descriptionParameter = navigationRuleExtension.getParameter("description");
        if (descriptionParameter != null) {
            xmlStreamWriter.writeStartElement("description");
            xmlStreamWriter.writeCharacters(descriptionParameter.valueAsString());
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeCharacters("\n");
        }
        xmlStreamWriter.writeStartElement("from-view-id");
        xmlStreamWriter.writeCharacters(fromViewId);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        Map<String, Parameter> caseParameterMap = (Map<String, Parameter>) map.get("caseParameterMap");
        List<Parameter> caseParameterList = new ArrayList<Parameter>();
        caseParameterList.addAll(caseParameterMap.values());
        Comparator<Parameter> caseParameterComparator = new Comparator<Parameter>() {
            public int compare(Parameter p1, Parameter p2) {
                Parameter p1FromActionParameter = p1.getSubParameter("from-action");
                Parameter p1FromOutcomeParameter = p1.getSubParameter("from-outcome");
                Parameter p1RedirectParameter = p1.getSubParameter("redirect");
                Parameter p2FromActionParameter = p2.getSubParameter("from-action");
                Parameter p2FromOutcomeParameter = p2.getSubParameter("from-outcome");
                Parameter p2RedirectParameter = p2.getSubParameter("redirect");
                String s1 = (p1FromActionParameter == null ? "null" : p1FromActionParameter.valueAsString())
                        + (p1FromOutcomeParameter == null ? "null" : p1FromOutcomeParameter.valueAsString())
                        + (p1RedirectParameter == null ? "null" : p1RedirectParameter.valueAsString());
                String s2 = (p2FromActionParameter == null ? "null" : p2FromActionParameter.valueAsString())
                        + (p2FromOutcomeParameter == null ? "null" : p2FromOutcomeParameter.valueAsString())
                        + (p2RedirectParameter == null ? "null" : p2RedirectParameter.valueAsString());
                return s1.compareTo(s2);
            };
        };
        Collections.sort(caseParameterList, caseParameterComparator);
        for (Parameter parameter : caseParameterList) {
            xmlStreamWriter.writeStartElement("navigation-case");
            xmlStreamWriter.writeCharacters("\n");
            Parameter fromActionParameter = parameter.getSubParameter("from-action");
            if (fromActionParameter != null) {
                xmlStreamWriter.writeStartElement("from-action");
                xmlStreamWriter.writeCharacters(fromActionParameter.valueAsString());
                xmlStreamWriter.writeEndElement();
                xmlStreamWriter.writeCharacters("\n");
            }
            Parameter fromOutcomeParameter = parameter.getSubParameter("from-outcome");
            if (fromOutcomeParameter != null) {
                xmlStreamWriter.writeStartElement("from-outcome");
                xmlStreamWriter.writeCharacters(fromOutcomeParameter.valueAsString());
                xmlStreamWriter.writeEndElement();
                xmlStreamWriter.writeCharacters("\n");
            }
            xmlStreamWriter.writeStartElement("to-view-id");
            xmlStreamWriter.writeCharacters(parameter.getSubParameter("to-view-id").valueAsString());
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeCharacters("\n");
            Parameter redirectParameter = parameter.getSubParameter("redirect");
            if (redirectParameter != null) {
                xmlStreamWriter.writeEmptyElement("redirect");
                xmlStreamWriter.writeCharacters("\n");
            }
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeCharacters("\n");
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        added = true;
    }
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    return added;
}

From source file:org.xwiki.xar.XarPackage.java

private void writeElement(XMLStreamWriter streamWriter, String localName, String value, boolean emptyIfNull)
        throws XMLStreamException {
    if (value != null) {
        if (value.isEmpty()) {
            streamWriter.writeEmptyElement(localName);
        } else {/*from  w ww .  j  a v a2s. co m*/
            streamWriter.writeStartElement(localName);
            streamWriter.writeCharacters(value);
            streamWriter.writeEndElement();
        }
    } else if (emptyIfNull) {
        streamWriter.writeEmptyElement(localName);
    }
}