Example usage for javax.xml.stream XMLStreamWriter setDefaultNamespace

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

Introduction

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

Prototype

public void setDefaultNamespace(String uri) throws XMLStreamException;

Source Link

Document

Binds a URI to the default namespace This URI is bound in the scope of the current START_ELEMENT / END_ELEMENT pair.

Usage

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void startDocument(final XMLStreamWriter writer, final String rootElement) throws XMLStreamException {
    writer.writeStartDocument();//www  . j  a  v a  2  s .  c om
    writer.setDefaultNamespace(Constants.NS_ATOM);
    writer.writeStartElement(rootElement);

    namespaces(writer);
}

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void entity(final Writer outWriter, final Entity entity)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    if (entity.getType() == null && entity.getProperties().isEmpty()) {
        writer.writeStartDocument();//from www .  j  a  v  a 2s  .c  o  m
        writer.setDefaultNamespace(namespaceMetadata);
        entityRef(writer, entity);
    } else {
        startDocument(writer, Constants.ATOM_ELEM_ENTRY);
        entity(writer, entity);
    }
    writer.writeEndDocument();
    writer.flush();
}

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void entity(final Writer outWriter, final ResWrap<Entity> container)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final Entity entity = container.getPayload();

    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    if (entity.getType() == null && entity.getProperties().isEmpty()) {
        writer.writeStartDocument();//from   w  w w .ja  v  a 2 s.  c o  m
        writer.setDefaultNamespace(namespaceMetadata);

        entityRef(writer, container);
    } else {
        startDocument(writer, Constants.ATOM_ELEM_ENTRY);

        addContextInfo(writer, container);

        entity(writer, entity);
    }

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
}

From source file:org.apache.olingo.commons.core.data.AtomSerializer.java

private void startDocument(final XMLStreamWriter writer, final String rootElement) throws XMLStreamException {
    writer.writeStartDocument();/*from w  ww.  j  a  v a 2  s.c  o m*/
    writer.setDefaultNamespace(Constants.NS_ATOM);

    writer.writeStartElement(rootElement);

    namespaces(writer);
}

From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java

private void entity(final Writer outWriter, final Entity entity)
        throws XMLStreamException, EdmPrimitiveTypeException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    if (entity.getType() == null && entity.getProperties().isEmpty()) {
        writer.writeStartDocument();//  w w w. j  a  v  a2  s  .c o  m
        writer.setDefaultNamespace(namespaceMetadata);

        entityRef(writer, entity);
    } else {
        startDocument(writer, Constants.ATOM_ELEM_ENTRY);

        entity(writer, entity);
    }

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
}

From source file:org.deegree.geometry.wkbadapter.WKBReaderTest.java

@Test
public void testWKBToGML() throws Exception {
    ICRS crs = CRSManager.lookup("EPSG:4326");

    InputStream is = WKBReaderTest.class.getResourceAsStream(BASE_DIR + "Polygon.wkb");
    byte[] wkb = IOUtils.toByteArray(is);

    Polygon geom = (Polygon) WKBReader.read(wkb, crs);
    assertTrue(geom.getGeometryType() == GeometryType.PRIMITIVE_GEOMETRY);

    StringWriter sw = new StringWriter();
    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    outFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    XMLStreamWriter writer = outFactory.createXMLStreamWriter(sw);
    writer.setDefaultNamespace(CommonNamespaces.GML3_2_NS);
    GMLStreamWriter gmlSw = GMLOutputFactory.createGMLStreamWriter(GMLVersion.GML_32, writer);
    gmlSw.write(geom);// ww  w .j  a v a2s. co  m
    writer.close();

    String s = "<gml:posList>5.148530 59.951879 5.134692 59.736522 5.561175 59.728897 5.577771 59.944188 5.148530 59.951879</gml:posList>";
    assertTrue(sw.toString().contains(s));
}

From source file:org.deegree.tools.services.wms.FeatureTypesToLayerTree.java

/**
 * @param args/*from   w w  w. ja va2 s .c  o m*/
 */
public static void main(String[] args) {
    Options options = initOptions();

    // for the moment, using the CLI API there is no way to respond to a help argument; see
    // https://issues.apache.org/jira/browse/CLI-179
    if (args.length == 0 || (args.length > 0 && (args[0].contains("help") || args[0].contains("?")))) {
        CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null);
    }

    XMLStreamWriter out = null;
    try {
        CommandLine line = new PosixParser().parse(options, args);

        String storeFile = line.getOptionValue("f");
        String nm = new File(storeFile).getName();
        String storeId = nm.substring(0, nm.length() - 4);

        FileOutputStream os = new FileOutputStream(line.getOptionValue("o"));
        XMLOutputFactory fac = XMLOutputFactory.newInstance();
        out = new IndentingXMLStreamWriter(fac.createXMLStreamWriter(os));
        out.setDefaultNamespace(ns);

        Workspace ws = new DefaultWorkspace(new File("nix"));
        ws.initAll();
        DefaultResourceIdentifier<FeatureStore> identifier = new DefaultResourceIdentifier<FeatureStore>(
                FeatureStoreProvider.class, "unknown");
        ws.add(new DefaultResourceLocation<FeatureStore>(new File(storeFile), identifier));
        ws.prepare(identifier);
        FeatureStore store = ws.init(identifier, null);

        AppSchema schema = store.getSchema();

        // prepare document
        out.writeStartDocument();
        out.writeStartElement(ns, "deegreeWMS");
        out.writeDefaultNamespace(ns);
        out.writeAttribute("configVersion", "0.5.0");
        out.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        out.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation",
                "http://www.deegree.org/services/wms http://schemas.deegree.org/wms/0.5.0/wms_configuration.xsd");
        out.writeStartElement(ns, "ServiceConfiguration");

        HashSet<FeatureType> visited = new HashSet<FeatureType>();

        if (schema.getRootFeatureTypes().length == 1) {
            writeLayer(visited, out, schema.getRootFeatureTypes()[0], storeId);
        } else {
            out.writeCharacters("\n");
            out.writeStartElement(ns, "UnrequestableLayer");
            XMLAdapter.writeElement(out, ns, "Title", "Root Layer");
            for (FeatureType ft : schema.getRootFeatureTypes()) {
                writeLayer(visited, out, ft, storeId);
            }
            out.writeEndElement();
            out.writeCharacters("\n");
        }

        out.writeEndElement();
        out.writeEndElement();
        out.writeEndDocument();
    } catch (ParseException exp) {
        System.err.println(Messages.getMessage("TOOL_COMMANDLINE_ERROR", exp.getMessage()));
        CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null);
    } catch (ResourceInitException e) {
        LOG.info("The feature store could not be loaded: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (FileNotFoundException e) {
        LOG.info("A file could not be found: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (XMLStreamException e) {
        LOG.info("The XML output could not be written: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (FactoryConfigurationError e) {
        LOG.info("The XML system could not be initialized: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (XMLStreamException e) {
                LOG.trace("Stack trace:", e);
            }
        }
    }

}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * Append a rec:RecordList element describing the given record list to the given XML stream. The element contains a
 * namespace declaration for xmlns:rec.//from   w w  w . ja  v  a2s  . co m
 *
 * @param staxWriter
 *          target XML stream
 * @param records
 *          the record list to transform.
 * @throws XMLStreamException
 *           StAX error
 *
 */
public void writeRecordList(final XMLStreamWriter staxWriter, final Iterable<Record> records)
        throws XMLStreamException {
    staxWriter.setDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    writeStartElement(staxWriter, RecordBuilder.TAG_RECORDLIST);
    staxWriter.writeDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    newline(staxWriter);
    for (final Record record : records) {
        writeRecord(staxWriter, record, false);
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.record.stax.RecordWriter.java

/**
 * write a record to an XML stream.//from   ww  w.  j  a  va2 s  .  com
 *
 * @param staxWriter
 *          target XML stream
 * @param record
 *          the record to write
 * @param addNamespace
 *          add xmlns attribute
 * @throws XMLStreamException
 *           StAX error
 */
private void writeRecord(final XMLStreamWriter staxWriter, final Record record, final boolean addNamespace)
        throws XMLStreamException {
    staxWriter.setDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    writeStartElement(staxWriter, RecordBuilder.TAG_RECORD);
    if (addNamespace) {
        staxWriter.writeDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    }
    staxWriter.writeAttribute(RecordBuilder.ATTRIBUTE_VERSION, RecordBuilder.SCHEMA_VERSION_RECORD);
    newline(staxWriter);
    if (record.getId() != null) {
        _idWriter.writeId(staxWriter, record.getId());
        staxWriter.setDefaultNamespace(RecordBuilder.NAMESPACE_RECORD);
    }

    if (record.getMetadata() != null) {
        writeMetadata(staxWriter, record.getMetadata());
    }
    if (record.hasAttachments()) {
        final Iterator<String> attachmentNames = record.getAttachmentNames();
        while (attachmentNames.hasNext()) {
            final String attachmentName = attachmentNames.next();
            writeTextElement(staxWriter, RecordBuilder.TAG_ATTACHMENT, attachmentName);
        }
    }
    writeEndElement(staxWriter);
}

From source file:org.eclipse.smila.datamodel.xml.StaxRecordWriter.java

/**
 * Append a rec:RecordList element describing the given record list to the given XML stream. The element contains a
 * namespace declaration for xmlns:rec./*from  w  w  w. ja v  a2s .co m*/
 * 
 * @param staxWriter
 *          target XML stream
 * @param records
 *          the record list to transform.
 * @throws XMLStreamException
 *           StAX error
 * 
 */
public void writeRecordList(final XMLStreamWriter staxWriter, final Iterable<Record> records)
        throws XMLStreamException {
    staxWriter.setDefaultNamespace(XmlConstants.NAMESPACE_RECORD);
    writeStartElement(staxWriter, XmlConstants.TAG_RECORDLIST);
    staxWriter.writeDefaultNamespace(XmlConstants.NAMESPACE_RECORD);
    newline(staxWriter);
    for (final Record record : records) {
        writeRecord(staxWriter, record, false);
    }
    writeEndElement(staxWriter);
}