jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
Subject svn commit: r1512568 [25/39] - in /jackrabbit/commons/filevault/trunk: ./ parent/ vault-cli/ vault-cli/src/ vault-cli/src/main/ vault-cli/src/main/appassembler/ vault-cli/src/main/assembly/ vault-cli/src/main/java/ vault-cli/src/main/java/org/ vault-cl...
Date Sat, 10 Aug 2013 05:53:54 GMT
Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/IndentPrinter.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/IndentPrinter.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/IndentPrinter.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/IndentPrinter.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,345 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+
+/**
+ * Extends {@link Printer} and adds support for indentation and line
+ * wrapping.
+ *
+ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @version $Revision$ $Date$
+ */
+public class IndentPrinter
+        extends Printer {
+
+
+    /**
+     * Holds the currently accumulating text line. This buffer will constantly
+     * be reused by deleting its contents instead of reallocating it.
+     */
+    private StringBuffer _line;
+
+
+    /**
+     * Holds the currently accumulating text that follows {@link #_line}.
+     * When the end of the part is identified by a call to {@link #printSpace}
+     * or {@link #breakLine}, this part is added to the accumulated line.
+     */
+    private StringBuffer _text;
+
+
+    /**
+     * Counts how many white spaces come between the accumulated line and the
+     * current accumulated text. Multiple spaces at the end of the a line
+     * will not be printed.
+     */
+    private int _spaces;
+
+
+    /**
+     * Holds the indentation for the current line that is now accumulating in
+     * memory and will be sent for printing shortly.
+     */
+    private int _thisIndent;
+
+
+    /**
+     * Holds the indentation for the next line to be printed. After this line is
+     * printed, {@link #_nextIndent} is assigned to {@link #_thisIndent}.
+     */
+    private int _nextIndent;
+
+
+    public IndentPrinter(Writer writer, OutputFormat format) {
+        super(writer, format);
+        // Initialize everything for a first/second run.
+        _line = new StringBuffer(80);
+        _text = new StringBuffer(20);
+        _spaces = 0;
+        _thisIndent = _nextIndent = 0;
+    }
+
+
+    /**
+     * Called by any of the DTD handlers to enter DTD mode.
+     * Once entered, all output will be accumulated in a string
+     * that can be printed as part of the document's DTD.
+     * This method may be called any number of time but will only
+     * have affect the first time it's called. To exist DTD state
+     * and get the accumulated DTD, call {@link #leaveDTD}.
+     */
+    public void enterDTD() {
+        // Can only enter DTD state once. Once we're out of DTD
+        // state, can no longer re-enter it.
+        if (_dtdWriter == null) {
+            _line.append(_text);
+            _text = new StringBuffer(20);
+            flushLine(false);
+            _dtdWriter = new StringWriter();
+            _docWriter = _writer;
+            _writer = _dtdWriter;
+        }
+    }
+
+
+    /**
+     * Called by the root element to leave DTD mode and if any
+     * DTD parts were printer, will return a string with their
+     * textual content.
+     */
+    public String leaveDTD() {
+        // Only works if we're going out of DTD mode.
+        if (_writer == _dtdWriter) {
+            _line.append(_text);
+            _text = new StringBuffer(20);
+            flushLine(false);
+            _writer = _docWriter;
+            return _dtdWriter.toString();
+        } else
+            return null;
+    }
+
+
+    /**
+     * Called to print additional text. Each time this method is called
+     * it accumulates more text. When a space is printed ({@link
+     * #printSpace}) all the accumulated text becomes one part and is
+     * added to the accumulate line. When a line is long enough, it can
+     * be broken at its text boundary.
+     *
+     * @param text The text to print
+     */
+    public void printText(String text) {
+        _text.append(text);
+    }
+
+
+    public void printText(StringBuffer text) {
+        _text.append(text.toString());
+    }
+
+
+    public void printText(char ch) {
+        _text.append(ch);
+    }
+
+
+    public void printText(char[] chars, int start, int length) {
+        _text.append(chars, start, length);
+    }
+
+
+    /**
+     * Called to print a single space between text parts that may be
+     * broken into separate lines. Must not be called to print a space
+     * when preserving spaces. The text accumulated so far with {@link
+     * #printText} will be added to the accumulated line, and a space
+     * separator will be counted. If the line accumulated so far is
+     * long enough, it will be printed.
+     */
+    public void printSpace() {
+        // The line consists of the text accumulated in _line,
+        // followed by one or more spaces as counted by _spaces,
+        // followed by more space accumulated in _text:
+        // -  Text is printed and accumulated into _text.
+        // -  A space is printed, so _text is added to _line and
+        //    a space is counted.
+        // -  More text is printed and accumulated into _text.
+        // -  A space is printed, the previous spaces are added
+        //    to _line, the _text is added to _line, and a new
+        //    space is counted.
+
+        // If text was accumulated with printText(), then the space
+        // means we have to move that text into the line and
+        // start accumulating new text with printText().
+        if (_text.length() > 0) {
+            // If the text breaks a line bounary, wrap to the next line.
+            // The printed line size consists of the indentation we're going
+            // to use next, the accumulated line so far, some spaces and the
+            // accumulated text so far.
+            if (_format.getLineWidth() > 0 &&
+                    _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth()) {
+                flushLine(false);
+                try {
+                    // Print line and new line, then zero the line contents.
+                    _writer.write(_format.getLineSeparator());
+                } catch (IOException except) {
+                    // We don't throw an exception, but hold it
+                    // until the end of the document.
+                    if (_exception == null)
+                        _exception = except;
+                }
+            }
+
+            // Add as many spaces as we accumulaed before.
+            // At the end of this loop, _spaces is zero.
+            while (_spaces > 0) {
+                _line.append(' ');
+                --_spaces;
+            }
+            _line.append(_text);
+            _text = new StringBuffer(20);
+        }
+        // Starting a new word: accumulate the text between the line
+        // and this new word; not a new word: just add another space.
+        ++_spaces;
+    }
+
+
+    /**
+     * Called to print a line consisting of the text accumulated so
+     * far. This is equivalent to calling {@link #printSpace} but
+     * forcing the line to print and starting a new line ({@link
+     * #printSpace} will only start a new line if the current line
+     * is long enough).
+     */
+    public void breakLine() {
+        breakLine(false);
+    }
+
+
+    public void breakLine(boolean preserveSpace) {
+        // Equivalent to calling printSpace and forcing a flushLine.
+        if (_text.length() > 0) {
+            while (_spaces > 0) {
+                _line.append(' ');
+                --_spaces;
+            }
+            _line.append(_text);
+            _text = new StringBuffer(20);
+        }
+        flushLine(preserveSpace);
+        try {
+            // Print line and new line, then zero the line contents.
+            _writer.write(_format.getLineSeparator());
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+        }
+    }
+
+
+    /**
+     * Flushes the line accumulated so far to the writer and get ready
+     * to accumulate the next line. This method is called by {@link
+     * #printText} and {@link #printSpace} when the accumulated line plus
+     * accumulated text are two long to fit on a given line. At the end of
+     * this method _line is empty and _spaces is zero.
+     */
+    public void flushLine(boolean preserveSpace) {
+        int indent;
+
+        if (_line.length() > 0) {
+            try {
+
+                if (_format.getIndenting() && !preserveSpace) {
+                    // Make sure the indentation does not blow us away.
+                    indent = _thisIndent;
+                    if ((2 * indent) > _format.getLineWidth() && _format.getLineWidth() > 0)
+                        indent = _format.getLineWidth() / 2;
+                    // Print the indentation as spaces and set the current
+                    // indentation to the next expected indentation.
+                    while (indent > 0) {
+                        _writer.write(' ');
+                        --indent;
+                    }
+                }
+                _thisIndent = _nextIndent;
+
+                // There is no need to print the spaces at the end of the line,
+                // they are simply stripped and replaced with a single line
+                // separator.
+                _spaces = 0;
+                _writer.write(_line.toString());
+
+                _line = new StringBuffer(40);
+            } catch (IOException except) {
+                // We don't throw an exception, but hold it
+                // until the end of the document.
+                if (_exception == null)
+                    _exception = except;
+            }
+        }
+    }
+
+
+    /**
+     * Flush the output stream. Must be called when done printing
+     * the document, otherwise some text might be buffered.
+     */
+    public void flush() {
+        if (_line.length() > 0 || _text.length() > 0)
+            breakLine();
+        try {
+            _writer.flush();
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+        }
+    }
+
+
+    /**
+     * Increment the indentation for the next line.
+     */
+    public void indent() {
+        _nextIndent += _format.getIndent();
+    }
+
+
+    /**
+     * Decrement the indentation for the next line.
+     */
+    public void unindent() {
+        _nextIndent -= _format.getIndent();
+        if (_nextIndent < 0)
+            _nextIndent = 0;
+        // If there is no current line and we're de-identing then
+        // this indentation level is actually the next level.
+        if ((_line.length() + _spaces + _text.length()) == 0)
+            _thisIndent = _nextIndent;
+    }
+
+
+    public int getNextIndent() {
+        return _nextIndent;
+    }
+
+
+    public void setNextIndent(int indent) {
+        _nextIndent = indent;
+    }
+
+
+    public void setThisIndent(int indent) {
+        _thisIndent = indent;
+    }
+
+
+}

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/LineSeparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/LineSeparator.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/LineSeparator.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/LineSeparator.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+/**
+ * @author <a href="mailto:arkin@intalio..com">Assaf Arkin</a>
+ * @version $Revision$ $Date$
+ * @see OutputFormat
+ */
+public final class LineSeparator {
+
+
+    /**
+     * Line separator for Unix systems (<tt>\n</tt>).
+     */
+    public static final String Unix = "\n";
+
+
+    /**
+     * Line separator for Windows systems (<tt>\r\n</tt>).
+     */
+    public static final String Windows = "\r\n";
+
+
+    /**
+     * Line separator for Macintosh systems (<tt>\r</tt>).
+     */
+    public static final String Macintosh = "\r";
+
+
+    /**
+     * Line separator for the Web (<tt>\n</tt>).
+     */
+    public static final String Web = "\n";
+
+
+}
+
+

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Method.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Method.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Method.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Method.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+/**
+ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @version $Revision$ $Date$
+ * @see OutputFormat
+ */
+public final class Method {
+
+
+    /**
+     * The output method for XML documents.
+     */
+    public static final String XML = "xml";
+
+
+    /**
+     * The output method for HTML documents.
+     */
+    public static final String HTML = "html";
+
+
+    /**
+     * The output method for HTML documents as XHTML.
+     */
+    public static final String XHTML = "xhtml";
+
+
+    /**
+     * The output method for text documents.
+     */
+    public static final String TEXT = "text";
+
+
+    /**
+     * The output method for FO documents as PDF.
+     */
+    public static final String FOP = "fop";
+
+
+}
+
+

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/OutputFormat.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/OutputFormat.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/OutputFormat.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/OutputFormat.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,938 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+// Aug 21, 2000:
+//  Added ability to omit DOCTYPE declaration.
+//  Reported by Lars Martin <lars@smb-tec.com>
+// Aug 25, 2000:
+//  Added ability to omit comments.
+//  Contributed by Anupam Bagchi <abagchi@jtcsv.com>
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+import java.io.UnsupportedEncodingException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Node;
+import org.w3c.dom.html.HTMLDocument;
+
+
+/**
+ * Specifies an output format to control the serializer. Based on the
+ * XSLT specification for output format, plus additional parameters.
+ * Used to select the suitable serializer and determine how the
+ * document should be formatted on output.
+ * <p/>
+ * The two interesting constructors are:
+ * <ul>
+ * <li>{@link #OutputFormat(String,String,boolean)} creates a format
+ * for the specified method (XML, HTML, Text, etc), encoding and indentation
+ * <li>{@link #OutputFormat(Document,String,boolean)} creates a format
+ * compatible with the document type (XML, HTML, Text, etc), encoding and
+ * indentation
+ * </ul>
+ *
+ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ *         <a href="mailto:visco@intalio.com">Keith Visco</a>
+ * @version $Revision$ $Date$
+ * @see Serializer
+ * @see Method
+ * @see LineSeparator
+ */
+public class OutputFormat {
+
+
+    public static class DTD {
+
+        /**
+         * Public identifier for HTML 4.01 (Strict) document type.
+         */
+        public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN";
+
+        /**
+         * System identifier for HTML 4.01 (Strict) document type.
+         */
+        public static final String HTMLSystemId =
+                "http://www.w3.org/TR/html4/strict.dtd";
+
+        /**
+         * Public identifier for XHTML 1.0 (Strict) document type.
+         */
+        public static final String XHTMLPublicId =
+                "-//W3C//DTD XHTML 1.0 Strict//EN";
+
+        /**
+         * System identifier for XHTML 1.0 (Strict) document type.
+         */
+        public static final String XHTMLSystemId =
+                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
+
+    }
+
+
+    public static class Defaults {
+
+        /**
+         * If indentation is turned on, the default identation
+         * level is 4.
+         *
+         * @see OutputFormat#setIndenting(boolean)
+         */
+        public static final int Indent = 4;
+
+        /**
+         * The default encoding for Web documents it UTF-8.
+         *
+         * @see OutputFormat#getEncoding()
+         */
+        public static final String Encoding = "UTF-8";
+
+        /**
+         * The default line width at which to break long lines
+         * when identing. This is set to 72.
+         */
+        public static final int LineWidth = 72;
+
+    }
+
+
+    /**
+     * Holds the output method specified for this document,
+     * or null if no method was specified.
+     */
+    private String _method;
+
+
+    /**
+     * Specifies the version of the output method.
+     */
+    private String _version;
+
+
+    /**
+     * The indentation level, or zero if no indentation
+     * was requested.
+     */
+    private int _indent = 0;
+
+    /**
+     * if <code>true</code> each attribute is placed on a new line
+     * (added by tripod@apache.org)
+     */
+    private boolean _breakEachAttribute = false;
+
+    /**
+     * The encoding to use, if an input stream is used.
+     * The default is always UTF-8.
+     */
+    private String _encoding = Defaults.Encoding;
+
+    /**
+     * The EncodingInfo instance for _encoding.
+     */
+    private EncodingInfo _encodingInfo = null;
+
+    // whether java names for encodings are permitted
+    private boolean _allowJavaNames = false;
+
+    /**
+     * The specified media type or null.
+     */
+    private String _mediaType;
+
+
+    /**
+     * The specified document type system identifier, or null.
+     */
+    private String _doctypeSystem;
+
+
+    /**
+     * The specified document type public identifier, or null.
+     */
+    private String _doctypePublic;
+
+
+    /**
+     * Ture if the XML declaration should be ommited;
+     */
+    private boolean _omitXmlDeclaration = false;
+
+
+    /**
+     * Ture if the DOCTYPE declaration should be ommited;
+     */
+    private boolean _omitDoctype = false;
+
+
+    /**
+     * Ture if comments should be ommited;
+     */
+    private boolean _omitComments = false;
+
+
+    /**
+     * Ture if the comments should be ommited;
+     */
+    private boolean _stripComments = false;
+
+
+    /**
+     * True if the document type should be marked as standalone.
+     */
+    private boolean _standalone = false;
+
+
+    /**
+     * List of element tag names whose text node children must
+     * be output as CDATA.
+     */
+    private String[] _cdataElements;
+
+
+    /**
+     * List of element tag names whose text node children must
+     * be output unescaped.
+     */
+    private String[] _nonEscapingElements;
+
+
+    /**
+     * The selected line separator.
+     */
+    private String _lineSeparator = LineSeparator.Web;
+
+
+    /**
+     * The line width at which to wrap long lines when indenting.
+     */
+    private int _lineWidth = Defaults.LineWidth;
+
+
+    /**
+     * True if spaces should be preserved in elements that do not
+     * specify otherwise, or specify the default behavior.
+     */
+    private boolean _preserve = false;
+    /**
+     * If true, an empty string valued attribute is output as "". If false and
+     * and we are using the HTMLSerializer, then only the attribute name is
+     * serialized. Defaults to false for backwards compatibility.
+     */
+    private boolean _preserveEmptyAttributes = false;
+
+    /**
+     * Constructs a new output format with the default values.
+     */
+    public OutputFormat() {
+    }
+
+
+    /**
+     * Constructs a new output format with the default values for
+     * the specified method and encoding. If <tt>indent</tt>
+     * is true, the document will be pretty printed with the default
+     * indentation level and default line wrapping.
+     *
+     * @param method    The specified output method
+     * @param encoding  The specified encoding
+     * @param indenting True for pretty printing
+     * @see #setEncoding
+     * @see #setIndenting
+     * @see #setMethod
+     */
+    public OutputFormat(String method, String encoding, boolean indenting) {
+        setMethod(method);
+        setEncoding(encoding);
+        setIndenting(indenting);
+    }
+
+
+    /**
+     * Constructs a new output format with the proper method,
+     * document type identifiers and media type for the specified
+     * document.
+     *
+     * @param doc The document to output
+     * @see #whichMethod
+     */
+    public OutputFormat(Document doc) {
+        setMethod(whichMethod(doc));
+        setDoctype(whichDoctypePublic(doc), whichDoctypeSystem(doc));
+        setMediaType(whichMediaType(getMethod()));
+    }
+
+
+    /**
+     * Constructs a new output format with the proper method,
+     * document type identifiers and media type for the specified
+     * document, and with the specified encoding. If <tt>indent</tt>
+     * is true, the document will be pretty printed with the default
+     * indentation level and default line wrapping.
+     *
+     * @param doc       The document to output
+     * @param encoding  The specified encoding
+     * @param indenting True for pretty printing
+     * @see #setEncoding
+     * @see #setIndenting
+     * @see #whichMethod
+     */
+    public OutputFormat(Document doc, String encoding, boolean indenting) {
+        this(doc);
+        setEncoding(encoding);
+        setIndenting(indenting);
+    }
+
+
+    /**
+     * Returns the method specified for this output format.
+     * Typically the method will be <tt>xml</tt>, <tt>html</tt>
+     * or <tt>text</tt>, but it might be other values.
+     * If no method was specified, null will be returned
+     * and the most suitable method will be determined for
+     * the document by calling {@link #whichMethod}.
+     *
+     * @return The specified output method, or null
+     */
+    public String getMethod() {
+        return _method;
+    }
+
+
+    /**
+     * Sets the method for this output format.
+     *
+     * @param method The output method, or null
+     * @see #getMethod
+     */
+    public void setMethod(String method) {
+        _method = method;
+    }
+
+
+    /**
+     * Returns the version for this output method.
+     * If no version was specified, will return null
+     * and the default version number will be used.
+     * If the serializerr does not support that particular
+     * version, it should default to a supported version.
+     *
+     * @return The specified method version, or null
+     */
+    public String getVersion() {
+        return _version;
+    }
+
+
+    /**
+     * Sets the version for this output method.
+     * For XML the value would be "1.0", for HTML
+     * it would be "4.0".
+     *
+     * @param version The output method version, or null
+     * @see #getVersion
+     */
+    public void setVersion(String version) {
+        _version = version;
+    }
+
+
+    /**
+     * Returns the indentation specified. If no indentation
+     * was specified, zero is returned and the document
+     * should not be indented.
+     *
+     * @return The indentation or zero
+     * @see #setIndenting
+     */
+    public int getIndent() {
+        return _indent;
+    }
+
+
+    /**
+     * Returns true if indentation was specified.
+     */
+    public boolean getIndenting() {
+        return (_indent > 0);
+    }
+
+
+    /**
+     * Sets the indentation. The document will not be
+     * indented if the indentation is set to zero.
+     * Calling {@link #setIndenting} will reset this
+     * value to zero (off) or the default (on).
+     *
+     * @param indent The indentation, or zero
+     */
+    public void setIndent(int indent) {
+        if (indent < 0)
+            _indent = 0;
+        else
+            _indent = indent;
+    }
+
+
+    /**
+     * Sets the indentation on and off. When set on, the default
+     * indentation level and default line wrapping is used
+     * (see {@link Defaults#Indent} and {@link Defaults#LineWidth}).
+     * To specify a different indentation level or line wrapping,
+     * use {@link #setIndent} and {@link #setLineWidth}.
+     *
+     * @param on True if indentation should be on
+     */
+    public void setIndenting(boolean on) {
+        if (on) {
+            _indent = Defaults.Indent;
+            _lineWidth = Defaults.LineWidth;
+        } else {
+            _indent = 0;
+            _lineWidth = 0;
+        }
+    }
+
+
+    /**
+     * Returns the specified encoding. If no encoding was
+     * specified, the default is always "UTF-8".
+     *
+     * @return The encoding
+     */
+    public String getEncoding() {
+        return _encoding;
+    }
+
+
+    /**
+     * Sets the encoding for this output method. If no
+     * encoding was specified, the default is always "UTF-8".
+     * Make sure the encoding is compatible with the one
+     * used by the {@link java.io.Writer}.
+     *
+     * @param encoding The encoding, or null
+     * @see #getEncoding
+     */
+    public void setEncoding(String encoding) {
+        _encoding = encoding;
+        _encodingInfo = null;
+    }
+
+    /**
+     * Sets the encoding for this output method with an <code>EncodingInfo</code>
+     * instance.
+     */
+    public void setEncoding(EncodingInfo encInfo) {
+        _encoding = encInfo.getIANAName();
+        _encodingInfo = encInfo;
+    }
+
+    /**
+     * Returns an <code>EncodingInfo<code> instance for the encoding.
+     *
+     * @see #setEncoding
+     */
+    public EncodingInfo getEncodingInfo() throws UnsupportedEncodingException {
+        if (_encodingInfo == null)
+            _encodingInfo = Encodings.getEncodingInfo(_encoding, _allowJavaNames);
+        return _encodingInfo;
+    }
+
+    /**
+     * Sets whether java encoding names are permitted
+     */
+    public void setAllowJavaNames(boolean allow) {
+        _allowJavaNames = allow;
+    }
+
+    /**
+     * Returns whether java encoding names are permitted
+     */
+    public boolean setAllowJavaNames() {
+        return _allowJavaNames;
+    }
+
+    /**
+     * Returns the specified media type, or null.
+     * To determine the media type based on the
+     * document type, use {@link #whichMediaType}.
+     *
+     * @return The specified media type, or null
+     */
+    public String getMediaType() {
+        return _mediaType;
+    }
+
+
+    /**
+     * Sets the media type.
+     *
+     * @param mediaType The specified media type
+     * @see #getMediaType
+     */
+    public void setMediaType(String mediaType) {
+        _mediaType = mediaType;
+    }
+
+
+    /**
+     * Sets the document type public and system identifiers.
+     * Required only if the DOM Document or SAX events do not
+     * specify the document type, and one must be present in
+     * the serialized document. Any document type specified
+     * by the DOM Document or SAX events will override these
+     * values.
+     *
+     * @param publicId The public identifier, or null
+     * @param systemId The system identifier, or null
+     */
+    public void setDoctype(String publicId, String systemId) {
+        _doctypePublic = publicId;
+        _doctypeSystem = systemId;
+    }
+
+
+    /**
+     * Returns the specified document type public identifier,
+     * or null.
+     */
+    public String getDoctypePublic() {
+        return _doctypePublic;
+    }
+
+
+    /**
+     * Returns the specified document type system identifier,
+     * or null.
+     */
+    public String getDoctypeSystem() {
+        return _doctypeSystem;
+    }
+
+
+    /**
+     * Returns true if comments should be ommited.
+     * The default is false.
+     */
+    public boolean getOmitComments() {
+        return _omitComments;
+    }
+
+
+    /**
+     * Sets comment omitting on and off.
+     *
+     * @param omit True if comments should be ommited
+     */
+    public void setOmitComments(boolean omit) {
+        _omitComments = omit;
+    }
+
+
+    /**
+     * Returns true if the DOCTYPE declaration should
+     * be ommited. The default is false.
+     */
+    public boolean getOmitDocumentType() {
+        return _omitDoctype;
+    }
+
+
+    /**
+     * Sets DOCTYPE declaration omitting on and off.
+     *
+     * @param omit True if DOCTYPE declaration should be ommited
+     */
+    public void setOmitDocumentType(boolean omit) {
+        _omitDoctype = omit;
+    }
+
+
+    /**
+     * Returns true if the XML document declaration should
+     * be ommited. The default is false.
+     */
+    public boolean getOmitXMLDeclaration() {
+        return _omitXmlDeclaration;
+    }
+
+
+    /**
+     * Sets XML declaration omitting on and off.
+     *
+     * @param omit True if XML declaration should be ommited
+     */
+    public void setOmitXMLDeclaration(boolean omit) {
+        _omitXmlDeclaration = omit;
+    }
+
+
+    /**
+     * Returns true if the document type is standalone.
+     * The default is false.
+     */
+    public boolean getStandalone() {
+        return _standalone;
+    }
+
+
+    /**
+     * Sets document DTD standalone. The public and system
+     * identifiers must be null for the document to be
+     * serialized as standalone.
+     *
+     * @param standalone True if document DTD is standalone
+     */
+    public void setStandalone(boolean standalone) {
+        _standalone = standalone;
+    }
+
+
+    /**
+     * Returns a list of all the elements whose text node children
+     * should be output as CDATA, or null if no such elements were
+     * specified.
+     */
+    public String[] getCDataElements() {
+        return _cdataElements;
+    }
+
+
+    /**
+     * Returns true if the text node children of the given elements
+     * should be output as CDATA.
+     *
+     * @param tagName The element's tag name
+     * @return True if should serialize as CDATA
+     */
+    public boolean isCDataElement(String tagName) {
+        int i;
+
+        if (_cdataElements == null)
+            return false;
+        for (i = 0; i < _cdataElements.length; ++i)
+            if (_cdataElements[i].equals(tagName))
+                return true;
+        return false;
+    }
+
+
+    /**
+     * Sets the list of elements for which text node children
+     * should be output as CDATA.
+     *
+     * @param cdataElements List of CDATA element tag names
+     */
+    public void setCDataElements(String[] cdataElements) {
+        _cdataElements = cdataElements;
+    }
+
+
+    /**
+     * Returns a list of all the elements whose text node children
+     * should be output unescaped (no character references), or null
+     * if no such elements were specified.
+     */
+    public String[] getNonEscapingElements() {
+        return _nonEscapingElements;
+    }
+
+
+    /**
+     * Returns true if the text node children of the given elements
+     * should be output unescaped.
+     *
+     * @param tagName The element's tag name
+     * @return True if should serialize unescaped
+     */
+    public boolean isNonEscapingElement(String tagName) {
+        int i;
+
+        if (_nonEscapingElements == null) {
+            return false;
+        }
+        for (i = 0; i < _nonEscapingElements.length; ++i)
+            if (_nonEscapingElements[i].equals(tagName))
+                return true;
+        return false;
+    }
+
+
+    /**
+     * Sets the list of elements for which text node children
+     * should be output unescaped (no character references).
+     *
+     * @param nonEscapingElements List of unescaped element tag names
+     */
+    public void setNonEscapingElements(String[] nonEscapingElements) {
+        _nonEscapingElements = nonEscapingElements;
+    }
+
+
+    /**
+     * Returns a specific line separator to use. The default is the
+     * Web line separator (<tt>\n</tt>). A string is returned to
+     * support double codes (CR + LF).
+     *
+     * @return The specified line separator
+     */
+    public String getLineSeparator() {
+        return _lineSeparator;
+    }
+
+
+    /**
+     * Sets the line separator. The default is the Web line separator
+     * (<tt>\n</tt>). The machine's line separator can be obtained
+     * from the system property <tt>line.separator</tt>, but is only
+     * useful if the document is edited on machines of the same type.
+     * For general documents, use the Web line separator.
+     *
+     * @param lineSeparator The specified line separator
+     */
+    public void setLineSeparator(String lineSeparator) {
+        if (lineSeparator == null)
+            _lineSeparator = LineSeparator.Web;
+        else
+            _lineSeparator = lineSeparator;
+    }
+
+
+    /**
+     * Returns true if the default behavior for this format is to
+     * preserve spaces. All elements that do not specify otherwise
+     * or specify the default behavior will be formatted based on
+     * this rule. All elements that specify space preserving will
+     * always preserve space.
+     */
+    public boolean getPreserveSpace() {
+        return _preserve;
+    }
+
+
+    /**
+     * Sets space preserving as the default behavior. The default is
+     * space stripping and all elements that do not specify otherwise
+     * or use the default value will not preserve spaces.
+     *
+     * @param preserve True if spaces should be preserved
+     */
+    public void setPreserveSpace(boolean preserve) {
+        _preserve = preserve;
+    }
+
+
+    /**
+     * Return the selected line width for breaking up long lines.
+     * When indenting, and only when indenting, long lines will be
+     * broken at space boundaries based on this line width.
+     * No line wrapping occurs if this value is zero.
+     */
+    public int getLineWidth() {
+        return _lineWidth;
+    }
+
+
+    /**
+     * Sets the line width. If zero then no line wrapping will
+     * occur. Calling {@link #setIndenting} will reset this
+     * value to zero (off) or the default (on).
+     *
+     * @param lineWidth The line width to use, zero for default
+     * @see #getLineWidth
+     * @see #setIndenting
+     */
+    public void setLineWidth(int lineWidth) {
+        if (lineWidth <= 0)
+            _lineWidth = 0;
+        else
+            _lineWidth = lineWidth;
+    }
+
+    public boolean getBreakEachAttribute() {
+        return _breakEachAttribute;
+    }
+
+    public void setBreakEachAttribute(boolean on) {
+        this._breakEachAttribute = on;
+    }
+
+    /**
+     * Returns the preserveEmptyAttribute flag. If flag is false, then'
+     * attributes with empty string values are output as the attribute
+     * name only (in HTML mode).
+     *
+     * @return preserve the preserve flag
+     */
+    public boolean getPreserveEmptyAttributes() {
+        return _preserveEmptyAttributes;
+    }
+
+    /**
+     * Sets the preserveEmptyAttribute flag. If flag is false, then'
+     * attributes with empty string values are output as the attribute
+     * name only (in HTML mode).
+     *
+     * @param preserve the preserve flag
+     */
+    public void setPreserveEmptyAttributes(boolean preserve) {
+        _preserveEmptyAttributes = preserve;
+    }
+
+    /**
+     * Returns the last printable character based on the selected
+     * encoding. Control characters and non-printable characters
+     * are always printed as character references.
+     */
+    public char getLastPrintable() {
+        if (getEncoding() != null &&
+                (getEncoding().equalsIgnoreCase("ASCII")))
+            return 0xFF;
+        else
+            return 0xFFFF;
+    }
+
+
+    /**
+     * Determine the output method for the specified document.
+     * If the document is an instance of {@link org.w3c.dom.html.HTMLDocument}
+     * then the method is said to be <tt>html</tt>. If the root
+     * element is 'html' and all text nodes preceding the root
+     * element are all whitespace, then the method is said to be
+     * <tt>html</tt>. Otherwise the method is <tt>xml</tt>.
+     *
+     * @param doc The document to check
+     * @return The suitable method
+     */
+    public static String whichMethod(Document doc) {
+        Node node;
+        String value;
+        int i;
+
+        // If document is derived from HTMLDocument then the default
+        // method is html.
+        if (doc instanceof HTMLDocument)
+            return Method.HTML;
+
+        // Lookup the root element and the text nodes preceding it.
+        // If root element is html and all text nodes contain whitespace
+        // only, the method is html.
+
+        // FIXME (SM) should we care about namespaces here?
+
+        node = doc.getFirstChild();
+        while (node != null) {
+            // If the root element is html, the method is html.
+            if (node.getNodeType() == Node.ELEMENT_NODE) {
+                if (node.getNodeName().equalsIgnoreCase("html")) {
+                    return Method.HTML;
+                } else if (node.getNodeName().equalsIgnoreCase("root")) {
+                    return Method.FOP;
+                } else {
+                    return Method.XML;
+                }
+            } else if (node.getNodeType() == Node.TEXT_NODE) {
+                // If a text node preceding the root element contains
+                // only whitespace, this might be html, otherwise it's
+                // definitely xml.
+                value = node.getNodeValue();
+                for (i = 0; i < value.length(); ++i)
+                    if (value.charAt(i) != 0x20 && value.charAt(i) != 0x0A &&
+                            value.charAt(i) != 0x09 && value.charAt(i) != 0x0D)
+                        return Method.XML;
+            }
+            node = node.getNextSibling();
+        }
+        // Anything else, the method is xml.
+        return Method.XML;
+    }
+
+
+    /**
+     * Returns the document type public identifier
+     * specified for this document, or null.
+     */
+    public static String whichDoctypePublic(Document doc) {
+        DocumentType doctype;
+
+        /*  DOM Level 2 was introduced into the code base*/
+        doctype = doc.getDoctype();
+        if (doctype != null) {
+            // Note on catch: DOM Level 1 does not specify this method
+            // and the code will throw a NoSuchMethodError
+            try {
+                return doctype.getPublicId();
+            } catch (Error except) {
+            }
+        }
+
+        if (doc instanceof HTMLDocument)
+            return DTD.XHTMLPublicId;
+        return null;
+    }
+
+
+    /**
+     * Returns the document type system identifier
+     * specified for this document, or null.
+     */
+    public static String whichDoctypeSystem(Document doc) {
+        DocumentType doctype;
+
+        /* DOM Level 2 was introduced into the code base*/
+        doctype = doc.getDoctype();
+        if (doctype != null) {
+            // Note on catch: DOM Level 1 does not specify this method
+            // and the code will throw a NoSuchMethodError
+            try {
+                return doctype.getSystemId();
+            } catch (Error except) {
+            }
+        }
+
+        if (doc instanceof HTMLDocument)
+            return DTD.XHTMLSystemId;
+        return null;
+    }
+
+
+    /**
+     * Returns the suitable media format for a document
+     * output with the specified method.
+     */
+    public static String whichMediaType(String method) {
+        if (method.equalsIgnoreCase(Method.XML))
+            return "text/xml";
+        if (method.equalsIgnoreCase(Method.HTML))
+            return "text/html";
+        if (method.equalsIgnoreCase(Method.XHTML))
+            return "text/html";
+        if (method.equalsIgnoreCase(Method.TEXT))
+            return "text/plain";
+        if (method.equalsIgnoreCase(Method.FOP))
+            return "application/pdf";
+        return null;
+    }
+
+
+}
+

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Printer.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Printer.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Printer.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Printer.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,348 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+// Sep 14, 2000:
+//  Fixed serializer to report IO exception directly, instead at
+//  the end of document processing.
+//  Reported by Patrick Higgins <phiggins@transzap.com>
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+
+/**
+ * The printer is responsible for sending text to the output stream
+ * or writer. This class performs direct writing for efficiency.
+ * {@link IndentPrinter} supports indentation and line wrapping by
+ * extending this class.
+ *
+ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @version $Revision$ $Date$
+ */
+public class Printer {
+
+
+    /**
+     * The output format associated with this serializer. This will never
+     * be a null reference. If no format was passed to the constructor,
+     * the default one for this document type will be used. The format
+     * object is never changed by the serializer.
+     */
+    protected final OutputFormat _format;
+
+
+    /**
+     * The writer to which the document is written.
+     */
+    protected Writer _writer;
+
+
+    /**
+     * The DTD writer. When we switch to DTD mode, all output is
+     * accumulated in this DTD writer. When we switch out of it,
+     * the output is obtained as a string. Must not be reset to
+     * null until we're done with the document.
+     */
+    protected StringWriter _dtdWriter;
+
+
+    /**
+     * Holds a reference to the document writer while we are
+     * in DTD mode.
+     */
+    protected Writer _docWriter;
+
+
+    /**
+     * Holds the exception thrown by the serializer.  Exceptions do not cause
+     * the serializer to quit, but are held and one is thrown at the end.
+     */
+    protected IOException _exception;
+
+
+    /**
+     * The size of the output buffer.
+     */
+    private static final int BufferSize = 4096;
+
+
+    /**
+     * Output buffer.
+     */
+    private final char[] _buffer = new char[BufferSize];
+
+
+    /**
+     * Position within the output buffer.
+     */
+    private int _pos = 0;
+
+
+    public Printer(Writer writer, OutputFormat format) {
+        _writer = writer;
+        _format = format;
+        _exception = null;
+        _dtdWriter = null;
+        _docWriter = null;
+        _pos = 0;
+    }
+
+
+    public IOException getException() {
+        return _exception;
+    }
+
+
+    /**
+     * Called by any of the DTD handlers to enter DTD mode.
+     * Once entered, all output will be accumulated in a string
+     * that can be printed as part of the document's DTD.
+     * This method may be called any number of time but will only
+     * have affect the first time it's called. To exist DTD state
+     * and get the accumulated DTD, call {@link #leaveDTD}.
+     */
+    public void enterDTD()
+            throws IOException {
+        // Can only enter DTD state once. Once we're out of DTD
+        // state, can no longer re-enter it.
+        if (_dtdWriter == null) {
+            flushLine(false);
+
+            _dtdWriter = new StringWriter();
+            _docWriter = _writer;
+            _writer = _dtdWriter;
+        }
+    }
+
+
+    /**
+     * Called by the root element to leave DTD mode and if any
+     * DTD parts were printer, will return a string with their
+     * textual content.
+     */
+    public String leaveDTD()
+            throws IOException {
+        // Only works if we're going out of DTD mode.
+        if (_writer == _dtdWriter) {
+            flushLine(false);
+
+            _writer = _docWriter;
+            return _dtdWriter.toString();
+        } else
+            return null;
+    }
+
+
+    public void printText(String text)
+            throws IOException {
+        try {
+            int length = text.length();
+            for (int i = 0; i < length; ++i) {
+                if (_pos == BufferSize) {
+                    _writer.write(_buffer);
+                    _pos = 0;
+                }
+                _buffer[_pos] = text.charAt(i);
+                ++_pos;
+            }
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void printText(StringBuffer text)
+            throws IOException {
+        try {
+            int length = text.length();
+            for (int i = 0; i < length; ++i) {
+                if (_pos == BufferSize) {
+                    _writer.write(_buffer);
+                    _pos = 0;
+                }
+                _buffer[_pos] = text.charAt(i);
+                ++_pos;
+            }
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void printText(char[] chars, int start, int length)
+            throws IOException {
+        try {
+            while (length-- > 0) {
+                if (_pos == BufferSize) {
+                    _writer.write(_buffer);
+                    _pos = 0;
+                }
+                _buffer[_pos] = chars[start];
+                ++start;
+                ++_pos;
+            }
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void printText(char ch)
+            throws IOException {
+        try {
+            if (_pos == BufferSize) {
+                _writer.write(_buffer);
+                _pos = 0;
+            }
+            _buffer[_pos] = ch;
+            ++_pos;
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void printSpace()
+            throws IOException {
+        try {
+            if (_pos == BufferSize) {
+                _writer.write(_buffer);
+                _pos = 0;
+            }
+            _buffer[_pos] = ' ';
+            ++_pos;
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void breakLine()
+            throws IOException {
+        try {
+            if (_pos == BufferSize) {
+                _writer.write(_buffer);
+                _pos = 0;
+            }
+            String line = _format.getLineSeparator();
+            char[] chL = line.toCharArray();
+            for (int i = 0; i < chL.length; i++) {
+                _buffer[_pos++] = chL[i];
+            }
+            //_buffer[ _pos ] = '\n';
+            //++_pos;
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+    }
+
+
+    public void breakLine(boolean preserveSpace)
+            throws IOException {
+        breakLine();
+    }
+
+
+    public void flushLine(boolean preserveSpace)
+            throws IOException {
+        // Write anything left in the buffer into the writer.
+        try {
+            _writer.write(_buffer, 0, _pos);
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+        }
+        _pos = 0;
+    }
+
+
+    /**
+     * Flush the output stream. Must be called when done printing
+     * the document, otherwise some text might be buffered.
+     */
+    public void flush()
+            throws IOException {
+        try {
+            _writer.write(_buffer, 0, _pos);
+            _writer.flush();
+        } catch (IOException except) {
+            // We don't throw an exception, but hold it
+            // until the end of the document.
+            if (_exception == null)
+                _exception = except;
+            throw except;
+        }
+        _pos = 0;
+    }
+
+
+    public void indent() {
+        // NOOP
+    }
+
+
+    public void unindent() {
+        // NOOP
+    }
+
+
+    public int getNextIndent() {
+        return 0;
+    }
+
+
+    public void setNextIndent(int indent) {
+    }
+
+
+    public void setThisIndent(int indent) {
+    }
+
+
+}

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Serializer.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Serializer.java?rev=1512568&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Serializer.java (added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/xml/serialize/Serializer.java Sat Aug 10 05:53:42 2013
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.jackrabbit.vault.util.xml.serialize;
+
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DocumentHandler;
+
+
+/**
+ * Interface for a DOM serializer implementation, factory for DOM and SAX
+ * serializers, and static methods for serializing DOM documents.
+ * <p/>
+ * To serialize a document using SAX events, create a compatible serializer
+ * and pass it around as a {@link
+ * org.xml.sax.DocumentHandler}. If an I/O error occurs while serializing, it will
+ * be thrown by {@link DocumentHandler#endDocument}. The SAX serializer
+ * may also be used as {@link org.xml.sax.DTDHandler}, {@link org.xml.sax.ext.DeclHandler} and
+ * {@link org.xml.sax.ext.LexicalHandler}.
+ * <p/>
+ * To serialize a DOM document or DOM element, create a compatible
+ * serializer and call it's {@link
+ * DOMSerializer#serialize(Document)} or {@link DOMSerializer#serialize(Element)} methods.
+ * Both methods would produce a full XML document, to serizlie only
+ * the portion of the document use {@link OutputFormat#setOmitXMLDeclaration}
+ * and specify no document type.
+ * <p/>
+ * The {@link OutputFormat} dictates what underlying serialized is used
+ * to serialize the document based on the specified method. If the output
+ * format or method are missing, the default is an XML serializer with
+ * UTF-8 encoding and now indentation.
+ *
+ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @author <a href="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
+ * @version $Revision$ $Date$
+ * @see DocumentHandler
+ * @see ContentHandler
+ * @see OutputFormat
+ * @see DOMSerializer
+ */
+public interface Serializer {
+
+
+    /**
+     * Specifies an output stream to which the document should be
+     * serialized. This method should not be called while the
+     * serializer is in the process of serializing a document.
+     */
+    public void setOutputByteStream(OutputStream output);
+
+
+    /**
+     * Specifies a writer to which the document should be serialized.
+     * This method should not be called while the serializer is in
+     * the process of serializing a document.
+     */
+    public void setOutputCharStream(Writer output);
+
+
+    /**
+     * Specifies an output format for this serializer. It the
+     * serializer has already been associated with an output format,
+     * it will switch to the new format. This method should not be
+     * called while the serializer is in the process of serializing
+     * a document.
+     *
+     * @param format The output format to use
+     */
+    public void setOutputFormat(OutputFormat format);
+
+
+    /**
+     * Return a {@link DocumentHandler} interface into this serializer.
+     * If the serializer does not support the {@link DocumentHandler}
+     * interface, it should return null.
+     */
+    public DocumentHandler asDocumentHandler()
+            throws IOException;
+
+
+    /**
+     * Return a {@link ContentHandler} interface into this serializer.
+     * If the serializer does not support the {@link ContentHandler}
+     * interface, it should return null.
+     */
+    public ContentHandler asContentHandler()
+            throws IOException;
+
+
+    /**
+     * Return a {@link DOMSerializer} interface into this serializer.
+     * If the serializer does not support the {@link DOMSerializer}
+     * interface, it should return null.
+     */
+    public DOMSerializer asDOMSerializer()
+            throws IOException;
+
+
+}
+
+
+
+
+



Mime
View raw message