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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.orsup.nonpublic;

import org.litesoft.commonfoundation.typeutils.*;

public class DottedReference
{
    public static final String SEPERATOR = ".";
    public static final String TO_SELF = ".";

    public static final char SELECTOR_START = '[';
    public static final char SELECTOR_END = ']';
    public static final char SELECTOR_BY_VALUE_SEPERATOR = '=';

    public static Parts parse( String pReference )
            throws IllegalArgumentException
    {
        return new Parts( pReference );
    }

    public interface Selector
    {
    }

    public static class SelectorIndexed implements Selector
    {
        private int mIndex;

        private SelectorIndexed( int pIndex )
        {
            mIndex = pIndex;
        }

        public int getIndex()
        {
            return mIndex;
        }

        @Override
        public String toString()
        {
            return Integer.toString( mIndex );
        }
    }

    public static class SelectorByValue implements Selector
    {
        private String mValue;
        private Parts mParts;

        private SelectorByValue( String pValue, Parts pParts )
        {
            mValue = pValue;
            mParts = pParts;
        }

        public Object getValue()
        {
            return mValue;
        }

        public Parts getParts()
        {
            return mParts;
        }

        @Override
        public String toString()
        {
            return mValue + SELECTOR_BY_VALUE_SEPERATOR + mParts;
        }
    }

    public static class Parts
    {
        private String mLocalReference;
        private Selector mSelector = null;
        private String mRestOfReference = null;

        public String getLocalReference()
        {
            return mLocalReference;
        }

        public Selector getSelector()
        {
            return mSelector;
        }

        public String getRestOfReference()
        {
            return mRestOfReference;
        }

        @Override
        public String toString()
        {
            StringBuilder sb = new StringBuilder( mLocalReference );
            if ( mSelector != null )
            {
                sb.append( SELECTOR_START ).append( mSelector ).append( SELECTOR_END );
            }
            if ( mRestOfReference != null )
            {
                sb.append( SEPERATOR ).append( mRestOfReference );
            }
            return sb.toString();
        }

        private void setLocalReference( String pReference, String pLocalReference )
        {
            if ( null == (mLocalReference = Strings.noEmpty( pLocalReference )) )
            {
                throw new IllegalArgumentException( "No Local Reference in '" + pReference + "'" );
            }
        }

        private void setRestOfReference( String pReference, String pRestOfReference )
        {
            if ( null == (mRestOfReference = Strings.noEmpty( pRestOfReference )) )
            {
                throw new IllegalArgumentException( "Nothing after '" + SEPERATOR + "' in '" + pReference + "'" );
            }
        }

        private IllegalArgumentException badSelector( String pReference, String pWhy )
        {
            return new IllegalArgumentException( "Invalid formated Selector in '" + pReference + "': " + pWhy );
        }

        private Parts( String pReference )
        {
            pReference = Strings.deNull( pReference ).trim();
            int sepAt = pReference.indexOf( SEPERATOR );
            int selectorStartsAt = pReference.indexOf( SELECTOR_START );
            if ( sepAt == -1 )
            {
                if ( selectorStartsAt == -1 )
                {
                    // neither a Sep or a Selector
                    setLocalReference( pReference, pReference );
                    return;
                }
                int lastCharAt = pReference.length() - 1;
                if ( SELECTOR_END == pReference.charAt( lastCharAt ) )
                {
                    // Just a Local Ref & Selector
                    setLocalReference( pReference, pReference.substring( 0, selectorStartsAt ) );
                    mSelector = parseSelector( pReference, pReference.substring( selectorStartsAt + 1, lastCharAt ) );
                    return;
                }
                throw badSelector( pReference, "End Not at End" );
            }
            if ( (selectorStartsAt == -1) || (sepAt < selectorStartsAt) )
            {
                // Is a Sep & Either No Selector or the Selector is in the Rest
                setLocalReference( pReference, pReference.substring( 0, sepAt ) );
                setRestOfReference( pReference, pReference.substring( sepAt + 1 ) );
                return;
            }
            // Selector and Sep are all mixed up - so isolate the 'whole' selector
            int selectorEndsAt = locateSelectorEnd( pReference, selectorStartsAt );
            setLocalReference( pReference, pReference.substring( 0, selectorStartsAt ) );
            mSelector = parseSelector( pReference, pReference.substring( selectorStartsAt + 1, selectorEndsAt ) );
            String zRest = pReference.substring( selectorEndsAt + 1 ).trim();
            if ( zRest.length() != 0 )
            {
                if ( zRest.startsWith( SEPERATOR ) )
                {
                    setRestOfReference( pReference, zRest.substring( 1 ) );
                    return;
                }
            }
            throw new IllegalArgumentException( "Invalid format after '" + SELECTOR_END + "' in '" + pReference + "'" );
        }

        private int locateSelectorEnd( String pReference, int pSelectorStartsAt )
        {
            int possibleEnd = pReference.indexOf( SELECTOR_END, pSelectorStartsAt + 1 );
            if ( possibleEnd == -1 )
            {
                throw badSelector( pReference, "No End" );
            }
            int matchingStartAt = pReference.lastIndexOf( SELECTOR_START, possibleEnd );
            while ( pSelectorStartsAt != matchingStartAt )
            {
                int backFrom = matchingStartAt - 1;
                matchingStartAt = pReference.lastIndexOf( SELECTOR_START, backFrom );
                int endFrom = possibleEnd + 1;
                possibleEnd = pReference.indexOf( SELECTOR_END, endFrom );
                if ( (possibleEnd == -1) || (matchingStartAt == -1) )
                {
                    throw badSelector( pReference, "No matching End" );
                }
            }
            return possibleEnd;
        }

        private Selector parseSelector( String pReference, String pSelector )
        {
            if ( null == (pSelector = Strings.noEmpty( pSelector )) )
            {
                throw badSelector( pReference, "No Selector text" );
            }
            int byValueAt = pSelector.indexOf( SELECTOR_BY_VALUE_SEPERATOR );
            if ( byValueAt == -1 )
            {
                int index;
                try
                {
                    index = Integer.parseInt( pSelector );
                }
                catch ( NumberFormatException e )
                {
                    throw badSelector( pReference, "No '" + SELECTOR_BY_VALUE_SEPERATOR + "', expected index" );
                }
                return new SelectorIndexed( index );
            }
            String value = pSelector.substring( 0, byValueAt ).trim();
            String rest = pSelector.substring( byValueAt + 1 ).trim();
            if ( rest.length() == 0 )
            {
                throw badSelector( pReference, "Nothing after '" + SELECTOR_BY_VALUE_SEPERATOR + "'" );
            }
            return new SelectorByValue( value, new Parts( rest ) );
        }
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/nonpublic/DottedReference.java

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

Extracting commonfoundation

802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
610 Diff Diff GeorgeS picture GeorgeS Mon 12 Mar, 2012 00:54:00 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000