Example usage for org.jdom2.output Format getCompactFormat

List of usage examples for org.jdom2.output Format getCompactFormat

Introduction

In this page you can find the example usage for org.jdom2.output Format getCompactFormat.

Prototype

public static Format getCompactFormat() 

Source Link

Document

Returns a new Format object that performs whitespace normalization, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy.

Usage

From source file:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

public static String toShortString(Document pDoc) {
    XMLOutputter xmlOutput = new XMLOutputter();

    // display nice nice
    xmlOutput.setFormat(Format.getCompactFormat());
    return xmlOutput.outputString(pDoc);
}

From source file:lu.list.itis.dkd.aig.util.DocumentConverter.java

License:Apache License

/**
 * PATCH: as template content is not using CDATA try to remove trailing
 * white spaces and blank lines.//from  w  ww  .  j  av a  2 s .co m
 * 
 * @param document
 * @return well formated XML string
 */
private static String removeTrailingEmptyElements(final Document document) {
    XMLOutputter format = new XMLOutputter();
    format.setFormat(Format.getCompactFormat());
    return format.outputString(document);
}

From source file:net.FriendsUnited.Services.FileServer.java

License:Open Source License

private void sendDirectoryListing(FriendPacket pkt, String path) {
    log.info("Received Request for Listing of {}--{}", SharedFolder, path);
    File dir = new File(SharedFolder + path);
    File[] files = dir.listFiles();
    if (null == files) {
        replyWithFailurePacket(pkt, "Directory " + dir.getPath() + " can not be read");
    } else {/*from  w w w.  j  a  v a  2  s.  c  om*/
        Element root = new Element("FolderListing");
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            Element fe;
            if (true == f.isDirectory()) {
                fe = new Element("Directory");
                fe.setText(f.getName());
            } else {
                fe = new Element("File");
                fe.setText(f.getName());
                Element size = new Element("Size");
                size.addContent("" + f.length());
                fe.addContent(size);
            }
            Element lastModified = new Element("lastModified");
            long time = f.lastModified();
            lastModified.addContent("" + time);
            fe.addContent(lastModified);
            root.addContent(fe);
        }
        Document doc = new Document(root);
        XMLOutputter xout = new XMLOutputter(Format.getCompactFormat());
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            xout.output(doc, bout);
            ByteConverter bc = new ByteConverter();
            bc.add(DIRECTORY_LISTING);
            bout.flush();
            log.debug("Size of Listing XML : " + bout.size());
            bc.add(bout.size()); // length of String
            bc.add(bout.toByteArray()); // String Data
            FriendPacket replyPacket = new FriendPacket(pkt.getTargetServer(), pkt.getTargetService(), // source
                    pkt.getSourceServer(), pkt.getSourceService(), // target
                    bc.toByteArray()); // payload
            log.debug("sending : " + replyPacket);
            server.sendPacketIntoNetwork(this, replyPacket);
            log.debug("Send reply with Listing !");
        } catch (IOException e) {
            replyWithFailurePacket(pkt, Tool.fromExceptionToString(e));
        }
    }
}

From source file:org.apache.wiki.util.XhtmlUtil.java

License:Apache License

/**
 *  Serializes the Element to a String. If <tt>pretty</tt> is true,
 *  uses a pretty whitespace format, otherwise a compact format.
 *  //from  w  ww .  j a va  2  s  .c  o m
 * @param element  the element to serialize.
 * @param pretty   if true, use a pretty whitespace format.
 * @return the serialized Element.
 */
public static String serialize(Element element, boolean pretty) {
    return serialize(element, pretty ? Format.getPrettyFormat() : Format.getCompactFormat());
}

From source file:org.fnppl.opensdx.xml.Document.java

License:Open Source License

public void outputCompact(OutputStream out) {
    try {/*from w ww . j  a v a2s  . c o  m*/
        Format f = Format.getCompactFormat();
        f.setEncoding("UTF-8");
        XMLOutputter outp = new XMLOutputter(f);
        outp.output(base, out);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.fnppl.opensdx.xml.Document.java

License:Open Source License

public String toStringCompact() {
    try {/*  w ww. j  av a  2 s  .  c  om*/
        Format f = Format.getCompactFormat();
        f.setEncoding("UTF-8");
        XMLOutputter outp = new XMLOutputter(f);
        StringWriter sw = new StringWriter();
        outp.output(base, sw);
        sw.flush();

        return sw.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jboss.hal.processor.mbui.XmlHelper.java

License:Apache License

static String xmlAsString(org.jdom2.Element element) {
    String asString;/*from  ww w  .j  a v a 2 s . c  o m*/
    StringWriter writer = new StringWriter();
    try {
        new XMLOutputter(Format.getCompactFormat()).output(element, writer);
        asString = writer.toString();
    } catch (IOException e) {
        asString = "<" + element + "/>";
    }
    return asString;
}

From source file:org.jumpmind.metl.core.runtime.component.XmlFormatter.java

License:Open Source License

private void createXml(ISendMessageCallback callback) {

    Document generatedXml = new Document();
    Stack<DocElement> parentStack = new Stack<DocElement>();
    ArrayList<String> outboundPayload = new ArrayList<String>();

    for (Message msg : messagesToProcess) {
        processMsgEntities(parentStack, msg, generatedXml);
    }/*from   w w w.ja  v a2  s  . com*/
    XMLOutputter xmlOutputter = new XMLOutputter();
    Format format = null;
    if (xmlFormat.equals(COMPACT_FORMAT)) {
        format = Format.getCompactFormat();
    } else if (xmlFormat.equals(RAW_FORMAT)) {
        format = Format.getRawFormat();
    } else {
        format = Format.getPrettyFormat();
    }
    xmlOutputter.setFormat(format);
    outboundPayload.add(xmlOutputter.outputString(generatedXml));
    callback.sendTextMessage(null, outboundPayload);
}

From source file:org.jumpmind.metl.core.runtime.component.XsltProcessor.java

License:Open Source License

public static String getTransformedXml(String inputXml, String stylesheetXml, String xmlFormat,
        boolean omitXmlDeclaration) {
    StringWriter writer = new StringWriter();
    SAXBuilder builder = new SAXBuilder();
    builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    try {/* www .  j av  a 2s .com*/
        Document inputDoc = builder.build(new StringReader(inputXml));
        StringReader reader = new StringReader(stylesheetXml);
        XSLTransformer transformer = new XSLTransformer(reader);
        Document outputDoc = transformer.transform(inputDoc);
        XMLOutputter xmlOutput = new XMLOutputter();
        Format format = null;
        if (xmlFormat.equals(COMPACT_FORMAT)) {
            format = Format.getCompactFormat();
        } else if (xmlFormat.equals(RAW_FORMAT)) {
            format = Format.getRawFormat();
        } else {
            format = Format.getPrettyFormat();
        }

        format.setOmitDeclaration(omitXmlDeclaration);
        xmlOutput.setFormat(format);
        xmlOutput.output(outputDoc, writer);
        writer.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return writer.toString();
}

From source file:org.kdp.word.utils.IOUtils.java

License:Apache License

public static void writeDocument(Context context, Document doc, OutputStream out) throws IOException {
    Parser parser = context.getParser();
    String outputEncoding = parser.getProperty(Parser.PROPERTY_OUTPUT_ENCODING);
    outputEncoding = outputEncoding != null ? outputEncoding : "UTF-8";
    String outputFormat = parser.getProperty(Parser.PROPERTY_OUTPUT_FORMAT);
    boolean pretty = Parser.OUTPUT_FORMAT_PRETTY.equals(outputFormat);

    XMLOutputter xo = new XMLOutputter();
    Format format = pretty ? Format.getPrettyFormat() : Format.getCompactFormat();
    format.setEncoding(outputEncoding);/*from   w w w.  j a v  a  2  s.co m*/
    EscapeStrategy strategy = new OutputEscapeStrategy(context, format.getEscapeStrategy());
    format.setEscapeStrategy(strategy);
    xo.setFormat(format.setOmitDeclaration(true));
    xo.output(doc, out);
}