com.ctc.wstx.api.WstxOutputProperties Java Examples

The following examples show how to use com.ctc.wstx.api.WstxOutputProperties. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestEmptyElementWriter.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testHTML() throws Exception
{
    XMLOutputFactory f = getOutputFactory();
    f.setProperty(WstxOutputProperties.P_OUTPUT_EMPTY_ELEMENT_HANDLER,
            EmptyElementHandler.HtmlEmptyElementHandler.getInstance());
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = f.createXMLStreamWriter(sw);
    w.writeStartElement("root");
    w.writeStartElement("a");
    w.writeEndElement();
    w.writeStartElement("br");
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    w.close();
    assertEquals("<root><a></a><br/></root>", sw.toString());
}
 
Example #2
Source File: TestEmptyElementWriter.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testSimple() throws Exception
{
    XMLOutputFactory f = getOutputFactory();
    // test with simple handler that lists explicitly all tags to close
    Set<String> tags = new HashSet<String> ();
    tags.add("a");
    f.setProperty(WstxOutputProperties.P_OUTPUT_EMPTY_ELEMENT_HANDLER,
            new EmptyElementHandler.SetEmptyElementHandler(tags));
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = f.createXMLStreamWriter(sw);
    w.writeStartElement("root");
    w.writeStartElement("a");
    w.writeEndElement();
    w.writeStartElement("b");
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    w.close();
    assertEquals("<root><a/><b></b></root>", sw.toString());
}
 
Example #3
Source File: BasicSerializableRepository.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private XmlMapper createXMLMapper() {
    final XMLInputFactory ifactory = new WstxInputFactory();
    ifactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);
    ifactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

    final XMLOutputFactory ofactory = new WstxOutputFactory();
    ofactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);
    ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final XmlFactory xf = new XmlFactory(ifactory, ofactory);

    final XmlMapper mapper = new XmlMapper(xf);
    mapper.registerModules(new JavaTimeModule());
    return mapper;
}
 
Example #4
Source File: TestOutputFactory.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void testMisc()
    throws XMLStreamException
{
    /* This is silly, but coverage testing is not happy that our
     * constant-defining classes are never constructed. So here we go,
     * just to mark it off the list...
     */
    WstxInputProperties fooin = new WstxInputProperties();
    WstxOutputProperties fooout = new WstxOutputProperties();
 
    // These just to keep compilers/FindBugs etc happy
    assertNotNull(fooin);
    assertNotNull(fooout);
}
 
Example #5
Source File: TestInvalidChars.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLOutputFactory2 getFactory(Character replChar)
       throws XMLStreamException
   {
       XMLOutputFactory2 f = getOutputFactory();
       setRepairing(f, false);
       setValidateContent(f, true);
f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
	      (replChar == null) ? null : new InvalidCharHandler.ReplacingHandler(replChar.charValue()));
       return f;
   }
 
Example #6
Source File: TestWriterProperties.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void testAccessWriter()
    throws XMLStreamException
{
    XMLOutputFactory f = getOutputFactory();
    StringWriter strw = new StringWriter();
    XMLStreamWriter sw = f.createXMLStreamWriter(strw);

    assertSame(strw, sw.getProperty(WstxOutputProperties.P_OUTPUT_UNDERLYING_WRITER));
}
 
Example #7
Source File: TestWriterProperties.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void testAccessStream()
    throws XMLStreamException
{
    XMLOutputFactory f = getOutputFactory();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLStreamWriter sw = f.createXMLStreamWriter(bos, "UTF-8");

    assertSame(bos, sw.getProperty(WstxOutputProperties.P_OUTPUT_UNDERLYING_STREAM));
}
 
Example #8
Source File: TestEscaping.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLOutputFactory2 getFactory(int type, boolean escapeCr)
    throws XMLStreamException
{
    XMLOutputFactory2 f = getOutputFactory();
    // type 0 -> non-ns, 1 -> ns, non-repairing, 2 -> ns, repairing
    setNamespaceAware(f, type > 0); 
    setRepairing(f, type > 1); 

    f.setProperty(WstxOutputProperties.P_OUTPUT_ESCAPE_CR, escapeCr ? Boolean.TRUE : Boolean.FALSE);

    return f;
}
 
Example #9
Source File: TestAutoEndElems.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLOutputFactory getFactory(boolean autoEndElems)
    throws XMLStreamException
{
    XMLOutputFactory f = getOutputFactory();
    // ns-awareness, repairing shouldn't matter, just whether automatic end elems enabled
    f.setProperty(WstxOutputProperties.P_AUTOMATIC_END_ELEMENTS, autoEndElems ? Boolean.TRUE : Boolean.FALSE);
    return f;
}
 
Example #10
Source File: TestOptions.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLStreamWriter getWriter(boolean addSpace, Writer sw, OutputStream out, String enc)
    throws IOException, XMLStreamException
{
    XMLOutputFactory f = getOutputFactory();
    f.setProperty(WstxOutputProperties.P_ADD_SPACE_AFTER_EMPTY_ELEM,
                  Boolean.valueOf(addSpace));
    if (sw != null) {
        return f.createXMLStreamWriter(sw);
    }
    return f.createXMLStreamWriter(out, enc);
}
 
Example #11
Source File: FormattingXmlStreamWriter.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
private FormattingXmlStreamWriter(XMLStreamWriter writer, OutputFormat output) {
    this.output = output;
    this.writer = writer;
    this.rawWriter = (Writer) writer.getProperty(WstxOutputProperties.P_OUTPUT_UNDERLYING_WRITER);
    if (this.rawWriter == null) {
        throw new IllegalStateException("Could not get underlying writer!");
    }
    this.elementIndentingXmlWriter = new IndentingXMLStreamWriter(writer);
    this.elementIndentingXmlWriter.setIndentStep(output.getIndent());
}
 
Example #12
Source File: FormattingXmlStreamWriter.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
public static FormattingXmlStreamWriter create(OutputStream output, OutputFormat format)
        throws XMLStreamException, FactoryConfigurationError {
    // always use WoodstoX
    XMLOutputFactory factory = new WstxOutputFactory();
    factory.setProperty(WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
    return new FormattingXmlStreamWriter(factory, output, format);
}
 
Example #13
Source File: BasicSerializableRepository.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private XmlMapper createXMLMapper() {
    final XMLInputFactory ifactory = new WstxInputFactory();
    ifactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);
    ifactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

    final XMLOutputFactory ofactory = new WstxOutputFactory();
    ofactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);
    ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final XmlFactory xf = new XmlFactory(ifactory, ofactory);

    final XmlMapper mapper = new XmlMapper(xf);
    mapper.registerModules(new JavaTimeModule());
    return mapper;
}
 
Example #14
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected static void setFixContent(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_FIX_CONTENT,
                  Boolean.valueOf(state));
}
 
Example #15
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected static void setValidateNames(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES,
                  Boolean.valueOf(state));
}
 
Example #16
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected static void setValidateContent(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT,
                  Boolean.valueOf(state));
}
 
Example #17
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected static void setValidateStructure(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE,
                  Boolean.valueOf(state));
}
 
Example #18
Source File: TestOutputFactory.java    From woodstox with Apache License 2.0 4 votes vote down vote up
public void testConfig()
    throws XMLStreamException
{
    XMLOutputFactory2 f = getNewOutputFactory();

    WriterConfig cfg = ((WstxOutputFactory) f).getConfig();
    assertNotNull(cfg);

    assertFalse(f.isPropertySupported("foobar"));

    // Let's just test some of known properties that should be supported...
    assertTrue(f.isPropertySupported(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE));
    assertTrue(f.isPropertySupported(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT));

    // And their default values?
    assertEquals(Boolean.TRUE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE));
    assertEquals(Boolean.TRUE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT));

    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_ATTR));
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES));
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT));
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_COPY_DEFAULT_ATTRS));

    // As per [WSTX-120], default with Woodstox 4.0 is false:
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_FIX_CONTENT));
    assertEquals(Boolean.TRUE, f.getProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS));
    assertEquals(Boolean.TRUE, f.getProperty(XMLStreamProperties.XSP_NAMESPACE_AWARE));

    assertNull(f.getProperty(XMLStreamProperties.XSP_PROBLEM_REPORTER));
    assertNull(f.getProperty(XMLOutputFactory2.P_TEXT_ESCAPER));
    assertNull(f.getProperty(XMLOutputFactory2.P_ATTR_VALUE_ESCAPER));

    // ... which can be changed
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, Boolean.FALSE);
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE));

    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT, Boolean.FALSE);
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT));

    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT, Boolean.FALSE);
    assertEquals(Boolean.FALSE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT));

    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES, Boolean.TRUE);
    assertEquals(Boolean.TRUE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES));
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_ATTR, Boolean.TRUE);
    assertEquals(Boolean.TRUE, f.getProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_ATTR));
}
 
Example #19
Source File: XmlModule.java    From proteus with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {

    XMLInputFactory inputFactory = new WstxInputFactory();
    inputFactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);

    bind(XMLInputFactory.class).toInstance(inputFactory);

    XMLOutputFactory outputFactory = new WstxOutputFactory();
    outputFactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);

    bind(XMLOutputFactory.class).toInstance(outputFactory);

    XmlFactory xmlFactory = new XmlFactory(inputFactory, outputFactory);

    XmlMapper xmlMapper = new XmlMapper(xmlFactory);
    xmlMapper.registerModule(new JavaTimeModule())
             .registerModule(new ParameterNamesModule())
             .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
             .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    xmlMapper.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);

    bind(XmlMapper.class).toInstance(xmlMapper);

}