Java源码示例:com.ctc.wstx.api.WstxOutputProperties

示例1
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());
}
 
示例2
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());
}
 
示例3
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;
}
 
示例4
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);
}
 
示例5
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());
}
 
示例6
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);
}
 
示例7
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;
}
 
示例8
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;
}
 
示例9
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));
}
 
示例10
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));
}
 
示例11
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;
   }
 
示例12
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);
}
 
示例13
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;
}
 
示例14
@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);

}
 
示例15
protected static void setValidateStructure(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE,
                  Boolean.valueOf(state));
}
 
示例16
protected static void setValidateContent(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_CONTENT,
                  Boolean.valueOf(state));
}
 
示例17
protected static void setValidateNames(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_NAMES,
                  Boolean.valueOf(state));
}
 
示例18
protected static void setFixContent(XMLOutputFactory f, boolean state)
{
    f.setProperty(WstxOutputProperties.P_OUTPUT_FIX_CONTENT,
                  Boolean.valueOf(state));
}
 
示例19
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));
}