Example usage for org.dom4j.io OutputFormat setTrimText

List of usage examples for org.dom4j.io OutputFormat setTrimText

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setTrimText.

Prototype

public void setTrimText(boolean trimText) 

Source Link

Document

This will set whether the text is output verbatim (false) or with whitespace stripped as per org.dom4j.Element#getTextTrim() .

Usage

From source file:architecture.common.license.io.LicenseReader.java

License:Apache License

public String prettyPrintLicense(String decryptedLicenseKey) throws LicenseException {
    try {// w  w  w .jav  a2s . c  o  m
        StringReader reader = new StringReader(decryptedLicenseKey);
        org.dom4j.Document doc = (new SAXReader()).read(reader);
        reader.close();
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setNewlines(true);
        outputFormat.setTrimText(false);
        StringWriter writer = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        xmlWriter.write(doc);
        writer.close();
        return writer.toString();
    } catch (Exception e) {
        throw new LicenseException(e);
    }
}

From source file:au.com.acegi.xmlformat.XmlFormatPlugin.java

License:Apache License

private OutputFormat buildFormatter() {
    final OutputFormat fmt = createPrettyPrint();
    fmt.setAttributeQuoteCharacter(attributeQuoteChar);
    fmt.setEncoding(encoding);/* ww  w  .j a  va2s  .  c  om*/
    fmt.setExpandEmptyElements(expandEmptyElements);
    fmt.setIndentSize(indentSize);
    fmt.setLineSeparator(determineLineSeparator());
    fmt.setNewLineAfterDeclaration(newLineAfterDeclaration);
    fmt.setNewLineAfterNTags(newLineAfterNTags);
    fmt.setNewlines(newlines);
    fmt.setOmitEncoding(omitEncoding);
    fmt.setPadText(padText);
    fmt.setSuppressDeclaration(suppressDeclaration);
    fmt.setTrimText(trimText);
    fmt.setXHTML(xhtml);
    return fmt;
}

From source file:bio.pih.genoogle.interfaces.Console.java

public void execute(InputStreamReader isr, boolean echo) {
    BufferedReader lineReader = new BufferedReader(isr);

    boolean executePrev = false;
    String prev = null;//  w w  w.j a v  a2s .  c  o m
    String line = null;

    Map<Parameter, Object> consoleParameters = SearchParams.getSearchParamsMap();

    System.out.print("genoogle console> ");

    try {
        while (running && (executePrev || (line = lineReader.readLine()) != null)) {
            long begin = System.currentTimeMillis();
            long end = -1;

            if (echo) {
                System.out.println(line);
            }
            try {
                line = line.trim();
                if (line.length() == 0) {
                    continue;
                }

                if (executePrev) {
                    if (prev == null) {
                        System.out.println("no previous commands.");
                        executePrev = false;
                        continue;
                    }
                    line = prev;
                    System.out.println(line);
                    executePrev = false;
                }

                String[] commands = line.split("[ \t]+");
                if (commands[0].equals(SEARCH)) {
                    if (commands.length >= 3) {
                        String db = commands[1];
                        String queryFile = commands[2];

                        Map<Parameter, Object> searchParameters = Maps.newHashMap();
                        searchParameters.putAll(consoleParameters);

                        for (int i = 4; i < commands.length; i++) {
                            String command = commands[i];
                            String[] split = command.split("=");
                            if (split.length != 2) {
                                System.out.println(command + " is an invalid parameter.");
                            }
                            String paramName = split[0];
                            String paramValue = split[1];

                            Parameter p = Parameter.getParameterByName(paramName);
                            if (p == null) {
                                System.out.println(paramName + " is an invalid parameter name");
                                continue;
                            }
                            Object value = p.convertValue(paramValue);
                            searchParameters.put(p, value);
                        }

                        if (new File(queryFile).exists()) {
                            BufferedReader in = new BufferedReader(new FileReader(queryFile));
                            profileLogger.info("<" + line + ">");
                            List<SearchResults> results = genoogle.doBatchSyncSearch(in, db, searchParameters);
                            end = System.currentTimeMillis();
                            long total = end - begin;
                            profileLogger.info("</" + line + ":" + total + ">");
                            Document document = Output.genoogleOutputToXML(results);
                            OutputFormat outformat = OutputFormat.createPrettyPrint();
                            outformat.setTrimText(false);
                            outformat.setEncoding("UTF-8");
                            OutputStream os;
                            if (commands.length >= 4) {
                                String outputFile = commands[3];
                                os = new FileOutputStream(new File(outputFile + ".xml"));
                            } else {
                                os = System.out;
                            }
                            XMLWriter writer = new XMLWriter(os, outformat);
                            writer.write(document);
                            writer.flush();

                        } else {
                            System.err.println("query file: " + queryFile + " does not exist.");
                        }

                    } else {
                        System.out.println("SEARCH DB QUERY_FILE OUTPUT_FILE");
                    }

                } else if (commands[0].equals(GC)) {
                    System.gc();

                } else if (commands[0].equals(LIST)) {
                    for (AbstractSequenceDataBank db : genoogle.getDatabanks()) {
                        System.out.println(db.getName() + " - " + db.getAlphabet().getName() + "("
                                + db.getClass().getName() + ")");
                    }

                } else if (commands[0].equals(DEFAULT)) {
                    System.out.println(genoogle.getDefaultDatabank());

                } else if (commands[0].equals(PARAMETERS)) {
                    for (Entry<Parameter, Object> entry : consoleParameters.entrySet()) {
                        System.out.println(entry.getKey().getName() + "=" + entry.getValue());
                    }

                } else if (commands[0].equals(SET)) {
                    String[] split = commands[1].split("=");
                    if (split.length != 2) {
                        System.out.println(commands[1] + " is invalid set parameters option.");
                    }
                    String paramName = split[0];
                    String paramValue = split[1];

                    Parameter p = Parameter.getParameterByName(paramName);
                    if (p == null) {
                        System.out.println(paramName + " is an invalid parameter name");
                        continue;
                    }
                    Object value = p.convertValue(paramValue);
                    consoleParameters.put(p, value);
                    System.out.println(paramName + " is " + paramValue);

                } else if (commands[0].equals(SEQ)) {
                    if (commands.length != 3) {
                        System.out.println("SEQ database id");
                        continue;
                    }

                    String db = commands[1];
                    int id = Integer.parseInt(commands[2]);

                    String seq = genoogle.getSequence(db, id);

                    System.out.println(seq);

                } else if (commands[0].equals(PREV) || commands[0].equals("p")) {
                    executePrev = true;
                    continue;

                } else if (commands[0].endsWith(BATCH)) {
                    if (commands.length != 2) {
                        System.out.println("BATCH <batchfile>");
                        continue;
                    }

                    File f = new File(commands[1]);
                    execute(new InputStreamReader(new FileInputStream(f)), true);
                    end = System.currentTimeMillis();

                } else if (commands[0].equals(EXIT)) {
                    genoogle.finish();

                } else if (commands[0].equals(HELP)) {
                    System.out.println("Commands:");
                    System.out.println(
                            "search <data bank> <input file> <output file> <parameters>: does the search");
                    System.out.println("list : lists the data banks.");
                    System.out.println("parameters : shows the search parameters and their values.");
                    System.out.println("set <parameter>=<value> : set the parameters value.");
                    System.out.println("gc : executes the java garbage collection.");
                    System.out.println("prev or l: executes the last command.");
                    System.out.println("batch <batch file> : runs the commands listed in this batch file.");
                    System.out.println("help: this help.");
                    System.out.println("exit : finish Genoogle execution.");
                    System.out.println();
                    System.out.println("Search Parameters:");

                    System.out.println(
                            "MaxSubSequenceDistance : maximum index entries distance to be considered in the same HSPs.");
                    System.out.println("SequencesExtendDropoff : drop off for sequence extension.");
                    System.out.println("MaxHitsResults : maximum quantity of returned results.");
                    System.out.println("QuerySplitQuantity : how many slices the input query will be divided.");
                    System.out.println("MinQuerySliceLength : minimum size of each input query slice.");
                    System.out.println(
                            "MaxThreadsIndexSearch : quantity of threads which will be used to index search.");
                    System.out.println(
                            "MaxThreadsExtendAlign : quantity of threads which will be used to extend and align the HSPs.");
                    System.out.println("MatchScore : score when has a match at the alignment.");
                    System.out.println("MismatchScore : score when has a mismatch at the alignment.");
                } else {
                    System.err.println("Unknow command: " + commands[0]);
                    continue;
                }

                prev = line;
                System.out.print("genoogle console> ");
            } catch (IndexOutOfBoundsException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (UnsupportedEncodingException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (FileNotFoundException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (ParseException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (IOException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (NoSuchElementException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (UnknowDataBankException e) {
                logger.error(e.getStackTrace(), e);
                continue;
            } catch (InterruptedException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (ExecutionException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            } catch (IllegalSymbolException e) {
                logger.fatal(e.getStackTrace(), e);
                continue;
            }
        }
    } catch (IOException e) {
        logger.fatal(e.getStackTrace(), e);
        return;
    }
}

From source file:bio.pih.genoogle.interfaces.WebServices.java

private String xmlToString(Document doc) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setTrimText(false);
    XMLWriter writer = null;/*from  w  ww  .j  av a 2s.c  om*/

    try {
        writer = new XMLWriter(outputStream, outformat);
    } catch (UnsupportedEncodingException e) {
        logger.fatal(e);
        return e.getLocalizedMessage();
    }

    try {
        writer.write(doc);
    } catch (IOException e) {
        logger.fatal(e);
        return e.getLocalizedMessage();
    }

    try {
        return outputStream.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.fatal(e);
        return e.getLocalizedMessage();
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?/*from w ww  .  j  a v  a 2 s .com*/
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outStream
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    //
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(true);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

public static void element2XML(Element element, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    ///*  ww  w . j  a v a 2 s  .  com*/
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(false);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(element);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.jswiff.xml.XMLWriter.java

License:Open Source License

/**
 * Writes the XML document generated from the SWF to a stream. If the
 * <code>format</code> flag is set, the output XML is formatted to make it
 * more readable.//www .  j av  a2 s.  c om
 *
 * @param stream the target stream
 * @param format specifies whether to format the output XML
 *
 * @throws IOException if an I/O error occured
 */
public void write(OutputStream stream, boolean format) throws IOException {
    if (format) {
        OutputFormat formatter = OutputFormat.createPrettyPrint();
        formatter.setNewLineAfterDeclaration(false);
        formatter.setTrimText(false);
        org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(stream, formatter);
        writer.write(xmlDocument);
    } else {
        org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(stream);
        writer.write(xmlDocument);
    }
}

From source file:com.jswiff.xml.XMLWriter.java

License:Open Source License

/**
 * Writes the XML document generated from the SWF to a writer. If the
 * <code>format</code> flag is set, the output XML is formatted to make it
 * more readable./*from   w w w  . j  a  v a 2 s  .  com*/
 *
 * @param writer the target writer
 * @param format specifies whether to format the output XML
 *
 * @throws IOException if an I/O error occured
 */
public void write(Writer writer, boolean format) throws IOException {
    if (format) {
        OutputFormat formatter = OutputFormat.createPrettyPrint();
        formatter.setNewLineAfterDeclaration(false);
        formatter.setTrimText(false);
        org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(writer, formatter);
        xmlWriter.write(xmlDocument);
    } else {
        org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(writer);
        xmlWriter.write(xmlDocument);
    }
}

From source file:com.liferay.petra.xml.Dom4jUtil.java

License:Open Source License

public static String toString(Node node, String indent, boolean expandEmptyElements, boolean trimText)
        throws IOException {

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    OutputFormat outputFormat = OutputFormat.createPrettyPrint();

    outputFormat.setExpandEmptyElements(expandEmptyElements);
    outputFormat.setIndent(indent);//from ww  w  . java2 s. c  o  m
    outputFormat.setLineSeparator(StringPool.NEW_LINE);
    outputFormat.setTrimText(trimText);

    XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat);

    xmlWriter.write(node);

    String content = unsyncByteArrayOutputStream.toString(StringPool.UTF8);

    // LEP-4257

    //content = StringUtil.replace(content, "\n\n\n", "\n\n");

    if (content.endsWith("\n\n")) {
        content = content.substring(0, content.length() - 2);
    }

    if (content.endsWith("\n")) {
        content = content.substring(0, content.length() - 1);
    }

    while (content.contains(" \n")) {
        content = StringUtil.replace(content, " \n", "\n");
    }

    if (content.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
        content = StringUtil.replaceFirst(content, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
                "<?xml version=\"1.0\"?>");
    }

    return content;
}

From source file:com.zimbra.cs.dav.client.DavRequest.java

License:Open Source License

public String getRequestMessageString() throws IOException {
    if (mDoc != null) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setTrimText(false);
        format.setOmitEncoding(false);/*from w w  w .  j  a  va 2s.c o m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(mDoc);
        return new String(out.toByteArray(), "UTF-8");
    }
    return "";
}