Subversion Repository Public Repository

WOX2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package wox.serial;

import java.io.*;
import org.litesoft.core.typeutils.Objects;
import java.util.*;

import org.jdom.*;
import org.jdom.output.*;

/**
 * Version: 1.0
 * Author: Carlos R. Jaimez Gonzalez
 * Author: Simon M. Lucas
 * Site: http://woxserializer.sourceforge.net/
 *
 * Version: 1.5
 * Author: Steven M Lewis
 *
 * Version: 2.0
 * Author: George Smith
 * SVN: http://svn.xp-dev.com/svn/WOX2/
 * Note: XML form for vs 2 is more compact and therefor incompatible with vs 1
 */

public class XMLwriterImpl implements XMLwriter
{
    private Writer mWriter;
    private boolean mPrettyPrint;
    private Stack<Element> mElements = new Stack<Element>();
    private boolean mTopElementStartTagSealed;

    public XMLwriterImpl( Writer pWriter, boolean pPrettyPrint )
    {
        Util.assertRequired( "Writer", mWriter = pWriter );
        mPrettyPrint = pPrettyPrint;
    }

    public void writeStartElement( String pName )
    {
        Util.assertXmlName( "Name", pName );
        assertXmlNotWritten();
        Element zElement = new Element( pName );
        mElements.push( zElement );
        mTopElementStartTagSealed = false;
    }

    public void writeAttributeString( String pName, String pValue )
    {
        Util.assertXmlName( "Name", pName );
        Util.assertRequired( "Value", pValue );
        assertTopElementNotSealed( "writeAttributeString" );
        mElements.peek().setAttribute( pName, XMLutils.encodeText( pValue ) );
    }

    public void writeElementText( String pElementText )
    {
        Util.assertRequired( "ElementText", pElementText );
        assertTopElementNotSealed( "writeElementText" );
        mElements.peek().setText( XMLutils.encodeText( pElementText ) );
        mTopElementStartTagSealed = true;
    }

    public void writeEndElement()
    {
        assertIsTopElement();
        mTopElementStartTagSealed = true;
        Element zElement = mElements.pop();
        if ( !mElements.isEmpty() )
        {
            mElements.peek().addContent( zElement );
            return;
        }
        Format zFormat = mPrettyPrint ? Format.getPrettyFormat() : Format.getCompactFormat();
        XMLOutputter out = new XMLOutputter( zFormat.setOmitDeclaration( false ) );
        try
        {
            out.output( zElement, mWriter );
            Closeable zCloseable = mWriter;
            mWriter = null;
            zCloseable.close();
        }
        catch ( IOException e )
        {
            throw new IllegalStateException( e );
        }
        finally
        {
            dispose();
        }
    }

    public void dispose()
    {
        if ( mWriter != null )
        {
            mElements.clear();
            try
            {
                mWriter.close();
            }
            catch ( IOException e )
            {
                // Whatever
            }
            mWriter = null;
        }
    }

    @Override
    protected void finalize()
            throws Throwable
    {
        dispose();
        super.finalize();
    }

    private void assertXmlNotWritten()
    {
        if ( mWriter == null )
        {
            throw new IllegalStateException( "Root Element already closed and Written" );
        }
    }

    private void assertIsTopElement()
    {
        assertXmlNotWritten();
        if ( mElements.isEmpty() )
        {
            throw new IllegalStateException( "No Root Element Written" );
        }
    }

    private void assertTopElementNotSealed( String pWhat )
    {
        assertIsTopElement();
        if ( mTopElementStartTagSealed )
        {
            throw new IllegalStateException( "May not '" + pWhat + "', as the current Element has already been sealed" );
        }
    }
}

Commits for WOX2/trunk/Java/src/wox/serial/XMLwriterImpl.java

Diff revisions: vs.
Revision Author Commited Message
17 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:44:02 +0000

Extracting commonfoundation

15 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 22:29:49 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000