Subversion Repository Public Repository

litesoft

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package org.litesoft.aokeyhole.swing.mains.mementobeans.support;

import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

import org.litesoft.aokeyhole.objects.*;
import org.litesoft.aokeyhole.persist.*;

public class BeanReader implements ObjectReader,
                                   BeanMagicStrings
{
    private final String mName;
    private final String[] mNotes;
    private final List<AttributeData> mAttributeDatas;
    private int mNextAttibute = 0;

    public BeanReader( String pName, String[] pNotes, List<AttributeData> pAttributeDatas )
    {
        mName = pName;
        mNotes = pNotes;
        mAttributeDatas = Lists.deNullMutable( pAttributeDatas );
    }

    private BeanReader( String pName, String[] pNotes )
    {
        this( pName, pNotes, null );
    }

    public List<AttributeData> getAttributeDatas()
    {
        return mAttributeDatas;
    }

    @Override
    public String getType()
    {
        return "Bean";
    }

    @Override
    public String getName()
    {
        return mName;
    }

    @Override
    public String getParent()
    {
        return null;
    }

    @Override
    public String[] getNotes()
    {
        return mNotes;
    }

    @Override
    public ParseException unrecognizedType( String pMessage )
    {
        return new ParseException( "Bean (" + getName() + ") - " + pMessage );
    }

    @Override
    public PropertyReader nextProperty()
    {
        return null;  // No Properties.
    }

    @Override
    public AttributeReader nextAttribute()
    {
        while ( mNextAttibute < mAttributeDatas.size() )
        {
            AttributeData zAttribute = mAttributeDatas.get( mNextAttibute++ );
            if ( zAttribute.getName() != null )
            {
                return new OurAttributeReader( zAttribute );
            }
        }
        return null;
    }

    /**
     * Parse the lines for a "possible" java MementoBean and create a BeanReader if appropriate.
     *
     * @param pLines from a "possible" java MementoBean extending file.
     *
     * @return a BeanReader or null if the lines passed in do not represent a MementoBean.
     */
    public static BeanReader createFor( String... pLines )
    {
        if ( pLines.length < 30 )
        {
            return null;
        }
        int extendsLine = findLine( pLines, EXTENDS_ABSTRACT_MEMO_BEAN );
        if ( extendsLine == -1 )
        {
            return null;
        }
        String zDeclarationLine = pLines[extendsLine].trim();
        String zName = getBeanName( zDeclarationLine );
        if ( zName == null )
        {
            return null;
        }
        String[] zNotes = getJavaDocBefore( pLines, extendsLine );
        int attributesAt = findLine( pLines, "private static final AbstractAttributeProxy[] ATTRIBUTES = //" );
        if ( attributesAt == -1 || (attributesAt > (pLines.length - 32)) || !"{ //".equals( pLines[attributesAt + 1].trim() ) )
        {
            return null;
        }
        BeanReader zBR = new BeanReader( zName, zNotes );
        String zLine;
        for ( int i = 0; !"};".equals( zLine = getLine( pLines, attributesAt + 2 + i ) ); i++ )
        {
            AttributeData zAttributeData = parseAttributeData( i, zLine, pLines );
            if ( zAttributeData == null )
            {
                System.err.println( "Expected AttributeProxy Line (" + i + ") at " + (attributesAt + 2 + i) + ", but saw: " + zLine );
                return null;
            }
            zBR.mAttributeDatas.add( zAttributeData );
        }
        return zBR;
    }

    private static String getBeanName( String pLine )
    {
        return !pLine.endsWith( ">" ) ? //
               null : //
               Strings.noEmpty( pLine.substring( pLine.indexOf( EXTENDS_ABSTRACT_MEMO_BEAN ) + //
                                                 EXTENDS_ABSTRACT_MEMO_BEAN.length(), pLine.length() - 1 ) );
    }

    private static int findLine( String[] pLines, String pContainsText )
    {
        for ( int i = 0; i < pLines.length; i++ )
        {
            if ( pLines[i].contains( pContainsText ) )
            {
                return i;
            }
        }
        return -1;
    }

    private static String[] getJavaDocBefore( String[] pLines, int pLine )
    {
        int jdocStart = findJdocStart( pLines, pLine );
        if ( jdocStart != -1 )
        {
            List<String> noteLines = new ArrayList<String>();
            String zLine;
            for ( int i = jdocStart + 1; (i < pLine) && (zLine = getLine( pLines, i )).startsWith( "*" ); i++ )
            {
                if ( "*/".equals( zLine ) )
                {
                    break;
                }
                noteLines.add( zLine.startsWith( "* " ) ? zLine.substring( 2 ) : "" );
            }
            if ( !noteLines.isEmpty() )
            {
                return noteLines.toArray( new String[noteLines.size()] );
            }
        }
        return Strings.EMPTY_ARRAY;
    }

    private static int findJdocStart( String[] pLines, int pFrom )
    {
        for ( String zLine; (zLine = getLine( pLines, --pFrom )).length() != 0; )
        {
            if ( "/**".equals( zLine ) )
            {
                return pFrom;
            }
        }
        return -1;
    }

    private static String getLine( String[] pLines, int pLine )
    {
        if ( (0 <= pLine) && (pLine < pLines.length) )
        {
            return pLines[pLine].trim();
        }
        return "";
    }

    private static AttributeData parseAttributeData( int pExpectedId, String pLine, String[] pLines )
    {
        AttributeData zAttribute = AttributeData.parseLine( pLine );
        if ( (zAttribute == null) || (zAttribute.getID() != pExpectedId) )
        {
            return null; // Unable to Parse OR ID wrong!
        }
        if ( zAttribute.getName() == null )
        {
            return zAttribute; // No name means that this is a legacy reference space holder
        }
        String zType = zAttribute.getType();
        if ( zAttribute.isRepeating() )
        {
            zType = "List<" + zType + ">";
        }
        int getterAt = findLine( pLines, "public " + zType + " get" + zAttribute.getName() + "()" );
        if ( getterAt == -1 )
        {
            return null; // "Real" attribute, but no getter?
        }
        zAttribute.setNotes( getJavaDocBefore( pLines, getterAt ) );
        return zAttribute;
    }

    private ParseException error( String pMessage )
    {
        return unrecognizedType( pMessage );
    }

    private class OurAttributeReader implements AttributeReader
    {
        private final AttributeData mAttribute;
        private final String mType;
        private final String[] mAdditionalValues;
        private BeanAttributeProperties nextProperty = BeanAttributeProperties.Notes;

        private OurAttributeReader( AttributeData pAttribute )
        {
            mAttribute = pAttribute;
            if ( mAttribute.isMementoableType() )
            {
                mType = (mAttribute.isRepeating() ? FundamentalAttributeType.ToMany : FundamentalAttributeType.ToOne).toString();
                mAdditionalValues = new String[]{mAttribute.getType()};
                return;
            }
            mType = mAttribute.getType();
            mAdditionalValues = Strings.EMPTY_ARRAY; // TODO: Valid Options!
        }

        @Override
        public String getType()
        {
            return mType;
        }

        @Override
        public boolean isVirtual()
        {
            return false;
        }

        @Override
        public String getName()
        {
            return mAttribute.getName();
        }

        @Override
        public String[] getAdditionalValues( Integer pExpected )
        {
            if ( pExpected == null )
            {
                return mAdditionalValues;
            }
            int zExpected = Integers.assertNonNegative( "Expected", pExpected );
            if ( mAdditionalValues.length > zExpected )
            {
                throw error( "Expected (" + pExpected + ") Additional Parts (seperated by '|'), but was (" + mAdditionalValues.length + ") Parts" );
            }
            return Strings.expectArray( pExpected, mAdditionalValues );
        }

        @Override
        public ParseException unrecognizedType( String pMessage )
        {
            return error( pMessage );
        }

        @Override
        public PropertyReader nextProperty()
        {
            while ( nextProperty != null )
            {
                final String zData = nextProperty.getData( mAttribute );
                final String zWhat = nextProperty.toString();
                nextProperty = nextProperty.next();
                if ( zData != null )
                {
                    return new PropertyReader()
                    {
                        @Override
                        public String getWhat()
                        {
                            return zWhat;
                        }

                        @Override
                        public String getData()
                        {
                            return zData;
                        }
                    };
                }
            }
            return null;
        }
    }
}

Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/swing/mains/mementobeans/support/BeanReader.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

846 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 19:47:32 +0000
845 Diff Diff GeorgeS picture GeorgeS Thu 06 Sep, 2012 19:44:44 +0000
835 Diff Diff GeorgeS picture GeorgeS Sun 02 Sep, 2012 14:02:10 +0000
834 Diff Diff GeorgeS picture GeorgeS Sun 02 Sep, 2012 14:00:11 +0000
830 Diff Diff GeorgeS picture GeorgeS Fri 31 Aug, 2012 18:10:19 +0000
828 Diff Diff GeorgeS picture GeorgeS Wed 29 Aug, 2012 00:53:01 +0000
820 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 22:41:35 +0000

!

818 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:59:45 +0000
815 GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:54:14 +0000