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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package wox.serial;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.*;
import org.jdom.input.*;
import org.w3c.dom.*;

/**
 * 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 XMLreaderImpl implements XMLreader
{
    private DomIterator mDomIterator;
    private int mNextBase;
    private Element mCurrentElement = null;

    private XMLreaderImpl( DomIterator pDomIterator )
    {
        if ( null != (mDomIterator = pDomIterator) )
        {
            mNextBase = mDomIterator.getCurrentDepth();
        }
    }

    public XMLreaderImpl( Reader pReader )
            throws DOMException, IOException
    {
        this( new DomIterator( pReader ) );
    }

    public boolean readStartElement()
    {
        if ( mDomIterator != null )
        {
            if ( null != (mCurrentElement = (mCurrentElement == null) ? mDomIterator.first() : mDomIterator.next( mNextBase )) )
            {
                return true;
            }
            mDomIterator = null;
        }
        return false;
    }

    public boolean hasAttributesOrElementText()
    {
        List zAttributes = mCurrentElement.getAttributes();
        return ((zAttributes != null) && !zAttributes.isEmpty()) || (null != readElementText());
    }

    public String getAttribute( String pName )
    {
        assertIsCurrentElement();
        return XMLutils.decodeText( mCurrentElement.getAttributeValue( pName ) );
    }

    public String readElementText()
    {
        assertIsCurrentElement();
        String s = mCurrentElement.getText();
        return XMLutils.decodeText( (s != null) ? s : "" );
    }

    public XMLreader createSubtreeReader()
    {
        assertIsCurrentElement();
        return new XMLreaderImpl( mDomIterator );
    }

    public void dispose()
    {
        mDomIterator = null;
        mCurrentElement = null;
    }

    private void assertIsCurrentElement()
    {
        if ( mCurrentElement == null )
        {
            throw new IllegalStateException( "No Current Element" );
        }
    }

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

    public static class DomIterator
    {
        /**
         * The Top is returned with first(), otherwise the Top is what WAS returned and is used by next() to determine what to return
         */
        private Stack<Entry> mElements = new Stack<Entry>();

        public DomIterator( Reader pReader )
                throws DOMException, IOException
        {
            try
            {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build( pReader );
                mElements.push( new Entry( doc.getRootElement(), 0 ) );
            }
            catch ( JDOMException e )
            {
                throw new OurDOMException( e );
            }
        }

        public int getCurrentDepth()
        {
            return mElements.size();
        }

        public Element first()
        {
            return !mElements.isEmpty() ? mElements.peek().getElement() : null;
        }

        public Element next( int pCurrentDepthLimit )
        {
            while ( !mElements.isEmpty() )
            {
                // First try to return my next (first?) Child
                Entry zEntry = mElements.peek();
                int zNextChild = zEntry.getAndIncNextChild();
                Element zElement = zEntry.getElement();
                List zChildren = zElement.getChildren();
                if ( (zChildren != null) && (zNextChild < zChildren.size()) )
                {
                    zElement = (Element) zChildren.get( zNextChild );
                    mElements.push( new Entry( zElement, 0 ) );
                    return zElement;
                }
                // Next Get My Sibling
                if ( mElements.size() <= pCurrentDepthLimit ) // No more at this level
                {
                    return null;
                }
                mElements.pop(); // loop using Parent
            }
            return null;
        }

        private static class Entry
        {
            private Element mElement;
            private int mNextChild;

            private Entry( Element pElement, int pNextChild )
            {
                mElement = pElement;
                mNextChild = pNextChild;
            }

            public Element getElement()
            {
                return mElement;
            }

            public int getAndIncNextChild()
            {
                return mNextChild++;
            }
        }
    }

    public static class OurDOMException extends DOMException
    {
        public OurDOMException( JDOMException pCause )
        {
            super( (short) 0, pCause.getMessage() );
            initCause( pCause );
        }
    }
}

Commits for WOX2/trunk/Java/src/wox/serial/XMLreaderImpl.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