Example usage for javax.xml.stream XMLStreamWriter setNamespaceContext

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

Introduction

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

Prototype

public void setNamespaceContext(NamespaceContext context) throws XMLStreamException;

Source Link

Document

Sets the current namespace context for prefix and uri bindings.

Usage

From source file:eu.esdihumboldt.hale.io.xslt.XslTransformationUtil.java

/**
 * Setup a XML writer configured with the namespace prefixes and UTF-8
 * encoding./*  w  w w. j av  a 2 s  . c o m*/
 * 
 * @param outStream the output stream to write the XML content to
 * @param namespaces the namespace context, e.g. as retrieved from a
 *            {@link XsltGenerationContext}
 * @return the XML stream writer
 * @throws XMLStreamException if an error occurs setting up the writer
 */
public static XMLStreamWriter setupXMLWriter(OutputStream outStream, NamespaceContext namespaces)
        throws XMLStreamException {
    // create and set-up a writer
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    // will set namespaces if these not set explicitly
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", //$NON-NLS-1$
            Boolean.valueOf(true));
    // create XML stream writer with UTF-8 encoding
    XMLStreamWriter tmpWriter = outputFactory.createXMLStreamWriter(outStream, "UTF-8"); //$NON-NLS-1$

    tmpWriter.setNamespaceContext(namespaces);

    return new IndentingXMLStreamWriter(tmpWriter);
}

From source file:org.unitedinternet.cosmo.dav.CosmoDavException.java

public void writeTo(XMLStreamWriter writer) throws XMLStreamException {
    writer.setNamespaceContext(nsc);
    writer.writeStartElement("DAV:", "error");
    for (String uri : nsc.getNamespaceURIs()) {
        writer.writeNamespace(nsc.getPrefix(uri), uri);
    }/*from ww w  .  java  2  s  .c om*/
    writeContent(writer);
    writer.writeEndElement();
}

From source file:org.apache.maven.dotnet.stylecop.StyleCopGenerator.java

/**
 * Generates the msbuild configuration for a project.
 * /* www. j  a v  a 2 s  . c om*/
 * @param stream
 */
public void generate(OutputStream stream) {
    Project project = new Project();

    // Properties used
    PropertyGroup propGroup = new PropertyGroup();
    propGroup.setProjectRoot(toWindowsPath(projectRoot));
    propGroup.setStyleCopRoot(toWindowsPath(styleCopRoot));

    // StyleCop task definition
    UsingTask usingTask = new UsingTask();
    usingTask.setAssemblyFile("$(StyleCopRoot)\\Microsoft.StyleCop.dll");
    usingTask.setTaskName("StyleCopTask");

    // StyleCop execution target
    Target target = new Target();
    target.setName("CheckStyle");
    StyleCopTask task = new StyleCopTask();
    task.setFullPath(toWindowsPath(visualSolution));
    task.setOutputFile(toWindowsPath(output));
    task.setSettingsFile(toWindowsPath(settings));
    task.setSourceFiles("@(SourceAnalysisFiles);@(CSFile)");

    // Builds the creation item
    CreateItem createItem = new CreateItem();
    createItem.setInclude("%(Project.RootDir)%(Project.Directory)**\\*.cs");
    ItemOutput itemOutput = new ItemOutput();
    itemOutput.setTaskParameter("Include");
    itemOutput.setItemName("SourceAnalysisFiles");
    createItem.setOutput(itemOutput);

    //
    ItemGroup group = new ItemGroup();
    // Adds all the projects files
    for (File visualProject : visualProjects) {
        if (visualProject.isDirectory()) {
            group.addCsFiles(visualProject + "\\**\\*.cs");
        } else {
            group.addProject(toWindowsPath(visualProject));
        }
    }

    // Populates the task
    target.setItem(createItem);
    target.setStyleCopTask(task);

    // Finishes the project
    project.setUsingTask(usingTask);
    project.setPropertyGroup(propGroup);
    project.setDefaultTargets("CheckStyle");
    project.setToolsVersion("3.5");
    project.addItem(group);
    project.addTarget(target);

    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLStreamWriter xtw = null;
    try {
        // Gets control of the generated namespaces
        xtw = xof.createXMLStreamWriter(writer);
        xtw.setNamespaceContext(new NamespaceContext() {

            @Override
            public Iterator getPrefixes(String arg0) {
                return null;
            }

            @Override
            public String getPrefix(String arg0) {
                if (STYLE_COP_NAMESPACE.equals(arg0)) {
                    return "stylecop";
                }
                return null;
            }

            @Override
            public String getNamespaceURI(String arg0) {
                return null;
            }
        });
        // Establish a jaxb context
        JAXBContext jc = JAXBContext.newInstance(Project.class);

        // Get a marshaller
        Marshaller m = jc.createMarshaller();

        // Enable formatted xml output
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // Marshal to system output: java to xml
        m.marshal(project, xtw);
    } catch (Exception e) {
        log.debug("Generation error", e);
    }
    String xmlContent = writer.toString();

    // Due to a bug of missing feature in JAXB, I could not generate an XML file
    // having the default XML namespace
    // of stylecop (it defines the objects in a namespace that is not the
    // default).
    // The problem is that MSBuild is non fully XML compliant and requires the
    // stylecop namespace as being explicitely
    // the default one for the file. This is achived by hand-made replacement in
    // hte generated file, hoping for something
    // more robust later.
    String temp = StringUtils.replace(xmlContent, "xmlns=\"\"", "xmlns=\"" + STYLE_COP_NAMESPACE + "\"");
    String result = StringUtils.replace(temp, "stylecop:Project", "Project");
    PrintWriter outputWriter = new PrintWriter(stream);
    outputWriter.print(result);
    outputWriter.flush();
}

From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java

/**
 * @param wrt/*from w  ww.j a v a  2s. c  o  m*/
 * @throws XMLStreamException 
 */
private void exportPrologue(XMLStreamWriter wrt) throws XMLStreamException {
    wrt.setDefaultNamespace(SynchronizationConstants.EXPORT_NAMESPACE);
    wrt.setPrefix(SynchronizationConstants.EXPORT_NAMESPACE_PREFIX, SynchronizationConstants.EXPORT_NAMESPACE);
    wrt.setPrefix(SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE_PREFIX,
            SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE);
    wrt.setPrefix(SynchronizationConstants.CONFIGURATION_NAMESPACE_PREFIX,
            SynchronizationConstants.CONFIGURATION_NAMESPACE);

    NamespaceContext nsContext = SynchronizationConstants.createConfigurationExportNamespaceContext();

    wrt.setNamespaceContext(nsContext);

    wrt.writeStartDocument();
    wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
            SynchronizationConstants.CONFIGURATION_EXPORT_ELEMENT);
    wrt.writeNamespace(SynchronizationConstants.CONFIGURATION_INSTANCE_NAMESPACE_PREFIX,
            ConfigurationInstanceDescriptorUtil.NS_CONFIGURATION_INSTANCE);
    wrt.writeNamespace(SynchronizationConstants.CONFIGURATION_NAMESPACE_PREFIX,
            SynchronizationConstants.CONFIGURATION_NAMESPACE);

    writeValidators(wrt);
}