/** * Render the given Item as XML using an XMLStreamWriter * * @param writer to XMLStreamWriter to write to * @param item the Item to render */ public static void renderImpl(XMLStreamWriter writer, Item item) { try { writer.writeStartElement("item"); if (item.getIdentifier() != null) { writer.writeAttribute("id", item.getIdentifier()); } writer.writeAttribute("class", item.getClassName() == null ? "" : item.getClassName()); if (item.getImplementations() != null && !"".equals(item.getImplementations())) { writer.writeAttribute("implements", item.getImplementations()); } writer.writeCharacters(ENDL); TreeSet<Attribute> attrs = new TreeSet<Attribute>(new RendererComparator()); attrs.addAll(item.getAttributes()); for (Attribute attr : attrs) { if (!"".equals(attr.getValue())) { writer.writeEmptyElement("attribute"); writer.writeAttribute("name", attr.getName()); writer.writeAttribute("value", attr.getValue()); writer.writeCharacters(ENDL); } } TreeSet<Reference> refs = new TreeSet<Reference>(new RendererComparator()); refs.addAll(item.getReferences()); for (Reference ref : refs) { writer.writeEmptyElement("reference"); writer.writeAttribute("name", ref.getName()); writer.writeAttribute("ref_id", ref.getRefId()); writer.writeCharacters(ENDL); } TreeSet<ReferenceList> cols = new TreeSet<ReferenceList>(new RendererComparator()); cols.addAll(item.getCollections()); for (ReferenceList refList : cols) { writer.writeStartElement("collection"); writer.writeAttribute("name", refList.getName()); for (String ref : refList.getRefIds()) { writer.writeEmptyElement("reference"); writer.writeAttribute("ref_id", ref); } writer.writeEndElement(); writer.writeCharacters(ENDL); } writer.writeEndElement(); writer.writeCharacters(ENDL); } catch (XMLStreamException e) { throw new RuntimeException("unexpected exception while accessing a XMLStreamWriter", e); } }
public void close() { if (closed) { return; } closed = true; // finish off anything try { xmlw.writeEndElement(); // msRun element // declare the index does not exist xmlw.writeEmptyElement("indexOffset"); xmlw.writeAttribute("xsi:nil", "1"); xmlw.writeEndElement(); // mzXML element // close out the xml writer xmlw.flush(); xmlw.close(); } catch (XMLStreamException e) { throw new RuntimeException("Can't write all of XML!", e); } finally { super.close(); } }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { try { String fileName = null; try { if (args[0].equals("-f")) { fileName = args[1]; } else { printUsage(); return; } } catch (Exception ex) { printUsage(); return; } XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; xtw = xof.createXMLStreamWriter(new FileOutputStream(fileName), "utf-8"); xtw.writeStartDocument("utf-8", "1.0"); xtw.writeComment("StAX Sample: writer.HelloWorld"); xtw.writeStartElement("hello"); xtw.writeDefaultNamespace("http://samples"); xtw.writeCharacters("this crazy"); xtw.writeEmptyElement("world"); xtw.writeEndElement(); xtw.writeEndDocument(); xtw.flush(); xtw.close(); } catch (Exception ex) { ex.printStackTrace(); System.err.println("Exception occurred while running Hello World samples"); } System.out.println("Done"); }
@Override public void marshallAsElement(ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException { if (resourceModel.hasDefined(getName()) && resourceModel.asInt() > 0) { final ModelNode modules = resourceModel.get(getName()); for (ModelNode module : modules.asList()) { writer.writeStartElement(getXmlName()); writer.writeAttribute(Attribute.CODE.getLocalName(), module.get(CODE).asString()); if (module.hasDefined(Constants.LOGIN_MODULE_STACK_REF)) { writer.writeAttribute( Attribute.LOGIN_MODULE_STACK_REF.getLocalName(), module.get(Constants.LOGIN_MODULE_STACK_REF).asString()); } if (module.hasDefined(Constants.MODULE_OPTIONS)) { for (ModelNode option : module.get(Constants.MODULE_OPTIONS).asList()) { writer.writeEmptyElement(Element.MODULE_OPTION.getLocalName()); writer.writeAttribute(Attribute.NAME.getLocalName(), option.asProperty().getName()); writer.writeAttribute( Attribute.VALUE.getLocalName(), option.asProperty().getValue().asString()); } } writer.writeEndElement(); } } }
private void appendKey(final XMLStreamWriter writer, final EdmEntityType entityType) throws XMLStreamException { List<EdmKeyPropertyRef> keyPropertyRefs = entityType.getKeyPropertyRefs(); if (keyPropertyRefs != null && !keyPropertyRefs.isEmpty()) { // Resolve Base Type key as it is shown in derived type EdmEntityType baseType = entityType.getBaseType(); if (baseType != null && baseType.getKeyPropertyRefs() != null && !(baseType.getKeyPropertyRefs().isEmpty())) { return; } writer.writeStartElement(XML_KEY); for (EdmKeyPropertyRef keyRef : keyPropertyRefs) { writer.writeEmptyElement(XML_PROPERTY_REF); writer.writeAttribute(XML_NAME, keyRef.getName()); if (keyRef.getAlias() != null) { writer.writeAttribute(XML_ALIAS, keyRef.getAlias()); } } writer.writeEndElement(); } }
@Override public void marshallAsElement(ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException { if (isMarshallable(resourceModel)) { writer.writeEmptyElement(getXmlName()); writer.writeAttribute(attribute.getLocalName(), resourceModel.get(getName()).asString()); } }
private void writeOnlineResource(final XMLStreamWriter writer, final String url) throws XMLStreamException { if (url != null) { writer.writeEmptyElement("OnlineResource"); writer.writeNamespace(XLINK_PREFIX, XLNNS); writer.writeAttribute(XLNNS, "type", "simple"); writer.writeAttribute(XLNNS, "href", url); } }
private void writeTransactional(TransactionalDef transactionDef) throws XMLStreamException { if (transactionDef != null) { writer.writeCharacters(" "); writer.writeEmptyElement("tx", "transaction", NS_TX); writer.writeAttribute("method", transactionDef.getMethod()); writer.writeAttribute("value", transactionDef.getType()); writer.writeCharacters("\n"); } }
private void writeServiceRef(OsgiServiceBean serviceBean) throws XMLStreamException { writer.writeEmptyElement("reference"); writer.writeAttribute("id", serviceBean.id); writer.writeAttribute("interface", serviceBean.clazz.getName()); if (serviceBean.filter != null && !"".equals(serviceBean.filter)) { writer.writeAttribute("filter", serviceBean.filter); } writer.writeCharacters("\n"); }
@Override public void writeXML(XMLStreamWriter writer) throws XMLStreamException { writer.writeEmptyElement("geofence"); writer.writeAttribute("name", nameTextField.getValue()); writer.writeAttribute("operator", getOperator()); writer.writeAttribute("longitude", longitudeTextField.getValue()); writer.writeAttribute("latitude", latitudeTextField.getValue()); writer.writeAttribute("radius", radiusTextField.getValue()); }
void writeObject(Object object) throws XMLStreamException { if (object == null) { writer.writeEmptyElement("null"); } else if (object instanceof Iterable) { writeIterable((Iterable) object); } else { writer.writeCharacters(object.toString()); } }
private void writePersistenceUnit(Field field) throws XMLStreamException { PersistenceUnit persistenceUnit = field.getAnnotation(PersistenceUnit.class); if (persistenceUnit != null) { writer.writeCharacters(" "); writer.writeEmptyElement("jpa", "context", NS_JPA); writer.writeAttribute("unitname", persistenceUnit.unitName()); writer.writeAttribute("property", field.getName()); writer.writeCharacters("\n"); } }
private void appendNavigationPropertyBindings( final XMLStreamWriter writer, final EdmBindingTarget bindingTarget) throws XMLStreamException { if (bindingTarget.getNavigationPropertyBindings() != null) { for (EdmNavigationPropertyBinding binding : bindingTarget.getNavigationPropertyBindings()) { writer.writeEmptyElement(XML_NAVIGATION_PROPERTY_BINDING); writer.writeAttribute(XML_PATH, binding.getPath()); writer.writeAttribute(XML_TARGET, binding.getTarget()); } } }
@Override public void marshallAsElement( AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException { writer.writeEmptyElement(Element.HOST.getLocalName()); writer.writeAttribute( Attribute.NAME.getLocalName(), resourceModel.asString()); }
protected void writeEmptyElementWithNS(String namespaceURI, String localname) throws XMLStreamException { if (namespaceURI == null || namespaceURI.length() == 0) { writer.writeEmptyElement(localname); } else { if (writer.getNamespaceContext().getPrefix(namespaceURI) == null) { String prefix = nsToPrefix.get(namespaceURI); if (prefix != null) { writer.writeEmptyElement(prefix, localname, namespaceURI); writer.writeNamespace(prefix, namespaceURI); } else { LOG.warn( "No prefix for namespace '{}' configured. Depending on XMLStream auto-repairing.", namespaceURI); writer.writeEmptyElement(namespaceURI, localname); } } else { writer.writeEmptyElement(namespaceURI, localname); } } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; Throwable error = null; try { result = method.invoke(target, args); } catch (InvocationTargetException e) { error = e.getTargetException(); } catch (Throwable e) { // Proxy not allowed to throw exception } if (method.getDeclaringClass() != Object.class) { try { StringWriter stringWriter = new StringWriter(); this.writer = XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter); writer.writeStartElement("invoke"); writer.writeAttribute("timestamp", Long.valueOf(System.currentTimeMillis()).toString()); writer.writeAttribute("class", target.getClass().getName()); writer.writeAttribute("name", method.getName()); if (args != null && args.length != 0) { writer.writeStartElement("arguments"); for (int i = 0; i < args.length; i++) { writer.writeStartElement("argument"); writeObject(args[i]); writer.writeEndElement(); } writer.writeEndElement(); } else { writer.writeEmptyElement("arguments"); } if (method.getReturnType() != void.class && error == null) { writer.writeStartElement("return"); writeObject(result); writer.writeEndElement(); } else if (error != null) { writer.writeStartElement("thrown"); writer.writeCharacters(error.toString()); writer.writeEndElement(); } writer.writeEndElement(); writer.flush(); initialWriter.write(stringWriter.toString() + System.lineSeparator()); } catch (Exception e) { // Proxy not allowed to throw exceptions } } if (error != null) { throw error; } return result; }
@Override void writeXML(XMLStreamWriter writer) throws XMLStreamException { writer.writeStartElement( COMPOSITE_INDEX_TAG.getNamespaceURI(), COMPOSITE_INDEX_TAG.getLocalPart()); this.writeAttributes(writer, true); for (int storageId : this.indexedFields) { writer.writeEmptyElement( INDEXED_FIELD_TAG.getNamespaceURI(), INDEXED_FIELD_TAG.getLocalPart()); writer.writeAttribute( STORAGE_ID_ATTRIBUTE.getNamespaceURI(), STORAGE_ID_ATTRIBUTE.getLocalPart(), "" + storageId); } writer.writeEndElement(); // </CompositeIndex> }
public void generateSchema(XMLStreamWriter out) throws XMLStreamException { out.writeStartElement("xsd", "simpleType", XML_SCHEMA_NS); out.writeAttribute("name", JAXBUtil.qNameToString(_typeName)); out.writeStartElement("xsd", "restriction", XML_SCHEMA_NS); out.writeAttribute("base", JAXBUtil.qNameToString(_baseName)); for (String name : _nameMap.values()) { out.writeEmptyElement("xsd", "enumeration", XML_SCHEMA_NS); out.writeAttribute("value", name); } out.writeEndElement(); // restriction out.writeEndElement(); // simpleType }
@Override public void marshallAsElement( AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException { resourceModel = resourceModel.get(attribute.getName()); if (resourceModel.isDefined()) { for (ModelNode property : resourceModel.asList()) { writer.writeEmptyElement(REALM_MAPPING); writer.writeAttribute(FROM, property.asProperty().getName()); writer.writeAttribute(TO, property.asProperty().getValue().asString()); } } }
@Override public void writeProperty(Property property) { try { writer.writeCharacters(" "); writer.writeEmptyElement("property"); writer.writeAttribute("name", property.name); if (property.ref != null) { writer.writeAttribute("ref", property.ref); } else if (property.value != null) { writer.writeAttribute("value", property.value); } writer.writeCharacters("\n"); } catch (XMLStreamException e) { throw new RuntimeException(e.getMessage(), e); } }
private void appendOperationReturnType(final XMLStreamWriter writer, final EdmOperation operation) throws XMLStreamException { EdmReturnType returnType = operation.getReturnType(); if (returnType != null) { writer.writeEmptyElement(XML_RETURN_TYPE); String returnTypeFqnString; if (EdmTypeKind.PRIMITIVE.equals(returnType.getType().getKind())) { returnTypeFqnString = getFullQualifiedName(returnType.getType(), returnType.isCollection()); } else { returnTypeFqnString = getAliasedFullQualifiedName(returnType.getType(), returnType.isCollection()); } writer.writeAttribute(XML_TYPE, returnTypeFqnString); appendReturnTypeFacets(writer, returnType); } }
private void appendOperationParameters(final XMLStreamWriter writer, final EdmOperation operation) throws XMLStreamException { for (String parameterName : operation.getParameterNames()) { EdmParameter parameter = operation.getParameter(parameterName); writer.writeEmptyElement(XML_PARAMETER); writer.writeAttribute(XML_NAME, parameterName); String typeFqnString; if (EdmTypeKind.PRIMITIVE.equals(parameter.getType().getKind())) { typeFqnString = getFullQualifiedName(parameter.getType(), parameter.isCollection()); } else { typeFqnString = getAliasedFullQualifiedName(parameter.getType(), parameter.isCollection()); } writer.writeAttribute(XML_TYPE, typeFqnString); appendParameterFacets(writer, parameter); } }
private void appendProperties(final XMLStreamWriter writer, final EdmStructuredType type) throws XMLStreamException { List<String> propertyNames = new ArrayList<String>(type.getPropertyNames()); if (type.getBaseType() != null) { propertyNames.removeAll(type.getBaseType().getPropertyNames()); } for (String propertyName : propertyNames) { EdmProperty property = type.getStructuralProperty(propertyName); writer.writeEmptyElement(XML_PROPERTY); writer.writeAttribute(XML_NAME, propertyName); String fqnString; if (property.isPrimitive()) { fqnString = getFullQualifiedName(property.getType(), property.isCollection()); } else { fqnString = getAliasedFullQualifiedName(property.getType(), property.isCollection()); } writer.writeAttribute(XML_TYPE, fqnString); // Facets if (!property.isNullable()) { writer.writeAttribute(XML_NULLABLE, "" + property.isNullable()); } if (!property.isUnicode()) { writer.writeAttribute(XML_UNICODE, "" + property.isUnicode()); } if (property.getDefaultValue() != null) { writer.writeAttribute(XML_DEFAULT_VALUE, property.getDefaultValue()); } if (property.getMaxLength() != null) { writer.writeAttribute(XML_MAX_LENGTH, "" + property.getMaxLength()); } if (property.getPrecision() != null) { writer.writeAttribute(XML_PRECISION, "" + property.getPrecision()); } if (property.getScale() != null) { writer.writeAttribute(XML_SCALE, "" + property.getScale()); } } }
public void serialize(XMLStreamWriter writer) throws XMLStreamException { QName name = constants.getUserNameToken(); String localname = name.getLocalPart(); String namespaceURI = name.getNamespaceURI(); String prefix = writer.getPrefix(namespaceURI); if (prefix == null) { prefix = name.getPrefix(); writer.setPrefix(prefix, namespaceURI); } // <sp:KeyValueToken writer.writeStartElement(prefix, localname, namespaceURI); writer.writeNamespace(prefix, namespaceURI); String inclusion; inclusion = constants.getAttributeValueFromInclusion(getInclusion()); if (inclusion != null) { writer.writeAttribute(prefix, namespaceURI, SPConstants.ATTR_INCLUDE_TOKEN, inclusion); } if (forceRsaKeyValue) { String pPrefix = writer.getPrefix(SPConstants.POLICY.getNamespaceURI()); if (pPrefix == null) { writer.setPrefix(SPConstants.POLICY.getPrefix(), SPConstants.POLICY.getNamespaceURI()); } // <wsp:Policy> writer.writeStartElement( pPrefix, SPConstants.POLICY.getLocalPart(), SPConstants.POLICY.getNamespaceURI()); writer.writeEmptyElement(prefix, "RsaKeyValue", namespaceURI); // </wsp:Policy> writer.writeEndElement(); } writer.writeEndElement(); // </sp:KeyValueToken> }
private void appendEnumTypes(final XMLStreamWriter writer, final List<EdmEnumType> enumTypes) throws XMLStreamException { for (EdmEnumType enumType : enumTypes) { writer.writeStartElement(XML_ENUM_TYPE); writer.writeAttribute(XML_NAME, enumType.getName()); writer.writeAttribute(XML_IS_FLAGS, Boolean.toString(enumType.isFlags())); writer.writeAttribute( XML_UNDERLYING_TYPE, getFullQualifiedName(enumType.getUnderlyingType(), false)); for (String memberName : enumType.getMemberNames()) { writer.writeEmptyElement(XML_MEMBER); writer.writeAttribute(XML_NAME, memberName); final String value = enumType.getMember(memberName).getValue(); if (value != null) { writer.writeAttribute(XML_VALUE, value); } } writer.writeEndElement(); } }
/** Recursively serialize a single element, appending it to DOM element <code>parent</code>. */ protected void serializeElement(XMLStreamWriter writer, EventDataElement element) throws XMLStreamException { if (!element.isNull()) { // create a new node for the element and append it to the parent. String name = getXMLElementName(element.getName()); if (element.isEmpty()) { writer.writeEmptyElement(name); // TODO: remove when stax bug is fixed. // this is a workaround for a bug in the 1.2 StAX implementation, where // if the only element in your document is empty, the closing "/>" never gets written. // any other API call fixes the problem, so here we do a no-op string append to force // the element closed. writer.writeCharacters(DUMMY_TEXT, 0, 0); } else { writer.writeStartElement(name); // add attributes for properties. Iterator<? extends Entry<String, Object>> props = element.iterateProperties(); while (props.hasNext()) { Entry<String, Object> prop = props.next(); Object value = prop.getValue(); if (value != null) { String propName = getXMLElementName(prop.getKey()); writer.writeAttribute(propName, value.toString()); } } // add text to the element if applicable. String text = element.getText(); if (text != null) writer.writeCharacters(text); // add child elements for children. Iterator<EventDataElement> children = element.iterateChildren(); while (children.hasNext()) serializeElement(writer, children.next()); writer.writeEndElement(); } } }
private void appendNavigationProperties( final XMLStreamWriter writer, final EdmStructuredType type) throws XMLStreamException { List<String> navigationPropertyNames = new ArrayList<String>(type.getNavigationPropertyNames()); if (type.getBaseType() != null) { navigationPropertyNames.removeAll(type.getBaseType().getNavigationPropertyNames()); } for (String navigationPropertyName : navigationPropertyNames) { EdmNavigationProperty navigationProperty = type.getNavigationProperty(navigationPropertyName); writer.writeStartElement(XML_NAVIGATION_PROPERTY); writer.writeAttribute(XML_NAME, navigationPropertyName); writer.writeAttribute( XML_TYPE, getAliasedFullQualifiedName( navigationProperty.getType(), navigationProperty.isCollection())); if (!navigationProperty.isNullable()) { writer.writeAttribute(XML_NULLABLE, "" + navigationProperty.isNullable()); } if (navigationProperty.getPartner() != null) { EdmNavigationProperty partner = navigationProperty.getPartner(); writer.writeAttribute(XML_PARTNER, partner.getName()); } if (navigationProperty.containsTarget()) { writer.writeAttribute(XML_CONTAINS_TARGET, "" + navigationProperty.containsTarget()); } if (navigationProperty.getReferentialConstraints() != null) { for (EdmReferentialConstraint constraint : navigationProperty.getReferentialConstraints()) { writer.writeEmptyElement("ReferentialConstraint"); writer.writeAttribute(XML_PROPERTY, constraint.getPropertyName()); writer.writeAttribute("ReferencedProperty", constraint.getReferencedPropertyName()); } } writer.writeEndElement(); } }
private void appendTypeDefinitions( final XMLStreamWriter writer, final List<EdmTypeDefinition> typeDefinitions) throws XMLStreamException { for (EdmTypeDefinition definition : typeDefinitions) { writer.writeEmptyElement(XML_TYPE_DEFINITION); writer.writeAttribute(XML_NAME, definition.getName()); writer.writeAttribute( XML_UNDERLYING_TYPE, getFullQualifiedName(definition.getUnderlyingType(), false)); // Facets if (definition.getMaxLength() != null) { writer.writeAttribute(XML_MAX_LENGTH, "" + definition.getMaxLength()); } if (definition.getPrecision() != null) { writer.writeAttribute(XML_PRECISION, "" + definition.getPrecision()); } if (definition.getScale() != null) { writer.writeAttribute(XML_SCALE, "" + definition.getScale()); } } }
@Override public void marshallAsElement( AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException { resourceModel = resourceModel.get(attribute.getName()); if (wrapElement) { writer.writeStartElement(wrapperElement == null ? attribute.getName() : wrapperElement); } for (ModelNode property : resourceModel.asList()) { writer.writeEmptyElement(attribute.getXmlName()); writer.writeAttribute( org.jboss.as.controller.parsing.Attribute.NAME.getLocalName(), property.asProperty().getName()); writer.writeAttribute( org.jboss.as.controller.parsing.Attribute.VALUE.getLocalName(), property.asProperty().getValue().asString()); } if (wrapElement) { writer.writeEndElement(); } }
private void writeEmpty(final String name) throws XMLStreamException { xmlWriter.writeEmptyElement(name); }