Subversion Repository Public Repository

litesoft

Diff Revisions 939 vs 948 for /trunk/Java/KeyHole/src/org/litesoft/aokeyhole/persist/lines/SubSystemLinesPersister.java

Diff revisions: vs.
  @@ -1,177 +1,147 @@
1 1 package org.litesoft.aokeyhole.persist.lines;
2 2
3 - import java.io.*;
4 -
5 - import org.litesoft.commonfoundation.typeutils.*;
6 -
7 - import java.util.*;
8 -
9 3 import org.litesoft.aokeyhole.objects.*;
10 4 import org.litesoft.aokeyhole.objects.support.*;
11 5 import org.litesoft.aokeyhole.persist.*;
12 6 import org.litesoft.aokeyhole.toolkit.*;
7 + import org.litesoft.commonfoundation.typeutils.*;
13 8 import org.litesoft.commonfoundation.typeutils.Objects;
14 9
15 - public class SubSystemLinesPersister implements SubSystemPersister
16 - {
10 + import java.io.*;
11 + import java.util.*;
12 +
13 + public class SubSystemLinesPersister implements SubSystemPersister {
17 14 public static final String TOO_MANY_DATA_PARTS = "Data Section of this line had too many parts";
18 15 private static final String ATTRIBUTE_VIRTUAL_PREFIX = "Virtual-";
19 16 private static final String NOTES = "Notes";
20 17
21 18 private final LinesPersister mLinesPersister;
22 19
23 - public SubSystemLinesPersister( LinesPersister pLinesPersister )
24 - {
20 + public SubSystemLinesPersister( LinesPersister pLinesPersister ) {
25 21 mLinesPersister = Objects.assertNotNull( "LinesPersister", pLinesPersister );
26 22 }
27 23
28 24 @Override
29 - public String getName()
30 - {
25 + public String getName() {
31 26 return mLinesPersister.getName();
32 27 }
33 28
34 29 @Override
35 - public boolean isReadOnly()
36 - {
30 + public boolean isReadOnly() {
37 31 return mLinesPersister.isReadOnly();
38 32 }
39 33
40 34 @Override
41 35 public SubSystemReader getReader( String pExpectedVersion )
42 - throws IOException
43 - {
36 + throws IOException {
44 37 return new LinesSubSystemReader( pExpectedVersion, mLinesPersister.loadLines() );
45 38 }
46 39
47 40 @Override
48 - public SubSystemBuilder getBuilder( String pVersion, String pKeyHoleVersion )
49 - {
41 + public SubSystemBuilder getBuilder( String pVersion, String pKeyHoleVersion ) {
50 42 return new LinesSubSystemBuilder( mLinesPersister, pVersion, pKeyHoleVersion );
51 43 }
52 44
53 - private static class LinesSubSystemReader implements SubSystemReader
54 - {
45 + private static class LinesSubSystemReader implements SubSystemReader {
55 46 private ToLinesObjectsParseStream mOPS;
56 47
57 - private LinesSubSystemReader( String pExpectedVersion, String[] pLines )
58 - {
48 + private LinesSubSystemReader( String pExpectedVersion, String[] pLines ) {
59 49 mOPS = new ToLinesObjectsParseStream( pLines );
60 - if ( mOPS.readLine() )
61 - {
50 + if ( mOPS.readLine() ) {
62 51 mOPS.validateDepth( 0 );
63 52
64 53 String zType = mOPS.getWhat();
65 54
66 - if ( !"vs".equals( zType ) )
67 - {
55 + if ( !"vs".equals( zType ) ) {
68 56 throw mOPS.error( "First Line not a 'vs' (version) line" );
69 57 }
70 58 String[] zParts = mOPS.parseData( '|' );
71 59 String zActual = zParts[0];
72 - if ( !pExpectedVersion.equals( zActual ) )
73 - {
60 + if ( !pExpectedVersion.equals( zActual ) ) {
74 61 throw mOPS.error( "File version mismatch, got '" + zActual + "', but expected '" + pExpectedVersion + "'." );
75 62 }
76 63 }
77 64 }
78 65
79 66 @Override
80 - public ObjectReader nextObject()
81 - {
67 + public ObjectReader nextObject() {
82 68 return mOPS.readLine() ? new LinesObjectReader( mOPS ) : null;
83 69 }
84 70 }
85 71
86 - private static class LinesObjectReader implements ObjectReader
87 - {
72 + private static class LinesObjectReader implements ObjectReader {
88 73 private final ToLinesObjectsParseStream mOPS;
89 74 private final String mType;
90 75 private final String mName;
91 76 private final String mParent;
92 77 private final String[] mNotes;
93 78
94 - private LinesObjectReader( ToLinesObjectsParseStream pOPS )
95 - {
79 + private LinesObjectReader( ToLinesObjectsParseStream pOPS ) {
96 80 mOPS = pOPS;
97 81 mOPS.validateDepth( 0 );
98 82 mType = mOPS.getWhat();
99 83 String[] nameNparent = Strings.expectArray( 2, mOPS.parseData( '|' ) );
100 - if ( nameNparent == null )
101 - {
84 + if ( nameNparent == null ) {
102 85 throw mOPS.error( TOO_MANY_DATA_PARTS );
103 86 }
104 87 mName = nameNparent[0];
105 88 mParent = nameNparent[1];
106 - if ( pOPS.isNextDepth( 2 ) && pOPS.isNextWhat( "Notes" ) )
107 - {
89 + if ( pOPS.isNextDepth( 2 ) && pOPS.isNextWhat( "Notes" ) ) {
108 90 pOPS.readLine();
109 91 mNotes = mOPS.parseData( '|' );
110 - }
111 - else
112 - {
92 + } else {
113 93 mNotes = null;
114 94 }
115 95 }
116 96
117 97 @Override
118 - public String getType()
119 - {
98 + public String getType() {
120 99 return mType;
121 100 }
122 101
123 102 @Override
124 - public String getName()
125 - {
103 + public String getName() {
126 104 return mName;
127 105 }
128 106
129 107 @Override
130 - public String getParent()
131 - {
108 + public String getParent() {
132 109 return mParent;
133 110 }
134 111
135 112 @Override
136 - public String[] getNotes()
137 - {
113 + public String[] getNotes() {
138 114 return mNotes;
139 115 }
140 116
141 117 @Override
142 - public ParseException unrecognizedType( String pMessage )
143 - {
118 + public ParseException unrecognizedType( String pMessage ) {
144 119 return mOPS.error( pMessage );
145 120 }
146 121
147 122 @Override
148 - public PropertyReader nextProperty()
149 - {
123 + public PropertyReader nextProperty() {
150 124 return mOPS.isNextDepth( 2 ) ? new LinesPropertyReader( mOPS ) : null; // Properties
151 125 }
152 126
153 127 @Override
154 - public AttributeReader nextAttribute()
155 - {
128 + public AttributeReader nextAttribute() {
156 129 return mOPS.isNextDepth( 1 ) ? new LinesAttributeReader( mOPS ) : null; // Attributes
157 130 }
158 131 }
159 132
160 - private static class LinesAttributeReader implements AttributeReader
161 - {
133 + private static class LinesAttributeReader implements AttributeReader {
162 134 private final ToLinesObjectsParseStream mOPS;
163 135 private final boolean mVirtual;
164 136 private final String mType;
165 137 private final String mName;
166 138 private final String[] mAdditionalValues;
167 139
168 - private LinesAttributeReader( ToLinesObjectsParseStream pOPS )
169 - {
140 + private LinesAttributeReader( ToLinesObjectsParseStream pOPS ) {
170 141 (mOPS = pOPS).readLine();
171 142 String zType = mOPS.getWhat();
172 143 mVirtual = zType.startsWith( ATTRIBUTE_VIRTUAL_PREFIX );
173 - if ( mVirtual )
174 - {
144 + if ( mVirtual ) {
175 145 zType = zType.substring( ATTRIBUTE_VIRTUAL_PREFIX.length() );
176 146 }
177 147 mType = zType;
  @@ -181,115 +151,95 @@
181 151 }
182 152
183 153 @Override
184 - public String getType()
185 - {
154 + public String getType() {
186 155 return mType;
187 156 }
188 157
189 158 @Override
190 - public boolean isVirtual()
191 - {
159 + public boolean isVirtual() {
192 160 return mVirtual;
193 161 }
194 162
195 163 @Override
196 - public String getName()
197 - {
164 + public String getName() {
198 165 return mName;
199 166 }
200 167
201 168 @Override
202 - public String[] getAdditionalValues( Integer pExpected )
203 - {
204 - if ( pExpected == null )
205 - {
169 + public String[] getAdditionalValues( Integer pExpected ) {
170 + if ( pExpected == null ) {
206 171 return mAdditionalValues;
207 172 }
208 173 int zExpected = Integers.assertNonNegative( "Expected", pExpected );
209 - if ( mAdditionalValues.length > zExpected )
210 - {
174 + if ( mAdditionalValues.length > zExpected ) {
211 175 throw mOPS.error( "Expected (" + pExpected + ") Additional Parts (seperated by '|'), but was (" + mAdditionalValues.length + ") Parts" );
212 176 }
213 177 return Strings.expectArray( pExpected, mAdditionalValues );
214 178 }
215 179
216 180 @Override
217 - public ParseException unrecognizedType( String pMessage )
218 - {
181 + public ParseException unrecognizedType( String pMessage ) {
219 182 return mOPS.error( pMessage );
220 183 }
221 184
222 185 @Override
223 - public PropertyReader nextProperty()
224 - {
186 + public PropertyReader nextProperty() {
225 187 return mOPS.isNextDepth( 3 ) ? new LinesPropertyReader( mOPS ) : null; // Properties
226 188 }
227 189 }
228 190
229 - private static class LinesPropertyReader implements PropertyReader
230 - {
191 + private static class LinesPropertyReader implements PropertyReader {
231 192 private final String mWhat, mData;
232 193
233 - private LinesPropertyReader( ToLinesObjectsParseStream pOPS )
234 - {
194 + private LinesPropertyReader( ToLinesObjectsParseStream pOPS ) {
235 195 pOPS.readLine();
236 196 mWhat = pOPS.getWhat();
237 197 mData = pOPS.getData();
238 198 }
239 199
240 200 @Override
241 - public String getWhat()
242 - {
201 + public String getWhat() {
243 202 return mWhat;
244 203 }
245 204
246 205 @Override
247 - public String getData()
248 - {
206 + public String getData() {
249 207 return mData;
250 208 }
251 209 }
252 210
253 - private static class Doneable extends DoneableBuilder
254 - {
211 + private static class Doneable extends DoneableBuilder {
255 212 protected final List<String> mCollector;
256 213
257 - protected Doneable( String pID, List<String> pCollector )
258 - {
214 + protected Doneable( String pID, List<String> pCollector ) {
259 215 super( pID );
260 216 mCollector = pCollector;
261 217 }
262 218
263 - protected void LLaddNotes( String pDepthDots, String[] pNotes )
264 - {
219 + protected void LLaddNotes( String pDepthDots, String[] pNotes ) {
265 220 String zNotes = SingleLineNotes.convertLinesToString( pNotes );
266 - if ( zNotes != null )
267 - {
221 + if ( zNotes != null ) {
268 222 mCollector.add( pDepthDots + NOTES + ":" + zNotes );
269 223 }
270 224 }
271 225 }
272 226
273 - private static class LinesSubSystemBuilder extends Doneable implements SubSystemBuilder
274 - {
227 + private static class LinesSubSystemBuilder extends Doneable implements SubSystemBuilder {
275 228 private final LinesPersister mLinesPersister;
276 229
277 - private LinesSubSystemBuilder( LinesPersister pLinesPersister, String pVersion, String pKeyHoleVersion )
278 - {
230 + private LinesSubSystemBuilder( LinesPersister pLinesPersister, String pVersion, String pKeyHoleVersion ) {
279 231 super( SUB_SYSTEM, new LinkedList<String>() );
280 232 mLinesPersister = pLinesPersister;
281 233 mCollector.add( "vs:" + pVersion + "|" + pKeyHoleVersion );
282 234 }
283 235
284 236 @Override
285 - public ObjectBuilder createObjectBuilder( String pType, String pName, String pParentName, String[] pNotes )
286 - {
237 + public ObjectBuilder createObjectBuilder( String pType, String pName, String pParentName, String[] pNotes ) {
287 238 checkCanDone();
288 239 mCollector.add( "" );
289 240 mCollector.add( "" );
290 241 StringBuilder sb = new StringBuilder( pType ).append( ':' ).append( pName );
291 - if ( pParentName != null )
292 - {
242 + if ( pParentName != null ) {
293 243 sb.append( '|' ).append( pParentName );
294 244 }
295 245 mCollector.add( sb.toString() );
  @@ -298,48 +248,39 @@
298 248 }
299 249
300 250 @Override
301 - public SubSystemWriter done()
302 - {
251 + public SubSystemWriter done() {
303 252 LLdone();
304 253 return new LinesSubSystemWriter( mLinesPersister, mCollector.toArray( new String[mCollector.size()] ) );
305 254 }
306 255 }
307 256
308 - private static abstract class OADoneable extends Doneable implements DoneablePropertyBuilder
309 - {
257 + private static abstract class OADoneable extends Doneable implements DoneablePropertyBuilder {
310 258 private final String mPropertyDepthDots;
311 259
312 - protected OADoneable( String pID, List<String> pCollector, String pPropertyDepthDots )
313 - {
260 + protected OADoneable( String pID, List<String> pCollector, String pPropertyDepthDots ) {
314 261 super( pID, pCollector );
315 262 mPropertyDepthDots = pPropertyDepthDots;
316 263 }
317 264
318 - public final void done()
319 - {
265 + public final void done() {
320 266 LLdone();
321 267 }
322 268
323 269 @Override
324 - public boolean recordAllProperties()
325 - {
270 + public boolean recordAllProperties() {
326 271 return false;
327 272 }
328 273
329 274 @Override
330 - public final void addProperty( String pName, String pValue, String... pAdditionalValues )
331 - {
275 + public final void addProperty( String pName, String pValue, String... pAdditionalValues ) {
332 276 checkCanAddProperty( getWhyCantAddProperty() );
333 277 pValue = Strings.deNull( pValue );
334 - if ( pAdditionalValues != null )
335 - {
336 - for ( String zValue : pAdditionalValues )
337 - {
278 + if ( pAdditionalValues != null ) {
279 + for ( String zValue : pAdditionalValues ) {
338 280 pValue += "|" + Strings.deNull( zValue );
339 281 }
340 282 }
341 - if ( pValue.length() != 0 )
342 - {
283 + if ( pValue.length() != 0 ) {
343 284 mCollector.add( mPropertyDepthDots + pName + ":" + pValue );
344 285 }
345 286 }
  @@ -347,39 +288,32 @@
347 288 abstract protected String getWhyCantAddProperty();
348 289 }
349 290
350 - private static class LinesObjectBuilder extends OADoneable implements ObjectBuilder
351 - {
291 + private static class LinesObjectBuilder extends OADoneable implements ObjectBuilder {
352 292 private final String mName;
353 293 private boolean mAnyAttributesAdded = false;
354 294
355 - private LinesObjectBuilder( List<String> pCollector, String pName )
356 - {
295 + private LinesObjectBuilder( List<String> pCollector, String pName ) {
357 296 super( objectID( pName ), pCollector, ".." );
358 297 mName = pName;
359 298 }
360 299
361 300 @Override
362 - protected String getWhyCantAddProperty()
363 - {
301 + protected String getWhyCantAddProperty() {
364 302 return !mAnyAttributesAdded ? null : ATTRIBUTES_ALREADY_ADDED;
365 303 }
366 304
367 305 @Override
368 - public AttributeBuilder createAttributeBuilder( String pType, boolean pVirtual, String pName, String[] pNotes, String[] pAdditionalValues )
369 - {
306 + public AttributeBuilder createAttributeBuilder( String pType, boolean pVirtual, String pName, String[] pNotes, String[] pAdditionalValues ) {
370 307 checkCanDone();
371 308 mAnyAttributesAdded = true;
372 309 mCollector.add( "" );
373 310 StringBuilder sb = new StringBuilder( "." );
374 - if ( pVirtual )
375 - {
311 + if ( pVirtual ) {
376 312 sb.append( ATTRIBUTE_VIRTUAL_PREFIX );
377 313 }
378 314 sb.append( pType ).append( ':' ).append( pName );
379 - if ( pAdditionalValues != null )
380 - {
381 - for ( String zValue : pAdditionalValues )
382 - {
315 + if ( pAdditionalValues != null ) {
316 + for ( String zValue : pAdditionalValues ) {
383 317 sb.append( '|' ).append( Strings.deNull( zValue ) );
384 318 }
385 319 }
  @@ -389,35 +323,29 @@
389 323 }
390 324 }
391 325
392 - private static class LinesAttributeBuilder extends OADoneable implements AttributeBuilder
393 - {
394 - private LinesAttributeBuilder( List<String> pCollector, String pObjectName, String pName )
395 - {
326 + private static class LinesAttributeBuilder extends OADoneable implements AttributeBuilder {
327 + private LinesAttributeBuilder( List<String> pCollector, String pObjectName, String pName ) {
396 328 super( attributeID( pObjectName, pName ), pCollector, "..." );
397 329 }
398 330
399 331 @Override
400 - protected String getWhyCantAddProperty()
401 - {
332 + protected String getWhyCantAddProperty() {
402 333 return null;
403 334 }
404 335 }
405 336
406 - private static class LinesSubSystemWriter implements SubSystemWriter
407 - {
337 + private static class LinesSubSystemWriter implements SubSystemWriter {
408 338 private final LinesPersister mLinesPersister;
409 339 private final String[] mLines;
410 340
411 - private LinesSubSystemWriter( LinesPersister pLinesPersister, String[] pLines )
412 - {
341 + private LinesSubSystemWriter( LinesPersister pLinesPersister, String[] pLines ) {
413 342 mLinesPersister = pLinesPersister;
414 343 mLines = pLines;
415 344 }
416 345
417 346 @Override
418 347 public void write()
419 - throws IOException
420 - {
348 + throws IOException {
421 349 mLinesPersister.saveLines( mLines );
422 350 }
423 351 }