litesoft
@ 939
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.hierarchicaldata; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.charstreams.*; import org.litesoft.core.util.*; public class HierarchicalDataSimpleFactory extends HierarchicalData implements HierarchicalDataFactory { public static final HierarchicalDataFactory INSTANCE = new HierarchicalDataSimpleFactory(); private HierarchicalDataSimpleFactory() { } public HierarchicalDataSource createSource( String pDataSource ) { LineSource ls = new LineSource( Strings.stringToLines( pDataSource ) ); if ( !ls.hasCurrentLine() ) { throw new IllegalArgumentException( "No Lines for 'Simple' HierarchicalDataSource" ); } return new SimpleSource( ls ); } public HierarchicalDataSink createSink( StringBuilder pDataSink ) { return new SimpleSink( pDataSink ); } private static class SimpleLine { private String mLine = null; private int mOffset = -1; private int mDepth = 0; private String mName = null; private Map<String, String> mAttributeNameValues = new HashMap<String, String>(); public SimpleLine( int pDepth, String pName ) { mDepth = pDepth; mName = validateName( "Name", pName ); } public SimpleLine( LineSource pLineSourceOfToStringsToParseCurrent ) { mLine = pLineSourceOfToStringsToParseCurrent.getCurrentLine().trim(); mOffset = pLineSourceOfToStringsToParseCurrent.getCurrentLineOffset(); pLineSourceOfToStringsToParseCurrent.advance(); int at = mLine.indexOf( '(' ); if ( (at != -1) && mLine.endsWith( ")" ) ) { while ( mLine.charAt( mDepth ) == '.' ) { mDepth++; } mName = mLine.substring( mDepth, at ).trim(); if ( (mName.length() != 0) && parseAttributes( mLine.substring( at + 1, mLine.length() - 1 ).trim() ) ) { return; } } throw new IllegalArgumentException( "Unacceptable SimpleLine @" + mOffset + ": " + mLine ); } private boolean parseAttributes( String pNameValues ) { if ( pNameValues.length() != 0 ) { CharSource cs = new CharSourceFromSequence( pNameValues ); while ( cs.anyRemaining() ) { String name = getUpTo( cs, '=' ).trim(); if ( name.length() == 0 ) { return false; } cs.get(); // eat '=' consumeSpaces( cs ); if ( cs.anyRemaining() ) { StringBuilder valueSB = new StringBuilder(); if ( cs.peek() != '\'' ) { parseRegularAttributeValue( valueSB, cs ); if ( valueSB.length() == 0 ) { return false; } } else if ( !parseQuotedAttributeValue( valueSB, cs ) ) { return false; } consumeSpaces( cs ); mAttributeNameValues.put( name, valueSB.toString() ); } } } return true; } private void consumeSpaces( CharSource pCS ) { while ( pCS.peek() == ' ' ) { pCS.get(); } } private String getUpTo( CharSource pCS, char c ) { StringBuilder sb = new StringBuilder(); while ( pCS.anyRemaining() ) { if ( c == pCS.peek() ) { return sb.toString(); } sb.append( pCS.getRequired() ); } return ""; } private void parseRegularAttributeValue( StringBuilder pValueSB, CharSource pCS ) { for ( int c = pCS.peek(); (c != -1) && (c != ' '); c = pCS.peek() ) { pValueSB.append( (char) pCS.get() ); } } private void addRegularAttributeValue( StringBuilder pSB, String pValue ) { pSB.append( pValue ); } private boolean parseQuotedAttributeValue( StringBuilder pValueSB, CharSource pCS ) { pCS.get(); // eat the ' for ( int c; -1 != (c = pCS.get()); ) { if ( c == '\'' ) { if ( c != pCS.peek() ) // Not Double '', then at end! { return true; } pCS.get(); // Drop the Double! pValueSB.append( (char) c ); } else if ( c != '\\' ) { pValueSB.append( (char) c ); } else { switch ( pCS.get() ) { default: return false; case '\\': pValueSB.append( '\\' ); break; case 'r': pValueSB.append( '\r' ); break; case 'n': pValueSB.append( '\n' ); break; case 'x': char[] chars = new char[4]; for ( int i = 0; i < chars.length; i++ ) { if ( -1 == (c = pCS.get()) ) { return false; } chars[i] = (char) c; } try { pValueSB.append( Hex.from4Chars( chars ) ); } catch ( IllegalArgumentException e ) { return false; } break; } } } return false; } private void addQuotedAttributeValue( StringBuilder pSB, String pValue ) { pSB.append( '\'' ); for ( int i = 0; i < pValue.length(); i++ ) { char c = pValue.charAt( i ); switch ( c ) { case '\'': case '\\': pSB.append( c ); // double it up pSB.append( c ); break; case '\r': pSB.append( "\\r" ); break; case '\n': pSB.append( "\\n" ); break; default: if ( (c < ' ') || (127 <= c) ) { pSB.append( "\\x" ); char[] chars = Hex.to4Chars( c ); for ( char aChar : chars ) { pSB.append( aChar ); } } else { pSB.append( c ); } break; } } pSB.append( '\'' ); } private boolean anyBadChars( String pToCheck ) { for ( int i = 0; i < pToCheck.length(); i++ ) { char c = pToCheck.charAt( i ); if ( (c <= ' ') || (127 <= c) || (-1 != ",()'\"".indexOf( c )) ) { return true; } } return false; } private void addAttributeValue( StringBuilder pSB, String pValue ) { if ( (pValue.length() != 0) && !anyBadChars( pValue ) ) { addRegularAttributeValue( pSB, pValue ); } else { addQuotedAttributeValue( pSB, pValue ); } } private void addAttribute( StringBuilder pSB, String pName ) { pSB.append( pName ); pSB.append( '=' ); addAttributeValue( pSB, getAttributeValue( pName ) ); } public void toString( StringBuilder pSB ) { for ( int i = 0; i < mDepth; i++ ) { pSB.append( '.' ); } pSB.append( mName ); pSB.append( '(' ); if ( !mAttributeNameValues.isEmpty() ) { String[] names = getSortedAttributeNames(); addAttribute( pSB, names[0] ); for ( int i = 1; i < names.length; i++ ) { pSB.append( ' ' ); addAttribute( pSB, names[i] ); } } pSB.append( ')' ); } public String toString() { StringBuilder sb = new StringBuilder(); toString( sb ); return sb.toString(); } public String getLine() { return mLine; } public int getOffset() { return mOffset; } public int getDepth() { return mDepth; } public void setDepth( int pDepth ) { mDepth = pDepth; } public String getName() { return mName; } public void setName( String pName ) { mName = pName; } public String[] getSortedAttributeNames() { return Maps.getSortedKeysAsStrings( mAttributeNameValues ); } public String getAttributeValue( String pAttributeName ) { return mAttributeNameValues.get( pAttributeName ); } public void setAttribute( String pName, String pValue ) { if ( pValue != null ) { mAttributeNameValues.put( pName, pValue ); } } } private static class SimpleLineSource { private LineSource mLineSource; private SimpleLine mCachedNextSimpleLine = null; public SimpleLineSource( LineSource pLineSource ) { mLineSource = pLineSource; } public SimpleLine getNextWithDepth( int pDepth ) { if ( mCachedNextSimpleLine == null ) { if ( mLineSource.hasCurrentLine() ) { mCachedNextSimpleLine = new SimpleLine( mLineSource ); } } if ( mCachedNextSimpleLine != null ) { int zDepth = mCachedNextSimpleLine.getDepth(); if ( zDepth > pDepth ) { throw new IllegalArgumentException( "Unacceptable SimpleLine depth (Expected: " + pDepth + ", but was: " + zDepth + ") @" + mCachedNextSimpleLine.getOffset() + ": " + mCachedNextSimpleLine.getLine() ); } if ( zDepth == pDepth ) { SimpleLine rv = mCachedNextSimpleLine; mCachedNextSimpleLine = null; return rv; } } return null; } } private static class SimpleSource implements HierarchicalDataSource { private SimpleLineSource mSimpleLineSource; private boolean mRootHandedOut = false; public SimpleSource( LineSource pLineSource ) { mSimpleLineSource = new SimpleLineSource( pLineSource ); } public Node getRootNode() { if ( mRootHandedOut ) { throw new IllegalStateException( "Attempt to call getRootNode twice" ); } SimpleLine simpleLine = mSimpleLineSource.getNextWithDepth( 0 ); if ( simpleLine == null ) { throw new IllegalArgumentException( "No Root Node" ); } mRootHandedOut = true; return new SourceNode( simpleLine ); } private class SourceNode implements Node { private SimpleLine mSimpleLine; public SourceNode( SimpleLine pSimpleLine ) { mSimpleLine = pSimpleLine; } public String getName() { return mSimpleLine.getName(); } public String[] getAttributeNames() { return mSimpleLine.getSortedAttributeNames(); } public String getAttributeValue( String pAttributeName ) { return mSimpleLine.getAttributeValue( pAttributeName ); } public Node getNextChild() { int depth = mSimpleLine.getDepth(); SimpleLine simpleLine = mSimpleLineSource.getNextWithDepth( depth + 1 ); if ( simpleLine != null ) { return new SourceNode( simpleLine ); } if ( depth == 0 ) { SimpleLine anotherRoot = mSimpleLineSource.getNextWithDepth( 0 ); if ( anotherRoot != null ) { throw new IllegalArgumentException( "2nd Root Node: " + anotherRoot.getName() ); } } return null; } public void assertNoMoreChildren() { assertNoMoreChildren( getNextChild() ); } public void assertNoMoreChildren( Node pPossiblePrefetchedChild ) { if ( pPossiblePrefetchedChild != null ) { throw new IllegalArgumentException( "No more children expected, but got: " + pPossiblePrefetchedChild.getName() ); } } } } private static class SimpleSink implements HierarchicalDataSink { private StringBuilder mDataSink; private boolean mRootCreated = false; public SimpleSink( StringBuilder pDataSink ) { mDataSink = pDataSink; } public Node createRootNode( String pName ) { if ( mRootCreated ) { throw new IllegalStateException( "Attempt to call createRootNode twice" ); } SinkNode rv = new SinkNode( 0, pName ); mRootCreated = true; return rv; } private class SinkNode implements Node { private int mDepth; private SimpleLine mSimpleLine; private boolean mCanAddAttributes = true; private boolean mCanAddChildren = true; private boolean mDoneCalled = false; public SinkNode( int pDepth, String pName ) { mDepth = pDepth; mSimpleLine = new SimpleLine( pDepth, pName ); } public void setAttribute( String pAttributeName, String pValue ) { if ( !mCanAddAttributes ) { throw new IllegalStateException( "Attempt to call addAttribute after createChildNode or done" ); } pAttributeName = validateName( "AttributeName", pAttributeName ); mSimpleLine.setAttribute( pAttributeName, pValue ); } private void flushLine() { if ( mSimpleLine != null ) { mSimpleLine.toString( mDataSink ); mDataSink.append( '\n' ); mSimpleLine = null; } } public Node createChildNode( String pName ) { if ( !mCanAddChildren ) { throw new IllegalStateException( "Attempt to call createChildNode after done" ); } Node rv = new SinkNode( mDepth + 1, pName ); mCanAddAttributes = false; flushLine(); return rv; } public void done() { mCanAddAttributes = mCanAddChildren = false; if ( mDoneCalled ) { throw new IllegalStateException( "Attempt to call done multiple times" ); } mDoneCalled = true; flushLine(); } } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/hierarchicaldata/HierarchicalDataSimpleFactory.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
811 Diff | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 | |
803 Diff | GeorgeS | Wed 15 Aug, 2012 04:08:34 +0000 | |
772 Diff | GeorgeS | Sun 15 Jul, 2012 16:55:51 +0000 | ! |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |