Example usage for org.jdom2.output.support FormatStack getLineSeparator

List of usage examples for org.jdom2.output.support FormatStack getLineSeparator

Introduction

In this page you can find the example usage for org.jdom2.output.support FormatStack getLineSeparator.

Prototype

public String getLineSeparator() 

Source Link

Usage

From source file:com.googlesource.gerrit.plugins.manifest.CustomOutputter.java

License:Apache License

@Override
protected void printAttribute(java.io.Writer out, FormatStack fstack, Attribute attribute)
        throws java.io.IOException {
    // Do not print attributes that use default values
    for (DTDAttribute dtdAttribute : dtdAttributes) {
        if (attribute.getName().equals(dtdAttribute.getAttributeName())
                && attribute.getParent().getName().equals(dtdAttribute.getElementName())
                && attribute.getAttributeType().toString().equals(dtdAttribute.getType())
                && attribute.getValue().equals(dtdAttribute.getValue())) {
            return;
        }//from  w w w .  ja v  a  2 s  .  c  o m
    }

    out.append(fstack.getLineSeparator());
    String indent = fstack.getIndent();
    out.append(fstack.getLevelIndent());
    out.append(indent);
    // super.printAttribute() indents with an extra space, this will offset that
    out.append(indent.substring(0, indent.length() - 1));
    super.printAttribute(out, fstack, attribute);
}

From source file:io.wcm.maven.plugins.contentpackage.unpacker.OneAttributePerLineXmlProcessor.java

License:Apache License

@Override
protected void printAttribute(Writer out, FormatStack fstack, Attribute attribute) throws IOException {
    if (!attribute.isSpecified() && fstack.isSpecifiedAttributesOnly()) {
        return;/*from www.j av  a2  s . c  o  m*/
    }

    write(out, StringUtils.defaultString(fstack.getLineSeparator()));
    write(out, StringUtils.defaultString(fstack.getLevelIndent()));
    write(out, StringUtils.defaultString(fstack.getIndent()));
    write(out, StringUtils.defaultString(fstack.getIndent()));

    write(out, attribute.getQualifiedName());
    write(out, "=");

    write(out, "\"");
    attributeEscapedEntitiesFilter(out, fstack, attribute.getValue());
    write(out, "\"");
}

From source file:org.apache.maven.io.util.AbstractJDOMWriter.java

License:Apache License

public final void write(final T source, final Document document, final Writer writer, final Format jdomFormat,
        final DocumentModifier modifier) throws java.io.IOException {
    if (modifier != null) {
        modifier.preProcess(document);/*  w w  w .ja  v a  2 s. co  m*/
    }
    update(source, new IndentationCounter(0), document.getRootElement());
    if (modifier != null) {
        modifier.postProcess(document);
    }
    // Override XMLOutputter to correct initial comment trailing newlines.
    final XMLOutputter outputter = new XMLOutputter(new AbstractXMLOutputProcessor() {
        /**
         * This will handle printing of a {@link Document}.
         *
         * @param out    <code>Writer</code> to use.
         * @param fstack the FormatStack
         * @param nstack the NamespaceStack
         * @param doc    <code>Document</code> to write.
         * @throws IOException if the destination Writer fails
         */
        @Override
        protected void printDocument(Writer out, FormatStack fstack, NamespaceStack nstack, Document doc)
                throws IOException {

            // If there is no root element then we cannot use the normal ways to
            // access the ContentList because Document throws an exception.
            // so we hack it and just access it by index.
            List<Content> list = doc.hasRootElement() ? doc.getContent()
                    : new ArrayList<Content>(doc.getContentSize());
            if (list.isEmpty()) {
                final int sz = doc.getContentSize();
                for (int i = 0; i < sz; i++) {
                    list.add(doc.getContent(i));
                }
            }

            printDeclaration(out, fstack);

            Walker walker = buildWalker(fstack, list, true);
            if (walker.hasNext()) {
                while (walker.hasNext()) {

                    final Content c = walker.next();
                    // we do not ignore Text-like things in the Document.
                    // the walker creates the indenting for us.
                    if (c == null) {
                        // but, what we do is ensure it is all whitespace, and not CDATA
                        final String padding = walker.text();
                        if (padding != null && Verifier.isAllXMLWhitespace(padding) && !walker.isCDATA()) {
                            // we do not use the escaping or text* method because this
                            // content is outside of the root element, and thus is not
                            // strict text.
                            write(out, padding);
                        }
                    } else {
                        switch (c.getCType()) {
                        case Comment:
                            printComment(out, fstack, (Comment) c);
                            // This modification we have made to the overridden method in order
                            // to correct newline declarations.
                            write(out, fstack.getLineSeparator());
                            break;
                        case DocType:
                            printDocType(out, fstack, (DocType) c);
                            break;
                        case Element:
                            printElement(out, fstack, nstack, (Element) c);
                            if (walker.hasNext()) {
                                // This modification we have made to the overridden method in order
                                // to correct newline declarations.
                                write(out, fstack.getLineSeparator());
                            }
                            break;
                        case ProcessingInstruction:
                            printProcessingInstruction(out, fstack, (ProcessingInstruction) c);
                            break;
                        case Text:
                            final String padding = ((Text) c).getText();
                            if (padding != null && Verifier.isAllXMLWhitespace(padding)) {
                                // we do not use the escaping or text* method because this
                                // content is outside of the root element, and thus is not
                                // strict text.
                                write(out, padding);
                            }
                        default:
                            // do nothing.
                        }
                    }

                }

                if (fstack.getLineSeparator() != null) {
                    write(out, fstack.getLineSeparator());
                }
            }
        }
    });

    outputter.setFormat(jdomFormat);
    outputter.output(document, writer);
}