Changeset 5193
- Timestamp:
- Nov 27, 2009, 2:06:17 PM (14 years ago)
- Location:
- trunk/src
- Files:
-
- 9 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/util/encode/TabCrLfEncoderDecoder.java
r5188 r5193 26 26 tab and backslash with \n, \r, \t and \\. This encoder is suitable for use with 27 27 tab-separated text files. 28 28 29 @author Nicklas 29 30 @since 2.15 … … 33 34 { 34 35 35 public TabCrLfEncoderDecoder() 36 {} 36 private final boolean nullIsEmptyString; 37 38 /** 39 Creates a new encoder/decoder. 40 @param nullIsEmptyString TRUE to encode null as empty string, 41 FALSE to not encode null. NOTE! If TRUE, this encoder is no 42 longer symmetrical 43 */ 44 public TabCrLfEncoderDecoder(boolean nullIsEmptyString) 45 { 46 this.nullIsEmptyString = nullIsEmptyString; 47 } 37 48 38 49 /* … … 43 54 public boolean isSymmetrical() 44 55 { 45 return true;56 return !nullIsEmptyString; 46 57 } 47 58 … … 50 61 { 51 62 if (s == null) return null; 63 if (s.length() == 0) return nullIsEmptyString ? null : s; 52 64 boolean neededDecode = false; 53 65 StringBuilder sb = new StringBuilder(s.length()); … … 97 109 public String encode(String s) 98 110 { 99 if (s == null) return null ;111 if (s == null) return nullIsEmptyString ? "" : null; 100 112 boolean neededEncode = false; 101 113 StringBuilder sb = new StringBuilder(s.length()); -
trunk/src/core/net/sf/basedb/util/export/TableWriter.java
r5188 r5193 169 169 first = false; 170 170 } 171 print ln();171 print("\n"); 172 172 } 173 173 -
trunk/src/core/net/sf/basedb/util/parser/FlatFileParser.java
r5014 r5193 181 181 182 182 /** 183 The regular expression for matching the beginning-of-file marker 184 */ 185 private Pattern bofMarker; 186 187 /** 183 188 The regular expression for matching a header line. 184 189 */ … … 263 268 264 269 /** 270 The value that was captured by the bofMarker pattern. 271 */ 272 private String bofType; 273 274 /** 265 275 List of lines parsed by the {@link #parseHeaders()} method. 266 276 */ … … 335 345 } 336 346 347 /** 348 Set a regular expression that maches a beginning-of-file 349 marker. This property should be set before starting to parse 350 the file (otherwise it is ignored). The first method call that 351 causes the parsing to be started will invoke {@link #parseToBof()} 352 (can also be invoked manually). 353 <p> 354 The regular expression may contain a single capturing group. The 355 matched value is returned by {@link #getBofType()}. 356 357 @param regexp A regular expression 358 @since 2.15 359 */ 360 public void setBofMarkerRegexp(Pattern regexp) 361 { 362 this.bofMarker = regexp; 363 } 364 337 365 /** 338 366 Set a regular expression that can be matched against a header. … … 537 565 } 538 566 567 /** 568 Parse the file until the beginning-of-file marker is found. If no 569 regular expression has been set with {@link #setBofMarkerRegexp(Pattern)} 570 or if the parsing of the file has already started, this method call is 571 ignored. 572 573 @return TRUE if this call resulted in parsing and the BOF marker was found, 574 FALSE otherwise 575 @since 2.15 576 */ 577 public boolean parseToBof() 578 throws IOException 579 { 580 if (bofMarker == null || parsedLines > 0) return false; 581 582 boolean done = false; 583 boolean matched = false; 584 while (!done) 585 { 586 ThreadSignalHandler.checkInterrupted(); 587 String line = reader.readLine(); 588 parsedLines++; 589 if (line == null) 590 { 591 done = true; 592 } 593 else 594 { 595 parsedCharacters += line.length(); 596 Matcher m = bofMarker.matcher(line); 597 if (m.matches()) 598 { 599 bofType = m.groupCount() > 0 ? m.group(1) : m.group(); 600 matched = true; 601 done = true; 602 } 603 } 604 } 605 return matched; 606 } 607 608 /** 609 Get the value captured by the BOF marker regular expression. If 610 no capturing groups was specified in the pattern this value is the 611 string that matched the entire pattern. 612 613 @return The matched value, or null if BOF matching has not been done 614 @since 2.15 615 */ 616 public String getBofType() 617 { 618 return bofType; 619 } 620 539 621 /** 540 622 Start parsing the input stream. The parser will read a single … … 583 665 throws IOException 584 666 { 667 if (parsedLines == 0) parseToBof(); 585 668 lines = new ArrayList<Line>(maxUnknownLines); 586 669 headers = new HashMap<String,String>(); 587 if (dataSplitter == null) return LineType.UNKNOWN;588 670 589 671 nextData = null; … … 1031 1113 if (nextData == null && nextSection == null) 1032 1114 { 1115 if (parsedLines == 0) parseToBof(); 1033 1116 ignoredLines = 0; 1034 1117 unknownLines = 0; … … 1267 1350 if (nextSection == null) 1268 1351 { 1352 if (parsedLines == 0) parseToBof(); 1269 1353 boolean done = false; 1270 1354 while (!done) … … 1561 1645 nullIsNull && "NULL".equalsIgnoreCase(value) ? null : value; 1562 1646 } 1647 1648 /** 1649 The data line as an array of strings. 1650 @since 2.15 1651 */ 1652 public String[] data() 1653 { 1654 return result; 1655 } 1563 1656 } 1564 1657 -
trunk/src/test/TestAll.java
r5188 r5193 132 132 results.put("TestJep", TestJep.test_all()); 133 133 results.put("TestEncoderDecoder", TestEncoderDecoder.test_all()); 134 results.put("TestBfs", TestBfs.test_all()); 134 135 results.put("TestDbInfo", TestDbInfo.test_all()); 135 136 results.put("TestPresets", TestPresets.test_all()); -
trunk/src/test/TestEncoderDecoder.java
r5188 r5193 40 40 { 41 41 write("++Testing string encode/decode"); 42 EncoderDecoder tabCrLf = new TabCrLfEncoderDecoder( );42 EncoderDecoder tabCrLf = new TabCrLfEncoderDecoder(false); 43 43 test_code(tabCrLf, "nothingtocode", "nothingtocode"); 44 44 test_code(tabCrLf, "a\tb\nc\rd\\e", "a\\tb\\nc\\rd\\\\e");
Note: See TracChangeset
for help on using the changeset viewer.