List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement
public void writeEndElement() throws XMLStreamException;
From source file:com.liferay.portal.util.LocalizationImpl.java
public String updateLocalization(String xml, String key, String value, String requestedLanguageId, String defaultLanguageId, boolean cdata, boolean localized) { xml = _sanitizeXML(xml);// w w w . ja va 2 s . c o m XMLStreamReader xmlStreamReader = null; XMLStreamWriter xmlStreamWriter = null; ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); try { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(portalClassLoader); } XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml)); String availableLocales = StringPool.BLANK; // Read root node if (xmlStreamReader.hasNext()) { xmlStreamReader.nextTag(); availableLocales = xmlStreamReader.getAttributeValue(null, _AVAILABLE_LOCALES); if (Validator.isNull(availableLocales)) { availableLocales = defaultLanguageId; } if (availableLocales.indexOf(requestedLanguageId) == -1) { availableLocales = StringUtil.add(availableLocales, requestedLanguageId, StringPool.COMMA); } } UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(unsyncStringWriter); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeStartElement(_ROOT); if (localized) { xmlStreamWriter.writeAttribute(_AVAILABLE_LOCALES, availableLocales); xmlStreamWriter.writeAttribute(_DEFAULT_LOCALE, defaultLanguageId); } _copyNonExempt(xmlStreamReader, xmlStreamWriter, requestedLanguageId, defaultLanguageId, cdata); xmlStreamWriter.writeStartElement(key); if (localized) { xmlStreamWriter.writeAttribute(_LANGUAGE_ID, requestedLanguageId); } if (cdata) { xmlStreamWriter.writeCData(value); } else { xmlStreamWriter.writeCharacters(value); } xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); xmlStreamWriter = null; xml = unsyncStringWriter.toString(); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn(e, e); } } finally { if (contextClassLoader != portalClassLoader) { currentThread.setContextClassLoader(contextClassLoader); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (Exception e) { } } if (xmlStreamWriter != null) { try { xmlStreamWriter.close(); } catch (Exception e) { } } } return xml; }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleGetContainerAcl(HttpServletResponse response, BlobStore blobStore, String containerName) throws IOException { ContainerAccess access = blobStore.getContainerAccess(containerName); try (Writer writer = response.getWriter()) { XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer); xml.writeStartDocument();/* w w w . j a va 2s. c o m*/ xml.writeStartElement("AccessControlPolicy"); xml.writeDefaultNamespace(AWS_XMLNS); writeOwnerStanza(xml); xml.writeStartElement("AccessControlList"); xml.writeStartElement("Grant"); xml.writeStartElement("Grantee"); xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xml.writeAttribute("xsi:type", "CanonicalUser"); writeSimpleElement(xml, "ID", FAKE_OWNER_ID); writeSimpleElement(xml, "DisplayName", FAKE_OWNER_DISPLAY_NAME); xml.writeEndElement(); writeSimpleElement(xml, "Permission", "FULL_CONTROL"); xml.writeEndElement(); if (access == ContainerAccess.PUBLIC_READ) { xml.writeStartElement("Grant"); xml.writeStartElement("Grantee"); xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xml.writeAttribute("xsi:type", "Group"); writeSimpleElement(xml, "URI", "http://acs.amazonaws.com/groups/global/AllUsers"); xml.writeEndElement(); writeSimpleElement(xml, "Permission", "READ"); xml.writeEndElement(); } xml.writeEndElement(); xml.writeEndElement(); xml.flush(); } catch (XMLStreamException xse) { throw new IOException(xse); } }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleGetBlobAcl(HttpServletResponse response, BlobStore blobStore, String containerName, String blobName) throws IOException { BlobAccess access = blobStore.getBlobAccess(containerName, blobName); try (Writer writer = response.getWriter()) { XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer); xml.writeStartDocument();/* w w w.j av a 2 s . co m*/ xml.writeStartElement("AccessControlPolicy"); xml.writeDefaultNamespace(AWS_XMLNS); writeOwnerStanza(xml); xml.writeStartElement("AccessControlList"); xml.writeStartElement("Grant"); xml.writeStartElement("Grantee"); xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xml.writeAttribute("xsi:type", "CanonicalUser"); writeSimpleElement(xml, "ID", FAKE_OWNER_ID); writeSimpleElement(xml, "DisplayName", FAKE_OWNER_DISPLAY_NAME); xml.writeEndElement(); writeSimpleElement(xml, "Permission", "FULL_CONTROL"); xml.writeEndElement(); if (access == BlobAccess.PUBLIC_READ) { xml.writeStartElement("Grant"); xml.writeStartElement("Grantee"); xml.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xml.writeAttribute("xsi:type", "Group"); writeSimpleElement(xml, "URI", "http://acs.amazonaws.com/groups/global/AllUsers"); xml.writeEndElement(); writeSimpleElement(xml, "Permission", "READ"); xml.writeEndElement(); } xml.writeEndElement(); xml.writeEndElement(); xml.flush(); } catch (XMLStreamException xse) { throw new IOException(xse); } }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * Creates the representation of the {@link EnumDomain} * <code>EnumDomain</code>. This representation is a UML enumeration with * stereotype <code><<record>></code>. The constants of * <code>enumDomain</code> are represented as constants of the generated UML * enumeration. Further more all {@link Comment}s attached to * <code>enumDomain</code> are created. * /*w w w . j a v a2 s . c om*/ * @param writer * {@link XMLStreamWriter} of the current XMI file * @param enumDomain * {@link EnumDomain} the current {@link EnumDomain} * @throws XMLStreamException */ private void createEnum(XMLStreamWriter writer, EnumDomain enumDomain) throws XMLStreamException { // start packagedElement writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_ENUMERATION); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, enumDomain.get_qualifiedName()); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, extractSimpleName(enumDomain.get_qualifiedName())); // create comments createComments(writer, enumDomain); // create enumeration constants for (String enumConst : enumDomain.get_enumConstants()) { // create ownedLiteral writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_OWNEDLITERAL); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.OWNEDLITERAL_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, enumDomain.get_qualifiedName() + "_" + enumConst); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, enumConst); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.OWNEDLITERAL_ATTRIBUTE_CLASSIFIER, enumDomain.get_qualifiedName()); } // end packagedElement writer.writeEndElement(); }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * Creates the representation of an {@link Attribute}. The represented * information are the name, the default value and the type. <br/> * If the {@link Attribute} is of type LongDomain, a DoubleDomain, a * CollectionDomain or a MapDomain the type is stored in * {@link SchemaGraph2XMI#typesToBeDeclaredAtTheEnd} because there has to be * created a representation in the primitive types package. * //from ww w . j av a2 s .co m * @param writer * {@link XMLStreamWriter} of the current XMI file * @param attributeName * {@link String} the name of the current {@link Attribute} * @param defaultValue * {@link String} the default value of the current * {@link Attribute} * @param domain * {@link Domain} of the current {@link Attribute} * @param id * {@link String} the id of the tag which represents the current * {@link Attribute} * @throws XMLStreamException */ private void createAttribute(XMLStreamWriter writer, String attributeName, String defaultValue, Domain domain, String id) throws XMLStreamException { boolean hasDefaultValue = defaultValue != null && !defaultValue.isEmpty(); // start ownedAttribute if (!hasDefaultValue && !(domain instanceof BooleanDomain || domain instanceof IntegerDomain || domain instanceof StringDomain)) { writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_OWNEDATTRIBUTE); } else { writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_OWNEDATTRIBUTE); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, id); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, attributeName); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_VISIBILITY, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_VISIBILITY_VALUE_PRIVATE); // create type if (domain instanceof BooleanDomain || domain instanceof IntegerDomain || domain instanceof StringDomain) { createType(writer, domain); } else { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, domain.get_qualifiedName().replaceAll("\\s", "").replaceAll("<", "_").replaceAll(">", "_")); } // create default value if (hasDefaultValue) { createDefaultValue(writer, defaultValue, id, domain); } if (hasDefaultValue || domain instanceof BooleanDomain || domain instanceof IntegerDomain || domain instanceof StringDomain) { // end ownedAttribute writer.writeEndElement(); } // if domain is a LongDomain, a DoubleDomain, a CollectionDomain or a // MapDomain there has to be created an entry in the package // PrimitiveTypes if (domain instanceof LongDomain || domain instanceof DoubleDomain || domain instanceof CollectionDomain || domain instanceof MapDomain) { typesToBeDeclaredAtTheEnd.add(domain); } }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleContainerList(HttpServletResponse response, BlobStore blobStore) throws IOException { PageSet<? extends StorageMetadata> buckets = blobStore.list(); try (Writer writer = response.getWriter()) { XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer); xml.writeStartDocument();//from w w w.jav a2 s . com xml.writeStartElement("ListAllMyBucketsResult"); xml.writeDefaultNamespace(AWS_XMLNS); writeOwnerStanza(xml); xml.writeStartElement("Buckets"); for (StorageMetadata metadata : buckets) { xml.writeStartElement("Bucket"); writeSimpleElement(xml, "Name", metadata.getName()); Date creationDate = metadata.getCreationDate(); if (creationDate == null) { // Some providers, e.g., Swift, do not provide container // creation date. Emit a bogus one to satisfy clients like // s3cmd which require one. creationDate = new Date(0); } writeSimpleElement(xml, "CreationDate", blobStore.getContext().utils().date().iso8601DateFormat(creationDate).trim()); xml.writeEndElement(); } xml.writeEndElement(); xml.writeEndElement(); xml.flush(); } catch (XMLStreamException xse) { throw new IOException(xse); } }
From source file:jp.zippyzip.impl.GeneratorServiceImpl.java
/** * XML ??//from www. j a v a 2 s. com * * @param name "area" / "corp" * @param suffix "" / "c" */ void storeXml(String name, String suffix) { long timestamp = getLzhDao().getZipInfo().getTimestamp().getTime(); Collection<City> cities = getCities(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); ZipEntry entry = new ZipEntry(FILENAME_DATE_FORMAT.format(new Date(timestamp)) + "_" + name + "_utf8.xml"); entry.setTime(timestamp); int cnt = 0; try { zos.putNextEntry(entry); OutputStreamWriter writer = new OutputStreamWriter(zos, "UTF-8"); XMLStreamWriter xwriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); xwriter.writeStartDocument("UTF-8", "1.0"); xwriter.writeStartElement("zips"); xwriter.writeAttribute("type", name); for (City city : cities) { ParentChild pc = getParentChildDao().get(city.getCode() + suffix); if (pc == null) { continue; } for (String json : pc.getChildren()) { Zip zip = Zip.fromJson(json); xwriter.writeStartElement("zip"); xwriter.writeAttribute("zip", zip.getCode()); xwriter.writeAttribute("x0402", zip.getX0402()); xwriter.writeAttribute("add1", zip.getAdd1()); xwriter.writeAttribute("add2", zip.getAdd2()); xwriter.writeAttribute("corp", zip.getCorp()); xwriter.writeAttribute("add1Yomi", zip.getAdd1Yomi()); xwriter.writeAttribute("add2Yomi", zip.getAdd2Yomi()); xwriter.writeAttribute("corpYomi", zip.getCorpYomi()); xwriter.writeAttribute("note", zip.getNote()); xwriter.writeEndElement(); ++cnt; } } xwriter.writeEndElement(); xwriter.writeEndDocument(); xwriter.flush(); zos.closeEntry(); zos.finish(); getRawDao().store(baos.toByteArray(), name + "_utf8_xml.zip"); log.info("count: " + cnt); } catch (XMLStreamException e) { log.log(Level.WARNING, "", e); } catch (FactoryConfigurationError e) { log.log(Level.WARNING, "", e); } catch (IOException e) { log.log(Level.WARNING, "", e); } }
From source file:ca.uhn.fhir.parser.XmlParser.java
private void encodeUndeclaredExtensions(IBaseResource theResource, XMLStreamWriter theEventWriter, List<? extends IBaseExtension<?, ?>> theExtensions, String tagName, boolean theIncludedResource) throws XMLStreamException, DataFormatException { for (IBaseExtension<?, ?> next : theExtensions) { if (next == null || (ElementUtil.isEmpty(next.getValue()) && next.getExtension().isEmpty())) { continue; }//from ww w .j a va2s . co m writeCommentsPre(theEventWriter, next); theEventWriter.writeStartElement(tagName); String elementId = getCompositeElementId(next); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); } String url = next.getUrl(); theEventWriter.writeAttribute("url", url); if (next.getValue() != null) { IBaseDatatype value = next.getValue(); RuntimeChildUndeclaredExtensionDefinition extDef = myContext .getRuntimeChildUndeclaredExtensionDefinition(); String childName = extDef.getChildNameByDatatype(value.getClass()); BaseRuntimeElementDefinition<?> childDef; if (childName == null) { childDef = myContext.getElementDefinition(value.getClass()); if (childDef == null) { throw new ConfigurationException( "Unable to encode extension, unrecognized child element type: " + value.getClass().getCanonicalName()); } else { childName = RuntimeChildUndeclaredExtensionDefinition.createExtensionChildName(childDef); } } else { childDef = extDef.getChildElementDefinitionByDatatype(value.getClass()); if (childDef == null) { throw new ConfigurationException( "Unable to encode extension, unrecognized child element type: " + value.getClass().getCanonicalName()); } } encodeChildElementToStreamWriter(theResource, theEventWriter, value, childName, childDef, null, theIncludedResource, null); } // child extensions encodeExtensionsIfPresent(theResource, theEventWriter, next, theIncludedResource); theEventWriter.writeEndElement(); writeCommentsPost(theEventWriter, next); } }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleListMultipartUploads(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore, String container) throws IOException, S3Exception { if (request.getParameter("delimiter") != null || request.getParameter("prefix") != null || request.getParameter("max-uploads") != null || request.getParameter("key-marker") != null || request.getParameter("upload-id-marker") != null) { throw new UnsupportedOperationException(); }/*from ww w . jav a 2 s . com*/ List<MultipartUpload> uploads = blobStore.listMultipartUploads(container); try (Writer writer = response.getWriter()) { XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(writer); xml.writeStartDocument(); xml.writeStartElement("ListMultipartUploadsResult"); xml.writeDefaultNamespace(AWS_XMLNS); writeSimpleElement(xml, "Bucket", container); // TODO: bogus values xml.writeEmptyElement("KeyMarker"); xml.writeEmptyElement("UploadIdMarker"); xml.writeEmptyElement("NextKeyMarker"); xml.writeEmptyElement("NextUploadIdMarker"); xml.writeEmptyElement("Delimiter"); xml.writeEmptyElement("Prefix"); writeSimpleElement(xml, "MaxUploads", "1000"); writeSimpleElement(xml, "IsTruncated", "false"); for (MultipartUpload upload : uploads) { xml.writeStartElement("Upload"); writeSimpleElement(xml, "Key", upload.blobName()); writeSimpleElement(xml, "UploadId", upload.id()); writeInitiatorStanza(xml); writeOwnerStanza(xml); writeSimpleElement(xml, "StorageClass", "STANDARD"); // TODO: bogus value writeSimpleElement(xml, "Initiated", blobStore.getContext().utils().date().iso8601DateFormat(new Date())); xml.writeEndElement(); } // TODO: CommonPrefixes xml.writeEndElement(); xml.flush(); } catch (XMLStreamException xse) { throw new IOException(xse); } }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * This method creates an <code>extension</code> tag. It is used in profile * application and to create stereotypes of the form * <code><<keyValue>></code>. In the first case a * <code>references</code> child is created in the other a * <code>details</code> child. To create the first one <code>nelement</code> * has to be <code>null</code>. * /*from w w w. jav a 2s . c o m*/ * @param writer * {@link XMLStreamWriter} of the current XMI file * @param nelement * {@link NamedElement} if null <code>references</code> is * created otherwise the stereotype <code>keyValue</code>. * @param keyValue * {@link String} the stereotype to be created * @throws XMLStreamException */ private void createExtension(XMLStreamWriter writer, NamedElement nelement, String keyValue) throws XMLStreamException { // start Extension writer.writeStartElement(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_TAG_EXTENSION); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_EXTENDER, XMIConstants4SchemaGraph2XMI.NAMESPACE_ECORE); // start eAnnotations writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_EANNOTATIONS); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.EANNOTATIONS_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, nelement != null ? nelement.get_qualifiedName() + "_" + XMIConstants4SchemaGraph2XMI.TAG_EANNOTATIONS : XMIConstants4SchemaGraph2XMI.TAG_EANNOTATIONS + System.currentTimeMillis()); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.EANNOTATIONS_ATTRIBUTE_SOURCE, XMIConstants4SchemaGraph2XMI.EANNOTATIONS_ATTRIBUTE_SOURCE_VALUE); if (nelement != null) { // write details writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_DETAILS); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.DETAILS_ATTRIBUTE_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, nelement.get_qualifiedName() + "_" + XMIConstants4SchemaGraph2XMI.TAG_DETAILS); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.DETAILS_ATTRIBUTE_KEY, keyValue); } else { // write references writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_REFERENCES); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.REFERENCES_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_HREF, XMIConstants4SchemaGraph2XMI.REFERENCES_HREF_VALUE); } // close eAnnotations writer.writeEndElement(); // close Extension writer.writeEndElement(); }