下面列出了javax.xml.stream.XMLStreamWriter#writeDefaultNamespace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", name.getLocalPart(), name.getNamespaceURI());
w.writeDefaultNamespace(name.getNamespaceURI());
if (mustUnderstand) {
//Writing the ns declaration conditionally checking in the NSContext breaks XWSS. as readHeader() adds ns declaration,
// where as writing alonf with the soap envelope does n't add it.
//Looks like they expect the readHeader() and writeTo() produce the same infoset, Need to understand their usage
//if(w.getNamespaceContext().getPrefix(soapVersion.nsUri) == null) {
w.writeNamespace("S", soapVersion.nsUri);
w.writeAttribute("S", soapVersion.nsUri, MUST_UNDERSTAND, getMustUnderstandLiteral(soapVersion));
// } else {
// w.writeAttribute(soapVersion.nsUri,MUST_UNDERSTAND, getMustUnderstandLiteral(soapVersion));
// }
}
w.writeCharacters(value);
w.writeEndElement();
}
public static XMLStreamWriter startElement(OutputStream outputStream, String rootName, String typeName,
String context, boolean defaultNameSpace) throws XMLStreamException {
XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream, UTF_8.name());
writer.writeStartElement(METADATA, rootName, ODATA_METADATA_NS);
if (defaultNameSpace) {
LOG.debug("Starting {} element with default data namespace", rootName);
writer.setDefaultNamespace(ODATA_DATA_NS);
writer.writeDefaultNamespace(ODATA_DATA_NS);
} else {
LOG.debug("Starting {} element without default namespaces", rootName);
writer.writeNamespace(ODATA_DATA, ODATA_DATA_NS);
}
writer.writeNamespace(METADATA, ODATA_METADATA_NS);
writer.writeAttribute(ODATA_METADATA_NS, ODATA_CONTEXT, context);
if (!PrimitiveType.STRING.getName().equals(typeName)) {
writer.writeAttribute(ODATA_METADATA_NS, TYPE, typeName);
}
return writer;
}
public void append(final XMLStreamWriter writer, final EntityInfoAggregator entityInfo, final List<Map<String, Object>> data) throws EntityProviderException {
try {
writer.writeStartElement(FormatXml.D_LINKS);
writer.writeDefaultNamespace(Edm.NAMESPACE_D_2007_08);
if (properties.getInlineCount() != null) {
writer.writeStartElement(Edm.PREFIX_M, FormatXml.M_COUNT, Edm.NAMESPACE_M_2007_08);
writer.writeNamespace(Edm.PREFIX_M, Edm.NAMESPACE_M_2007_08);
writer.writeCharacters(properties.getInlineCount().toString());
writer.writeEndElement();
}
XmlLinkEntityProducer provider = new XmlLinkEntityProducer(properties);
for (final Map<String, Object> entityData : data) {
provider.append(writer, entityInfo, entityData, false);
}
writer.writeEndElement();
writer.flush();
} catch (final XMLStreamException e) {
throw new EntityProviderException(EntityProviderException.COMMON, e);
}
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", getLocalPart(), getNamespaceURI());
w.writeDefaultNamespace(getNamespaceURI());
w.writeStartElement(actionLocalName);
w.writeCharacters(action);
w.writeEndElement();
if (soapAction != null) {
w.writeStartElement(soapActionLocalName);
w.writeCharacters(soapAction);
w.writeEndElement();
}
w.writeEndElement();
}
public void append(final XMLStreamWriter writer, final EntityInfoAggregator eia,
final List<Map<String, Object>> data, final boolean isInline) throws EntityProviderException {
try {
writer.writeStartElement(FormatXml.ATOM_FEED);
TombstoneCallback callback = null;
if (!isInline) {
writer.writeDefaultNamespace(Edm.NAMESPACE_ATOM_2005);
writer.writeNamespace(Edm.PREFIX_M, Edm.NAMESPACE_M_2007_08);
writer.writeNamespace(Edm.PREFIX_D, Edm.NAMESPACE_D_2007_08);
callback = getTombstoneCallback();
if (callback != null) {
writer.writeNamespace(TombstoneCallback.PREFIX_TOMBSTONE, TombstoneCallback.NAMESPACE_TOMBSTONE);
}
}
writer.writeAttribute(Edm.PREFIX_XML, Edm.NAMESPACE_XML_1998, FormatXml.XML_BASE, properties.getServiceRoot()
.toASCIIString());
// write all atom infos (mandatory and optional)
appendAtomMandatoryParts(writer, eia);
appendAtomSelfLink(writer, eia);
if (properties.getInlineCountType() == InlineCount.ALLPAGES) {
appendInlineCount(writer, properties.getInlineCount());
}
appendEntries(writer, eia, data);
if (callback != null) {
appendDeletedEntries(writer, eia, callback);
}
if (properties.getNextLink() != null) {
appendNextLink(writer, properties.getNextLink());
}
writer.writeEndElement();
} catch (XMLStreamException e) {
throw new EntityProviderProducerException(EntityProviderException.COMMON, e);
}
}
@Override
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", name.getLocalPart(), name.getNamespaceURI());
w.writeDefaultNamespace(name.getNamespaceURI());
if (type != null)
w.writeAttribute("type", type);
w.writeCharacters(value);
w.writeEndElement();
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", getLocalPart(), getNamespaceURI());
w.writeDefaultNamespace(getNamespaceURI());
w.writeStartElement(actionLocalName);
w.writeCharacters(action);
w.writeEndElement();
if (soapAction != null) {
w.writeStartElement(soapActionLocalName);
w.writeCharacters(soapAction);
w.writeEndElement();
}
w.writeEndElement();
}
@Override
public void outputNamespaceDeclarations(XMLStreamWriter w) throws XMLStreamException
{
String[] ns = mNamespaces;
for (int i = mFirstLocalNs, len = mNsLength; i < len; i += 2) {
String nsURI = ns[i+1];
String prefix = ns[i];
if (prefix != null && prefix.length() > 0) {
w.writeNamespace(prefix, nsURI);
} else {
w.writeDefaultNamespace(nsURI);
}
}
}
private void appendSchema(final XMLStreamWriter writer, final EdmSchema schema) throws XMLStreamException {
writer.writeStartElement(NS_EDM, SCHEMA);
writer.writeDefaultNamespace(NS_EDM);
writer.writeAttribute(XML_NAMESPACE, schema.getNamespace());
if (schema.getAlias() != null) {
writer.writeAttribute(XML_ALIAS, schema.getAlias());
namespaceToAlias.put(schema.getNamespace(), schema.getAlias());
}
// EnumTypes
appendEnumTypes(writer, schema.getEnumTypes());
// TypeDefinitions
appendTypeDefinitions(writer, schema.getTypeDefinitions());
// EntityTypes
appendEntityTypes(writer, schema.getEntityTypes());
// ComplexTypes
appendComplexTypes(writer, schema.getComplexTypes());
// Actions
appendActions(writer, schema.getActions());
// Functions
appendFunctions(writer, schema.getFunctions());
appendTerms(writer, schema.getTerms());
// EntityContainer
appendEntityContainer(writer, schema.getEntityContainer());
// AnnotationGroups
appendAnnotationGroups(writer, schema.getAnnotationGroups());
appendAnnotations(writer, schema);
writer.writeEndElement();
}
@Override
public void handle ( final DurationEntry entry )
{
try
{
final XMLOutputFactory xml = XMLOutputFactory.newInstance ();
final Path name = makeNextName ();
try ( OutputStream stream = new BufferedOutputStream ( Files.newOutputStream ( name, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE ) ) )
{
final XMLStreamWriter xsw = xml.createXMLStreamWriter ( stream );
xsw.writeStartDocument ();
xsw.writeCharacters ( "\n\n" );
xsw.writeStartElement ( "trace" );
xsw.writeDefaultNamespace ( "http://packagedrone.org/profile/trace/v1.0" );
xsw.writeCharacters ( "\n" );
dumpEntry ( xsw, entry, 1 );
xsw.writeEndElement ();
xsw.writeCharacters ( "\n" ); // end white new line
xsw.writeEndDocument ();
}
if ( !Boolean.getBoolean ( "drone.profile.xml.disableAnnounce" ) )
{
System.out.format ( "Wrote profile trace: %s ms - %s -> %s%n", entry.getDuration ().toMillis (), entry.getOperation (), name.toAbsolutePath () );
}
}
catch ( final Exception e )
{
logger.warn ( "Failed to write profile trace", e );
}
}
/**
* This serializes the xml payload feed
* @param writer
* @param eia
* @param data
* @param isInline
* @throws EntityProviderException
*/
public void append(final XMLStreamWriter writer, final EntityInfoAggregator eia,
final EntityCollection data, final boolean isInline) throws EntityProviderException {
try {
if (properties.getServiceRoot() == null) {
throw new EntityProviderProducerException(EntityProviderException.MANDATORY_WRITE_PROPERTY);
}
writer.writeStartElement(FormatXml.ATOM_FEED);
if (!isInline) {
writer.writeDefaultNamespace(Edm.NAMESPACE_ATOM_2005);
writer.writeNamespace(Edm.PREFIX_M, Edm.NAMESPACE_M_2007_08);
writer.writeNamespace(Edm.PREFIX_D, Edm.NAMESPACE_D_2007_08);
}
writer.writeAttribute(Edm.PREFIX_XML, Edm.NAMESPACE_XML_1998, FormatXml.XML_BASE, properties.getServiceRoot()
.toASCIIString());
// write all atom infos (mandatory and optional)
appendAtomMandatoryParts(writer, eia);
appendAtomSelfLink(writer, eia);
appendEntries(writer, eia, data);
writer.writeEndElement();
} catch (XMLStreamException e) {
throw new EntityProviderProducerException(EntityProviderException.COMMON, e);
}
}
@Override
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", name.getLocalPart(), name.getNamespaceURI());
w.writeDefaultNamespace(name.getNamespaceURI());
if (type != null)
w.writeAttribute("type", type);
w.writeCharacters(value);
w.writeEndElement();
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", getLocalPart(), getNamespaceURI());
w.writeDefaultNamespace(getNamespaceURI());
w.writeStartElement(actionLocalName);
w.writeCharacters(action);
w.writeEndElement();
if (soapAction != null) {
w.writeStartElement(soapActionLocalName);
w.writeCharacters(soapAction);
w.writeEndElement();
}
w.writeEndElement();
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", av.faultDetailTag.getLocalPart(), av.faultDetailTag.getNamespaceURI());
w.writeDefaultNamespace(av.nsUri);
w.writeStartElement("", wrapper, av.nsUri);
w.writeCharacters(problemValue);
w.writeEndElement();
w.writeEndElement();
}
private void writeBooksElem(XMLStreamWriter writer, List<Book> books) throws XMLStreamException {
writer.writeStartDocument("utf-8", "1.0");
writer.writeComment("Describes list of books");
writer.setDefaultNamespace(NS);
writer.writeStartElement(NS, "books");
writer.writeDefaultNamespace(NS);
for (Book book : books)
writeBookElem(writer, book);
writer.writeEndElement();
writer.writeEndDocument();
}
private static File createConfigFileWithColors(Boolean enable, int timeout,
Boolean validate, Boolean outputJSON, Boolean colorOutput, String error,
String warn, String success, String required, String batch) {
File f = new File(TestSuiteEnvironment.getTmpDir(), "test-jboss-cli" +
System.currentTimeMillis() + ".xml");
f.deleteOnExit();
String namespace = Namespace.CURRENT.getUriString();
XMLOutputFactory output = XMLOutputFactory.newInstance();
try (Writer stream = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
XMLStreamWriter writer = output.createXMLStreamWriter(stream);
writer.writeStartDocument();
writer.writeStartElement("jboss-cli");
writer.writeDefaultNamespace(namespace);
writer.writeStartElement("echo-command");
writer.writeCharacters(enable.toString());
writer.writeEndElement(); //echo-command
if (timeout != 0) {
writer.writeStartElement("command-timeout");
writer.writeCharacters("" + timeout);
writer.writeEndElement(); //command-timeout
}
writer.writeStartElement("validate-operation-requests");
writer.writeCharacters(validate.toString());
writer.writeEndElement(); //validate-operation-requests
writer.writeStartElement("output-json");
writer.writeCharacters(outputJSON.toString());
writer.writeEndElement(); //output-json
CliConfigUtils.writeColorConfig(writer, colorOutput, error, warn, success, required, batch);
writer.writeEndElement(); //jboss-cli
writer.writeEndDocument();
writer.flush();
writer.close();
} catch (XMLStreamException | IOException ex) {
fail("Failure creating config file " + ex);
}
return f;
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", getLocalPart(), getNamespaceURI());
w.writeDefaultNamespace(getNamespaceURI());
w.writeStartElement(actionLocalName);
w.writeCharacters(action);
w.writeEndElement();
if (soapAction != null) {
w.writeStartElement(soapActionLocalName);
w.writeCharacters(soapAction);
w.writeEndElement();
}
w.writeEndElement();
}
public void writeResultElement(XMLStreamWriter xmlWriter, String name, ParamValue value,
QName xsdType, int categoryType, int resultType, ExternalParamCollection params)
throws XMLStreamException {
if (xmlWriter == null) {
return;
}
String nsPrefix;
boolean writeNS;
switch (resultType) {
case DBConstants.ResultTypes.XML:
xmlWriter.writeStartElement(name);
/* write default namespace */
nsPrefix = xmlWriter.getNamespaceContext().getPrefix(this.getNamespace());
writeNS = nsPrefix == null || !"".equals(nsPrefix);
if (writeNS) {
xmlWriter.setDefaultNamespace(namespace);
xmlWriter.writeDefaultNamespace(namespace);
}
if (value != null) {
this.writeElementValue(xmlWriter, value);
}
xmlWriter.writeEndElement();
break;
case DBConstants.ResultTypes.RDF:
switch (categoryType) {
case DBConstants.DataCategory.VALUE:
/* <productCode rdf:datatype="http://www.w3.org/2001/XMLSchema#string">S10_1678</productCode> */
xmlWriter.writeStartElement(this.getNamespace(), name);
String dataTypeString = xsdType.getNamespaceURI() + "#" + xsdType.getLocalPart();
xmlWriter.writeAttribute(DBConstants.RDF_NAMESPACE,
DBConstants.DBSFields.RDF_DATATYPE, dataTypeString);
this.writeElementValue(xmlWriter, value);
xmlWriter.writeEndElement();
break;
case DBConstants.DataCategory.REFERENCE:
/* <productLine rdf:resource="http://productLines.com/Motorcycles/"/> */
try {
xmlWriter.writeStartElement(this.getNamespace(), name);
String evalValue = DBUtils.evaluateString(value.toString(), params);
xmlWriter.writeAttribute(DBConstants.RDF_NAMESPACE,
DBConstants.DBSFields.RDF_RESOURCE, evalValue);
xmlWriter.writeEndElement();
} catch (DataServiceFault e) {
throw new XMLStreamException("Error in writing result element using RDF", e);
}
break;
}
}
}
private void startDocumentEmptyDefaultNamespace(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement("root");
xmlStreamWriter.writeDefaultNamespace("");
}
protected static File createConfigFile(Boolean enable, int timeout,
Boolean validate, Boolean outputJSON, Boolean colorOutput, Boolean outputPaging) {
File f = new File(TestSuiteEnvironment.getTmpDir(), "test-jboss-cli" +
System.currentTimeMillis() + ".xml");
f.deleteOnExit();
String namespace = Namespace.CURRENT.getUriString();
XMLOutputFactory output = XMLOutputFactory.newInstance();
try (Writer stream = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
XMLStreamWriter writer = output.createXMLStreamWriter(stream);
writer.writeStartDocument();
writer.writeStartElement("jboss-cli");
writer.writeDefaultNamespace(namespace);
writer.writeStartElement("echo-command");
writer.writeCharacters(enable.toString());
writer.writeEndElement(); //echo-command
if (timeout != 0) {
writer.writeStartElement("command-timeout");
writer.writeCharacters("" + timeout);
writer.writeEndElement(); //command-timeout
}
writer.writeStartElement("validate-operation-requests");
writer.writeCharacters(validate.toString());
writer.writeEndElement(); //validate-operation-requests
writer.writeStartElement("output-json");
writer.writeCharacters(outputJSON.toString());
writer.writeEndElement(); //output-json
writeColorConfig(writer, colorOutput, "", "", "", "", "");
writer.writeStartElement("output-paging");
writer.writeCharacters(outputPaging.toString());
writer.writeEndElement(); //echo-command
writer.writeEndElement(); //jboss-cli
writer.writeEndDocument();
writer.flush();
writer.close();
} catch (XMLStreamException | IOException ex) {
fail("Failure creating config file " + ex);
}
return f;
}