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
package org.litesoft.aokeyhole.swing.mains.mementobeans.support;

import java.util.*;

import org.litesoft.aokeyhole.objects.*;
import org.litesoft.aokeyhole.objects.factories.*;
import org.litesoft.aokeyhole.objects.factories.mementobean.*;
import org.litesoft.core.typeutils.*;

public class AttributeData implements BeanMagicStrings
{
    private static final String[] VALID_TYPES = extractValidTypes( MementoBeanPropertyMetaDataDefinitionAccessor.INSTANCE );

    private int mID;
    private String mName;
    private String mType;
    private boolean mMementoableType;
    private boolean mNullEmpties;
    private boolean mNullZeros;
    private boolean mNullFalses;
    private boolean mRepeating;
    private String[] mNotes = Strings.EMPTY_ARRAY;

    public int getID()
    {
        return mID;
    }

    public String getName()
    {
        return mName;
    }

    public String getType()
    {
        return mType;
    }

    public boolean isMementoableType()
    {
        return mMementoableType;
    }

    public boolean isNullEmpties()
    {
        return mNullEmpties;
    }

    public boolean isNullZeros()
    {
        return mNullZeros;
    }

    public boolean isNullFalses()
    {
        return mNullFalses;
    }

    public boolean isRepeating()
    {
        return mRepeating;
    }

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

    public void setNotes( String[] pNotes )
    {
        mNotes = Strings.deNull( pNotes );
    }

    /**
     * An ID is a 2 digit value (with leading zeros) between 0 & 99
     */
    private void parseID( String pID )
    {
        mID = Integer.parseInt( pID );
    }

    /**
     * Parse the Pre & Post type indicators.
     *
     * @param pPreType           an AttributeProxy type: Mementoable, String, Integer, ...
     * @param pBracketedPostType Either an empty String of the class name of the Mementoable surrounded by generic type indicators ('<' & '>').
     */
    private void parseType( String pPreType, String pBracketedPostType )
    {
        // "String" & "" || "Mementoable" & "<DetailBean>"
        if ( !MEMENTOABLE_ATTRIBUTE_PROXY_TYPE.equals( pPreType ) )
        {
            Strings.assertNullOrEmpty( "BracketedPostType", pBracketedPostType );
            mType = Objects.assertOneOf( pPreType, VALID_TYPES );
            return;
        }
        mMementoableType = true;
        if ( pBracketedPostType.startsWith( "<" ) && pBracketedPostType.endsWith( ">" ) )
        {
            mType = pBracketedPostType.substring( 1, pBracketedPostType.length() - 1 );
            if ( Strings.isValidAttributeIdentifier( mType ) )
            {
                return;
            }
        }
        throw new IllegalArgumentException( "Unacceptable BracketedPostType: " + pBracketedPostType );
    }

    /**
     * Parse the name from the Attribute Proxy's Parameters.
     */
    private void parseName( String pAttributeProxyParameters )
    {
        // A string of 'null' which indicates that this is a legacy place holder
        // Single (double quoted) Names, e.g.: "FirstName", "Name", "Description"
        // or the above followed by a default instance, like:
        // "SpecialDetail", DetailBean.DEFAULT_INSTANCE
        // "Details", DetailBean.DEFAULT_INSTANCE
        String zName = pAttributeProxyParameters;
        if ( mMementoableType )
        {
            String zDefaultInstanceTail = ", " + mType + ".DEFAULT_INSTANCE";
            if ( !pAttributeProxyParameters.endsWith( zDefaultInstanceTail ) )
            {
                throw new IllegalArgumentException(
                        "Mementoable attribute proxy, but not default instance in the parameter section: " + pAttributeProxyParameters );
            }
            zName = pAttributeProxyParameters.substring( 0, pAttributeProxyParameters.length() - zDefaultInstanceTail.length() );
        }
        if ( !zName.equals( "null" ) )
        {
            if ( (zName.length() < 3) || !zName.startsWith( "\"" ) || !zName.endsWith( "\"" ) )
            {
                throw new IllegalArgumentException( "Malformed name '" + zName + "' in: " + pAttributeProxyParameters );
            }
            zName = zName.substring( 1, zName.length() - 1 );
            if ( !Strings.isAsciiIdentifier( zName ) )
            {
                throw new IllegalArgumentException( "Name '" + zName + "' not an Ascii Identifier in: " + pAttributeProxyParameters );
            }
            mName = zName;
        }
    }

    // /* 00 */ new StringAttributeProxy( "FirstName" ), //
    // /* 00 */ new StringAttributeProxy( "Name" ).nullEmpties(), //
    // /* 01 */ new StringAttributeProxy( null ), //
    // /* 02 */ new StringAttributeProxy( "Description" ).nullEmpties().repeating(), //
    // /* 03 */ new MementoableAttributeProxy<DetailBean>( "SpecialDetail", DetailBean.DEFAULT_INSTANCE ).nullEmpties(), //
    // 01234567-10123
    // /* 04 */ new MementoableAttributeProxy<DetailBean>( "Details", DetailBean.DEFAULT_INSTANCE ).nullEmpties().repeating(), //
    // 123  12345678           12345678901234            12                                      12                          1234
    // 0  1 2       3          4             5           6 7                                     8 9                         10
    private void parseFlags( String pFlags )
    {
        StringBuilder zFlags = new StringBuilder( pFlags );
        mNullEmpties = checkFor( ".nullEmpties()", zFlags );
        mNullZeros = checkFor( ".nullZeros()", zFlags );
        mNullFalses = checkFor( ".nullFalses()", zFlags );
        mRepeating = checkFor( ".repeating()", zFlags );
        if ( zFlags.length() != 0 )
        {
            throw new IllegalArgumentException( "Don't understand '" + zFlags + "' in: " + pFlags );
        }
    }

    private boolean checkFor( String pStringToFind, StringBuilder pFlags )
    {
        int zAt = pFlags.indexOf( pStringToFind );
        if ( zAt == -1 )
        {
            return false;
        }
        pFlags.delete( zAt, zAt + pStringToFind.length() );
        return true;
    }

    public static AttributeData parseLine( String pLine )
    {
        // /* 00 */ new StringAttributeProxy( "FirstName" ), //
        // /* 00 */ new StringAttributeProxy( "Name" ).nullEmpties(), //
        // /* 01 */ new StringAttributeProxy( null ), //
        // /* 02 */ new StringAttributeProxy( "Description" ).nullEmpties().repeating(), //
        // /* 03 */ new MementoableAttributeProxy<DetailBean>( "SpecialDetail", DetailBean.DEFAULT_INSTANCE ).nullEmpties(), //
        // 01234567-10123
        // /* 04 */ new MementoableAttributeProxy<DetailBean>( "Details", DetailBean.DEFAULT_INSTANCE ).nullEmpties().repeating(), //
        // 123  12345678           12345678901234            12                                      12                          1234
        // 0  1 2       3          4             5           6 7                                     8 9                         10
        int atPart2 = pLine.indexOf( ATTR_PART2 );
        int atPart4 = pLine.indexOf( ATTR_PART4, atPart2 + 1 );
        int atPart6 = pLine.indexOf( ATTR_PART6, atPart4 + 1 );
        int atPart8 = pLine.indexOf( ATTR_PART8, atPart6 + 1 );
        if ( pLine.startsWith( ATTR_PART0 ) && //
             (atPart2 != -1) && (atPart4 != -1) && (atPart6 != -1) && (atPart8 != -1) && //
             pLine.endsWith( ATTR_PART10 ) )
        {
            AttributeData zAttribute = new AttributeData();
            try
            {
                zAttribute.parseID( pLine.substring( 3, atPart2 ) );
                zAttribute.parseType( pLine.substring( 13, atPart4 ), pLine.substring( atPart4 + 14, atPart6 ) );
                zAttribute.parseName( pLine.substring( atPart6 + 2, atPart8 ) );
                zAttribute.parseFlags( pLine.substring( atPart8 + 2, pLine.length() - 4 ) );
                return zAttribute;
            }
            catch ( IllegalArgumentException e )
            {
                System.out.println( e.getMessage() );
                // Fall thru...
            }
        }
        return null;
    }

    private static String[] extractValidTypes( PropertyMetaDataDefinitionAccessor pAccessor )
    {
        List<String> zTypes = Lists.newArrayList();
        for ( AttributeSet zAttribute : pAccessor.getAttributeSets() )
        {
            AttributeType zType = zAttribute.getType();
            if ( zType.getFAT().isSimple() )
            {
                zTypes.add( zType.toString() );
            }
        }
        return zTypes.toArray( new String[zTypes.size()] );
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
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
827 Diff Diff GeorgeS picture GeorgeS Sun 26 Aug, 2012 17:14:31 +0000

!

821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
820 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 22:41:35 +0000

!

815 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:54:14 +0000
788 GeorgeS picture GeorgeS Sun 05 Aug, 2012 22:58:50 +0000

!