Example usage for org.dom4j.io SAXReader setStripWhitespaceText

List of usage examples for org.dom4j.io SAXReader setStripWhitespaceText

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader setStripWhitespaceText.

Prototype

public void setStripWhitespaceText(boolean stripWhitespaceText) 

Source Link

Document

Sets whether whitespace between element start and end tags should be ignored.

Usage

From source file:com.anite.zebra.ext.xmlloader.XMLLoadProcess.java

License:Apache License

/**
 * Read in a file of XML, strip out the pretty printing via some juggling of objects and then return a
 * org.w3.document DOM object./*from   w  w w .j  a v  a2 s  .  co m*/
 * 
 * @param xmlFile
 *            The file to be read in.
 * @return A ready to use org.w3.Document with pretty printing stripped out.
 * @throws DocumentException
 * @throws MalformedURLException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
private Document readDocument(File xmlFile)
        throws DocumentException, MalformedURLException, UnsupportedEncodingException, IOException {
    SAXReader xmlReader = new SAXReader();
    xmlReader.setStripWhitespaceText(true);

    org.dom4j.Document dom4jDocument = xmlReader.read(xmlFile);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputFormat format = OutputFormat.createCompactFormat();
    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(dom4jDocument);

    dom4jDocument = DocumentHelper.parseText(baos.toString());

    DOMWriter domWriter = new DOMWriter();
    Document doc = domWriter.write(dom4jDocument);
    return doc;
}

From source file:com.bplow.netconn.base.net.Request.java

License:Open Source License

static Request parse(ByteBuffer bb) throws MalformedRequestException, UnsupportedEncodingException {

    byte[] requestMessage = bb.array();
    String requestMsg = new String(requestMessage, "GBK");
    System.out.println("??:" + requestMsg);
    String tradeType = null;//from  w ww  .  j a va2 s  .com

    SAXReader reader = new SAXReader();
    reader.setStripWhitespaceText(true);
    Document document;
    try {
        document = reader.read(new ByteArrayInputStream(requestMsg.trim().getBytes()));
        Iterator orderIt = document.selectNodes("/TX/TX_CODE").iterator();
        while (orderIt.hasNext()) {
            Element elem = (Element) orderIt.next();
            tradeType = elem.getText();
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    int i = 0;
    String outNo = "";
    String jyteype = "";

    /*42+2 22*/
    byte[] type = new byte[2];
    //bb.get(type, 44, 2);

    while (bb.hasRemaining()) {
        byte c = bb.get();
        //int devIdInt = Integer.parseInt(devId);
        //String devIdString = Integer.toHexString(c);
        String hex = Integer.toHexString(c & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        if (i >= 41 && i < 44) {
            outNo = outNo + hex;
        }
        if (i >= 44 && i < 46) {
            jyteype = jyteype + hex;
        }
        i++;
        System.out.print(hex.toUpperCase() + " ");
    }

    System.out.println("");
    System.out.println("" + tradeType);
    System.out.println(outNo);
    //outOrderNo = outNo;
    /*CharBuffer cb = ascii.decode(bb);
    Matcher m = requestPattern.matcher(cb);
    if (!m.matches())
       throw new MalformedRequestException();
    Action a;
    try {
       a = Action.parse(m.group(1));
    } catch (IllegalArgumentException x) {
       throw new MalformedRequestException();
    }
    URI u;
    try {
       u = new URI("http://" + m.group(4) + m.group(2));
    } catch (URISyntaxException x) {
       throw new MalformedRequestException();
    }
    return new Request(a, m.group(3), u);*/
    return new Request(outNo, tradeType);
}

From source file:com.cladonia.xml.webservice.soap.SOAPDialog.java

License:Open Source License

private String format(Reader reader) throws SAXParseException, IOException {
    SAXReader sax = XMLUtilities.createReader(false);
    sax.setStripWhitespaceText(false);
    sax.setMergeAdjacentText(false);/* w  ww . j a v a2  s .c o m*/

    XDocument doc = XMLUtilities.parse(sax, new BufferedReader(reader), null);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    String indent = "";

    ExchangerOutputFormat format = new ExchangerOutputFormat();
    format.setEncoding(doc.getEncoding());

    XMLFormatter formatter = new XMLFormatter(out, format);

    EditorProperties properties = config.getEditorProperties();
    switch (properties.getFormatType()) {
    case EditorProperties.FORMAT_CUSTOM:
        if (properties.isCustomIndent()) {
            format.setIndent(TextPreferences.getTabString());
        } else {
            format.setIndent("");
        }

        format.setNewlines(properties.isCustomNewline());
        format.setPadText(properties.isCustomPadText());

        formatter.setTrimText(properties.isCustomStrip());
        formatter.setPreserveMixedContent(properties.isCustomPreserveMixedContent());
        break;
    case EditorProperties.FORMAT_COMPACT:
        format.setIndent("");
        format.setNewlines(false);
        format.setPadText(false);

        formatter.setTrimText(true);
        formatter.setPreserveMixedContent(false);
        break;
    case EditorProperties.FORMAT_STANDARD:
        format.setIndent(TextPreferences.getTabString());

        format.setNewlines(true);
        format.setPadText(false);

        formatter.setTrimText(true);
        formatter.setPreserveMixedContent(true);
        break;
    }

    formatter.write(doc);

    return out.toString(doc.getEncoding());
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Creates a new SAXReader.//w w  w. j  a va2 s  .  c o m
 *
 * @param validate when true the reader validates the input.
 *
 * @return the reader.
 */
public static SAXReader createReader(boolean validate, boolean loadExternalDTD) {
    SAXReader reader = new SAXReader(XDocumentFactory.getInstance(), validate);

    reader.setStripWhitespaceText(false);
    reader.setMergeAdjacentText(true);
    //      reader.setMergeAdjacentText( true);

    if (!validate) {
        reader.setIncludeExternalDTDDeclarations(false);
        reader.setIncludeInternalDTDDeclarations(true);

        try {
            if (loadExternalDTD) {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
                //               System.out.println( "http://apache.org/xml/features/nonvalidating/load-external-dtd = "+reader.getXMLReader().getFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd"));
                reader.setEntityResolver(getCatalogResolver());
            } else {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            reader.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return reader;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Creates a new SAXReader./*from  ww  w  .  jav a  2 s. c  o  m*/
 *
 * @param validate when true the reader validates the input.
 *
 * @return the reader.
 */
public static SAXReader createReader(boolean validate, boolean loadExternalDTD, boolean stripWhiteSpace) {
    SAXReader reader = new SAXReader(XDocumentFactory.getInstance(), validate);

    reader.setStripWhitespaceText(stripWhiteSpace);
    reader.setMergeAdjacentText(true);
    //      reader.setMergeAdjacentText( true);

    if (!validate) {
        reader.setIncludeExternalDTDDeclarations(false);
        reader.setIncludeInternalDTDDeclarations(true);

        try {
            if (loadExternalDTD) {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
                //               System.out.println( "http://apache.org/xml/features/nonvalidating/load-external-dtd = "+reader.getXMLReader().getFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd"));
                reader.setEntityResolver(getCatalogResolver());
            } else {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            reader.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return reader;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Parses the document for this URL, does not substitute &amp; 
 * and does resolve all XIncludes.// www  . j  a  v a  2 s  . co  m
 *
 * @param url the URL of the document.
 *
 * @return the Dom4J document.
 */
public static synchronized XDocument parseWithoutSubstitution(URL url) throws IOException, SAXParseException {
    if (DEBUG)
        System.out.println("XMLUtilities.parseWithoutSubstitution( " + url + ")");
    XDocument document = null;
    InputStream stream = URLUtilities.open(url);
    XMLReader reader = new XMLReader(stream);

    // create parser that resolves XIncludes.
    SAXParser parser = new SAXParser(new XIncludeParserConfiguration());
    SAXReader saxer = new SAXReader(parser);

    // create parser that does not resolves XIncludes.
    // SAXReader saxer = createReader( false);
    saxer.setStripWhitespaceText(true);
    saxer.setMergeAdjacentText(true);

    try {
        document = parse(saxer, reader, url.toString());
    } finally {
        stream.close();
        reader.close();
    }

    return document;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

public static String format(String text, String systemId, String encoding, String indent, boolean newLines,
        boolean padText, int lineLength, boolean trim, boolean preserveMixed, ExchangerOutputFormat format)
        throws IOException, SAXParseException {

    ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes(mapXMLEncodingToJava(encoding)));
    InputStreamReader reader = new InputStreamReader(stream, mapXMLEncodingToJava(encoding));

    SAXReader sax = createReader(false, false);
    sax.setStripWhitespaceText(false);
    sax.setMergeAdjacentText(false);/*  w  w  w.j  a  va 2s.  c  o  m*/

    XDocument doc = XMLUtilities.parse(sax, new BufferedReader(reader), systemId);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    //      OutputFormat format = new OutputFormat();
    //      format.setEncoding( doc.getEncoding());

    XMLFormatter formatter = new XMLFormatter(out, format);

    format.setIndent(indent);
    format.setNewlines(newLines);
    format.setPadText(padText);
    formatter.setMaxLineLength(lineLength);
    formatter.setTrimText(trim);
    formatter.setPreserveMixedContent(preserveMixed);

    formatter.write(doc);
    formatter.flush();

    text = out.toString(mapXMLEncodingToJava(encoding));

    stream = new ByteArrayInputStream(text.getBytes(mapXMLEncodingToJava(encoding)));
    reader = new InputStreamReader(stream, mapXMLEncodingToJava(encoding));

    sax = createReader(false, false);
    sax.setStripWhitespaceText(false);
    sax.setMergeAdjacentText(false);

    doc = XMLUtilities.parse(sax, new BufferedReader(reader), systemId);

    out = new ByteArrayOutputStream();
    ExchangerOutputFormat format2 = new ExchangerOutputFormat("", false, encoding);

    if (!format.isSuppressDeclaration()) {
        format2.setStandalone(format.getStandalone());
        format2.setOmitStandalone(format.isOmitStandalone());

        format2.setVersion(format.getVersion());
        format2.setOmitEncoding(format.isOmitEncoding());
        format2.setSuppressDeclaration(false);
    } else {
        format2.setSuppressDeclaration(true);
    }

    ExchangerXMLWriter writer = new ExchangerXMLWriter(out, format2);
    writer.write(doc);
    writer.flush();

    return out.toString(mapXMLEncodingToJava(encoding));
}

From source file:edu.ku.brc.helpers.XMLHelper.java

License:Open Source License

/**
 * Reads a DOM from a stream/*from   w w  w.  jav  a 2 s .  c  o m*/
 * @param fileinputStream the stream to be read
 * @return the root element of the DOM
 */
public static org.dom4j.Element readFileToDOM4J(final FileInputStream fileinputStream) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);
    saxReader.setStripWhitespaceText(true);

    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    //saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    //saxReader.setFeature("http://xml.org/sax/features/validation", false);
    //saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    org.dom4j.Document document = saxReader.read(fileinputStream);
    return document.getRootElement();
}

From source file:edu.ku.brc.helpers.XMLHelper.java

License:Open Source License

public static org.dom4j.Element readStrToDOM4J(final String data) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);//  w w w .  ja  va2 s. c  om
    saxReader.setStripWhitespaceText(true);
    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    //saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    //saxReader.setFeature("http://xml.org/sax/features/validation", false);
    //saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    org.dom4j.Document document = saxReader.read(new StringReader(data));
    return document.getRootElement();
}

From source file:edu.ku.brc.specify.tools.AppendHelp.java

License:Open Source License

/**
 * Reads a DOM from a stream// w  ww  .j a  v a 2 s. co  m
 * @param fileinputStream the stream to be read
 * @return the root element of the DOM
 */
public Element readFileToDOM4J(final File file) throws IOException, DocumentException {
    SAXReader saxReader = new SAXReader();

    try {
        saxReader.setValidation(false);
        saxReader.setStripWhitespaceText(true);
        //saxReader.setIncludeExternalDTDDeclarations(false);
        //saxReader.setIncludeInternalDTDDeclarations(false);
        saxReader.setIgnoreComments(true);
        //saxReader.setXMLFilter(new TransparentFilter(saxReader.getXMLReader()));

        EntityResolver entityResolver = new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                return new InputSource("");
            }
        };
        saxReader.setEntityResolver(entityResolver);

        //saxReader.getXMLFilter().setDTDHandler(null);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    org.dom4j.Document document = saxReader.read(new FileInputStream(file));
    return document.getRootElement();
}