fix Base64 but breaks compat
[nikiroo-utils.git] / src / be / nikiroo / utils / StringUtils.java
1 package be.nikiroo.utils;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.io.UnsupportedEncodingException;
9 import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11 import java.text.Normalizer;
12 import java.text.Normalizer.Form;
13 import java.text.ParseException;
14 import java.text.SimpleDateFormat;
15 import java.util.AbstractMap;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.regex.Pattern;
21 import java.util.zip.GZIPInputStream;
22 import java.util.zip.GZIPOutputStream;
23
24 import org.unbescape.html.HtmlEscape;
25 import org.unbescape.html.HtmlEscapeLevel;
26 import org.unbescape.html.HtmlEscapeType;
27
28 import be.nikiroo.utils.streams.Base64InputStream;
29
30 /**
31 * This class offer some utilities based around {@link String}s.
32 *
33 * @author niki
34 */
35 public class StringUtils {
36 /**
37 * This enum type will decide the alignment of a {@link String} when padding
38 * or justification is applied (if there is enough horizontal space for it
39 * to be aligned).
40 */
41 public enum Alignment {
42 /** Aligned at left. */
43 LEFT,
44 /** Centered. */
45 CENTER,
46 /** Aligned at right. */
47 RIGHT,
48 /** Full justified (to both left and right). */
49 JUSTIFY,
50
51 // Old Deprecated values:
52
53 /** DEPRECATED: please use LEFT. */
54 @Deprecated
55 Beginning,
56 /** DEPRECATED: please use CENTER. */
57 @Deprecated
58 Center,
59 /** DEPRECATED: please use RIGHT. */
60 @Deprecated
61 End;
62
63 /**
64 * Return the non-deprecated version of this enum if needed (or return
65 * self if not).
66 *
67 * @return the non-deprecated value
68 */
69 Alignment undeprecate() {
70 if (this == Beginning)
71 return LEFT;
72 if (this == Center)
73 return CENTER;
74 if (this == End)
75 return RIGHT;
76 return this;
77 }
78 }
79
80 static private Pattern marks = getMarks();
81
82 /**
83 * Fix the size of the given {@link String} either with space-padding or by
84 * shortening it.
85 *
86 * @param text
87 * the {@link String} to fix
88 * @param width
89 * the size of the resulting {@link String} or -1 for a noop
90 *
91 * @return the resulting {@link String} of size <i>size</i>
92 */
93 static public String padString(String text, int width) {
94 return padString(text, width, true, null);
95 }
96
97 /**
98 * Fix the size of the given {@link String} either with space-padding or by
99 * optionally shortening it.
100 *
101 * @param text
102 * the {@link String} to fix
103 * @param width
104 * the size of the resulting {@link String} if the text fits or
105 * if cut is TRUE or -1 for a noop
106 * @param cut
107 * cut the {@link String} shorter if needed
108 * @param align
109 * align the {@link String} in this position if we have enough
110 * space (default is Alignment.Beginning)
111 *
112 * @return the resulting {@link String} of size <i>size</i> minimum
113 */
114 static public String padString(String text, int width, boolean cut,
115 Alignment align) {
116
117 if (align == null) {
118 align = Alignment.LEFT;
119 }
120
121 align = align.undeprecate();
122
123 if (width >= 0) {
124 if (text == null)
125 text = "";
126
127 int diff = width - text.length();
128
129 if (diff < 0) {
130 if (cut)
131 text = text.substring(0, width);
132 } else if (diff > 0) {
133 if (diff < 2 && align != Alignment.RIGHT)
134 align = Alignment.LEFT;
135
136 switch (align) {
137 case RIGHT:
138 text = new String(new char[diff]).replace('\0', ' ') + text;
139 break;
140 case CENTER:
141 int pad1 = (diff) / 2;
142 int pad2 = (diff + 1) / 2;
143 text = new String(new char[pad1]).replace('\0', ' ') + text
144 + new String(new char[pad2]).replace('\0', ' ');
145 break;
146 case LEFT:
147 default:
148 text = text + new String(new char[diff]).replace('\0', ' ');
149 break;
150 }
151 }
152 }
153
154 return text;
155 }
156
157 /**
158 * Justify a text into width-sized (at the maximum) lines.
159 *
160 * @param text
161 * the {@link String} to justify
162 * @param width
163 * the maximum size of the resulting lines
164 *
165 * @return a list of justified text lines
166 */
167 static public List<String> justifyText(String text, int width) {
168 return justifyText(text, width, null);
169 }
170
171 /**
172 * Justify a text into width-sized (at the maximum) lines.
173 *
174 * @param text
175 * the {@link String} to justify
176 * @param width
177 * the maximum size of the resulting lines
178 * @param align
179 * align the lines in this position (default is
180 * Alignment.Beginning)
181 *
182 * @return a list of justified text lines
183 */
184 static public List<String> justifyText(String text, int width,
185 Alignment align) {
186 if (align == null) {
187 align = Alignment.LEFT;
188 }
189
190 align = align.undeprecate();
191
192 switch (align) {
193 case CENTER:
194 return StringJustifier.center(text, width);
195 case RIGHT:
196 return StringJustifier.right(text, width);
197 case JUSTIFY:
198 return StringJustifier.full(text, width);
199 case LEFT:
200 default:
201 return StringJustifier.left(text, width);
202 }
203 }
204
205 /**
206 * Justify a text into width-sized (at the maximum) lines.
207 *
208 * @param text
209 * the {@link String} to justify
210 * @param width
211 * the maximum size of the resulting lines
212 *
213 * @return a list of justified text lines
214 */
215 static public List<String> justifyText(List<String> text, int width) {
216 return justifyText(text, width, null);
217 }
218
219 /**
220 * Justify a text into width-sized (at the maximum) lines.
221 *
222 * @param text
223 * the {@link String} to justify
224 * @param width
225 * the maximum size of the resulting lines
226 * @param align
227 * align the lines in this position (default is
228 * Alignment.Beginning)
229 *
230 * @return a list of justified text lines
231 */
232 static public List<String> justifyText(List<String> text, int width,
233 Alignment align) {
234 List<String> result = new ArrayList<String>();
235
236 // Content <-> Bullet spacing (null = no spacing)
237 List<Entry<String, String>> lines = new ArrayList<Entry<String, String>>();
238 StringBuilder previous = null;
239 StringBuilder tmp = new StringBuilder();
240 String previousItemBulletSpacing = null;
241 String itemBulletSpacing = null;
242 for (String inputLine : text) {
243 boolean previousLineComplete = true;
244
245 String current = inputLine.replace("\t", " ");
246 itemBulletSpacing = getItemSpacing(current);
247 boolean bullet = isItemLine(current);
248 if ((previousItemBulletSpacing == null || itemBulletSpacing
249 .length() <= previousItemBulletSpacing.length()) && !bullet) {
250 itemBulletSpacing = null;
251 }
252
253 if (itemBulletSpacing != null) {
254 current = current.trim();
255 if (!current.isEmpty() && bullet) {
256 current = current.substring(1);
257 }
258 current = current.trim();
259 previousLineComplete = bullet;
260 } else {
261 tmp.setLength(0);
262 for (String word : current.split(" ")) {
263 if (word.isEmpty()) {
264 continue;
265 }
266
267 if (tmp.length() > 0) {
268 tmp.append(' ');
269 }
270 tmp.append(word.trim());
271 }
272 current = tmp.toString();
273
274 previousLineComplete = current.isEmpty()
275 || previousItemBulletSpacing != null
276 || (previous != null && isFullLine(previous))
277 || isHrLine(current) || isHrLine(previous);
278 }
279
280 if (previous == null) {
281 previous = new StringBuilder();
282 } else {
283 if (previousLineComplete) {
284 lines.add(new AbstractMap.SimpleEntry<String, String>(
285 previous.toString(), previousItemBulletSpacing));
286 previous.setLength(0);
287 previousItemBulletSpacing = itemBulletSpacing;
288 } else {
289 previous.append(' ');
290 }
291 }
292
293 previous.append(current);
294
295 }
296
297 if (previous != null) {
298 lines.add(new AbstractMap.SimpleEntry<String, String>(previous
299 .toString(), previousItemBulletSpacing));
300 }
301
302 for (Entry<String, String> line : lines) {
303 String content = line.getKey();
304 String spacing = line.getValue();
305
306 String bullet = "- ";
307 if (spacing == null) {
308 bullet = "";
309 spacing = "";
310 }
311
312 if (spacing.length() > width + 3) {
313 spacing = "";
314 }
315
316 for (String subline : StringUtils.justifyText(content, width
317 - (spacing.length() + bullet.length()), align)) {
318 result.add(spacing + bullet + subline);
319 if (!bullet.isEmpty()) {
320 bullet = " ";
321 }
322 }
323 }
324
325 return result;
326 }
327
328 /**
329 * Sanitise the given input to make it more Terminal-friendly by removing
330 * combining characters.
331 *
332 * @param input
333 * the input to sanitise
334 * @param allowUnicode
335 * allow Unicode or only allow ASCII Latin characters
336 *
337 * @return the sanitised {@link String}
338 */
339 static public String sanitize(String input, boolean allowUnicode) {
340 return sanitize(input, allowUnicode, !allowUnicode);
341 }
342
343 /**
344 * Sanitise the given input to make it more Terminal-friendly by removing
345 * combining characters.
346 *
347 * @param input
348 * the input to sanitise
349 * @param allowUnicode
350 * allow Unicode or only allow ASCII Latin characters
351 * @param removeAllAccents
352 * TRUE to replace all accentuated characters by their non
353 * accentuated counter-parts
354 *
355 * @return the sanitised {@link String}
356 */
357 static public String sanitize(String input, boolean allowUnicode,
358 boolean removeAllAccents) {
359
360 if (removeAllAccents) {
361 input = Normalizer.normalize(input, Form.NFKD);
362 if (marks != null) {
363 input = marks.matcher(input).replaceAll("");
364 }
365 }
366
367 input = Normalizer.normalize(input, Form.NFKC);
368
369 if (!allowUnicode) {
370 StringBuilder builder = new StringBuilder();
371 for (int index = 0; index < input.length(); index++) {
372 char car = input.charAt(index);
373 // displayable chars in ASCII are in the range 32<->255,
374 // except DEL (127)
375 if (car >= 32 && car <= 255 && car != 127) {
376 builder.append(car);
377 }
378 }
379 input = builder.toString();
380 }
381
382 return input;
383 }
384
385 /**
386 * Convert between the time in milliseconds to a {@link String} in a "fixed"
387 * way (to exchange data over the wire, for instance).
388 * <p>
389 * Precise to the second.
390 *
391 * @param time
392 * the specified number of milliseconds since the standard base
393 * time known as "the epoch", namely January 1, 1970, 00:00:00
394 * GMT
395 *
396 * @return the time as a {@link String}
397 */
398 static public String fromTime(long time) {
399 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
400 return sdf.format(new Date(time));
401 }
402
403 /**
404 * Convert between the time as a {@link String} to milliseconds in a "fixed"
405 * way (to exchange data over the wire, for instance).
406 * <p>
407 * Precise to the second.
408 *
409 * @param displayTime
410 * the time as a {@link String}
411 *
412 * @return the number of milliseconds since the standard base time known as
413 * "the epoch", namely January 1, 1970, 00:00:00 GMT, or -1 in case
414 * of error
415 *
416 * @throws ParseException
417 * in case of parse error
418 */
419 static public long toTime(String displayTime) throws ParseException {
420 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
421 return sdf.parse(displayTime).getTime();
422 }
423
424 /**
425 * Return a hash of the given {@link String}.
426 *
427 * @param input
428 * the input data
429 *
430 * @return the hash
431 */
432 static public String getMd5Hash(String input) {
433 try {
434 MessageDigest md = MessageDigest.getInstance("MD5");
435 md.update(input.getBytes("UTF-8"));
436 byte byteData[] = md.digest();
437
438 StringBuffer hexString = new StringBuffer();
439 for (int i = 0; i < byteData.length; i++) {
440 String hex = Integer.toHexString(0xff & byteData[i]);
441 if (hex.length() == 1)
442 hexString.append('0');
443 hexString.append(hex);
444 }
445
446 return hexString.toString();
447 } catch (NoSuchAlgorithmException e) {
448 return input;
449 } catch (UnsupportedEncodingException e) {
450 return input;
451 }
452 }
453
454 /**
455 * Remove the HTML content from the given input, and un-html-ize the rest.
456 *
457 * @param html
458 * the HTML-encoded content
459 *
460 * @return the HTML-free equivalent content
461 */
462 public static String unhtml(String html) {
463 StringBuilder builder = new StringBuilder();
464
465 int inTag = 0;
466 for (char car : html.toCharArray()) {
467 if (car == '<') {
468 inTag++;
469 } else if (car == '>') {
470 inTag--;
471 } else if (inTag <= 0) {
472 builder.append(car);
473 }
474 }
475
476 char nbsp = ' '; // non-breakable space (a special char)
477 char space = ' ';
478 return HtmlEscape.unescapeHtml(builder.toString()).replace(nbsp, space);
479 }
480
481 /**
482 * Escape the given {@link String} so it can be used in XML, as content.
483 *
484 * @param input
485 * the input {@link String}
486 *
487 * @return the escaped {@link String}
488 */
489 public static String xmlEscape(String input) {
490 if (input == null) {
491 return "";
492 }
493
494 return HtmlEscape.escapeHtml(input,
495 HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA,
496 HtmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT);
497 }
498
499 /**
500 * Escape the given {@link String} so it can be used in XML, as text content
501 * inside double-quotes.
502 *
503 * @param input
504 * the input {@link String}
505 *
506 * @return the escaped {@link String}
507 */
508 public static String xmlEscapeQuote(String input) {
509 if (input == null) {
510 return "";
511 }
512
513 return HtmlEscape.escapeHtml(input,
514 HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA,
515 HtmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT);
516 }
517
518 /**
519 * Zip the data and then encode it into Base64.
520 *
521 * @param data
522 * the data
523 *
524 * @return the Base64 zipped version
525 *
526 * @throws IOException
527 * in case of I/O error
528 */
529 public static String zip64s(String data) throws IOException {
530 try {
531 return zip64(data.getBytes("UTF-8"));
532 } catch (UnsupportedEncodingException e) {
533 // All conforming JVM are required to support UTF-8
534 e.printStackTrace();
535 return null;
536 }
537 }
538
539 /**
540 * Zip the data and then encode it into Base64.
541 *
542 * @param data
543 * the data
544 *
545 * @return the Base64 zipped version
546 *
547 * @throws IOException
548 * in case of I/O error
549 */
550 public static String zip64(byte[] data) throws IOException {
551 // 1. compress
552 ByteArrayOutputStream bout = new ByteArrayOutputStream();
553 try {
554 OutputStream out = new GZIPOutputStream(bout);
555 try {
556 out.write(data);
557 } finally {
558 out.close();
559 }
560 } finally {
561 data = bout.toByteArray();
562 bout.close();
563 }
564
565 // 2. base64
566 InputStream in = new ByteArrayInputStream(data);
567 try {
568 in = new Base64InputStream(in, true);
569 return new String(IOUtils.toByteArray(in), "UTF-8");
570 } finally {
571 in.close();
572 }
573 }
574
575 /**
576 * Unconvert from Base64 then unzip the content, which is assumed to be a
577 * String.
578 *
579 * @param data
580 * the data in Base64 format
581 *
582 * @return the raw data
583 *
584 * @throws IOException
585 * in case of I/O error
586 */
587 public static String unzip64s(String data) throws IOException {
588 return new String(unzip64(data), "UTF-8");
589 }
590
591 /**
592 * Unconvert from Base64 then unzip the content.
593 *
594 * @param data
595 * the data in Base64 format
596 *
597 * @return the raw data
598 *
599 * @throws IOException
600 * in case of I/O error
601 */
602 public static byte[] unzip64(String data) throws IOException {
603 InputStream in = new Base64InputStream(new ByteArrayInputStream(
604 data.getBytes("UTF-8")), false);
605 try {
606 in = new GZIPInputStream(in);
607 return IOUtils.toByteArray(in);
608 } finally {
609 in.close();
610 }
611 }
612
613 /**
614 * Convert the given data to Base64 format.
615 *
616 * @param data
617 * the data to convert
618 *
619 * @return the Base64 {@link String} representation of the data
620 *
621 * @throws IOException
622 * in case of I/O errors
623 */
624 public static String base64(String data) throws IOException {
625 return base64(data.getBytes("UTF-8"));
626 }
627
628 /**
629 * Convert the given data to Base64 format.
630 *
631 * @param data
632 * the data to convert
633 *
634 * @return the Base64 {@link String} representation of the data
635 *
636 * @throws IOException
637 * in case of I/O errors
638 */
639 public static String base64(byte[] data) throws IOException {
640 Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(
641 data), true);
642 try {
643 return new String(IOUtils.toByteArray(in), "UTF-8");
644 } finally {
645 in.close();
646 }
647 }
648
649 /**
650 * Unconvert the given data from Base64 format back to a raw array of bytes.
651 *
652 * @param data
653 * the data to unconvert
654 *
655 * @return the raw data represented by the given Base64 {@link String},
656 *
657 * @throws IOException
658 * in case of I/O errors
659 */
660 public static byte[] unbase64(String data) throws IOException {
661 Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(
662 data.getBytes("UTF-8")), false);
663 try {
664 return IOUtils.toByteArray(in);
665 } finally {
666 in.close();
667 }
668 }
669
670 /**
671 * Unonvert the given data from Base64 format back to a {@link String}.
672 *
673 * @param data
674 * the data to unconvert
675 *
676 * @return the {@link String} represented by the given Base64 {@link String}
677 *
678 * @throws IOException
679 * in case of I/O errors
680 */
681 public static String unbase64s(String data) throws IOException {
682 return new String(unbase64(data), "UTF-8");
683 }
684
685 /**
686 * Return a display {@link String} for the given value, which can be
687 * suffixed with "k" or "M" depending upon the number, if it is big enough.
688 * <p>
689 * <p>
690 * Examples:
691 * <ul>
692 * <li><tt>8 765</tt> becomes "8k"</li>
693 * <li><tt>998 765</tt> becomes "998k"</li>
694 * <li><tt>12 987 364</tt> becomes "12M"</li>
695 * <li><tt>5 534 333 221</tt> becomes "5G"</li>
696 * </ul>
697 *
698 * @param value
699 * the value to convert
700 *
701 * @return the display value
702 */
703 public static String formatNumber(long value) {
704 return formatNumber(value, 0);
705 }
706
707 /**
708 * Return a display {@link String} for the given value, which can be
709 * suffixed with "k" or "M" depending upon the number, if it is big enough.
710 * <p>
711 * Examples (assuming decimalPositions = 1):
712 * <ul>
713 * <li><tt>8 765</tt> becomes "8.7k"</li>
714 * <li><tt>998 765</tt> becomes "998.7k"</li>
715 * <li><tt>12 987 364</tt> becomes "12.9M"</li>
716 * <li><tt>5 534 333 221</tt> becomes "5.5G"</li>
717 * </ul>
718 *
719 * @param value
720 * the value to convert
721 * @param decimalPositions
722 * the number of decimal positions to keep
723 *
724 * @return the display value
725 */
726 public static String formatNumber(long value, int decimalPositions) {
727 long userValue = value;
728 String suffix = "";
729 long mult = 1;
730
731 if (value >= 1000000000l) {
732 mult = 1000000000l;
733 userValue = value / 1000000000l;
734 suffix = " G";
735 } else if (value >= 1000000l) {
736 mult = 1000000l;
737 userValue = value / 1000000l;
738 suffix = " M";
739 } else if (value >= 1000l) {
740 mult = 1000l;
741 userValue = value / 1000l;
742 suffix = " k";
743 }
744
745 String deci = "";
746 if (decimalPositions > 0) {
747 deci = Long.toString(value % mult);
748 int size = Long.toString(mult).length() - 1;
749 while (deci.length() < size) {
750 deci = "0" + deci;
751 }
752
753 deci = deci.substring(0, Math.min(decimalPositions, deci.length()));
754 while (deci.length() < decimalPositions) {
755 deci += "0";
756 }
757
758 deci = "." + deci;
759 }
760
761 return Long.toString(userValue) + deci + suffix;
762 }
763
764 /**
765 * The reverse operation to {@link StringUtils#formatNumber(long)}: it will
766 * read a "display" number that can contain a "M" or "k" suffix and return
767 * the full value.
768 * <p>
769 * Of course, the conversion to and from display form is lossy (example:
770 * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
771 *
772 * @param value
773 * the value in display form with possible "M" and "k" suffixes,
774 * can be NULL
775 *
776 * @return the value as a number, or 0 if not possible to convert
777 */
778 public static long toNumber(String value) {
779 return toNumber(value, 0l);
780 }
781
782 /**
783 * The reverse operation to {@link StringUtils#formatNumber(long)}: it will
784 * read a "display" number that can contain a "M" or "k" suffix and return
785 * the full value.
786 * <p>
787 * Of course, the conversion to and from display form is lossy (example:
788 * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
789 *
790 * @param value
791 * the value in display form with possible "M" and "k" suffixes,
792 * can be NULL
793 * @param def
794 * the default value if it is not possible to convert the given
795 * value to a number
796 *
797 * @return the value as a number, or 0 if not possible to convert
798 */
799 public static long toNumber(String value, long def) {
800 long count = def;
801 if (value != null) {
802 value = value.trim().toLowerCase();
803 try {
804 long mult = 1;
805 if (value.endsWith("g")) {
806 value = value.substring(0, value.length() - 1).trim();
807 mult = 1000000000;
808 } else if (value.endsWith("m")) {
809 value = value.substring(0, value.length() - 1).trim();
810 mult = 1000000;
811 } else if (value.endsWith("k")) {
812 value = value.substring(0, value.length() - 1).trim();
813 mult = 1000;
814 }
815
816 long deci = 0;
817 if (value.contains(".")) {
818 String[] tab = value.split("\\.");
819 if (tab.length != 2) {
820 throw new NumberFormatException(value);
821 }
822 double decimal = Double.parseDouble("0."
823 + tab[tab.length - 1]);
824 deci = ((long) (mult * decimal));
825 value = tab[0];
826 }
827 count = mult * Long.parseLong(value) + deci;
828 } catch (Exception e) {
829 }
830 }
831
832 return count;
833 }
834
835 /**
836 * The "remove accents" pattern.
837 *
838 * @return the pattern, or NULL if a problem happens
839 */
840 private static Pattern getMarks() {
841 try {
842 return Pattern
843 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
844 } catch (Exception e) {
845 // Can fail on Android...
846 return null;
847 }
848 }
849
850 //
851 // justify List<String> related:
852 //
853
854 /**
855 * Check if this line ends as a complete line (ends with a "." or similar).
856 * <p>
857 * Note that we consider an empty line as full, and a line ending with
858 * spaces as not complete.
859 *
860 * @param line
861 * the line to check
862 *
863 * @return TRUE if it does
864 */
865 static private boolean isFullLine(StringBuilder line) {
866 if (line.length() == 0) {
867 return true;
868 }
869
870 char lastCar = line.charAt(line.length() - 1);
871 switch (lastCar) {
872 case '.': // points
873 case '?':
874 case '!':
875
876 case '\'': // quotes
877 case '‘':
878 case '’':
879
880 case '"': // double quotes
881 case '”':
882 case '“':
883 case '»':
884 case '«':
885 return true;
886 default:
887 return false;
888 }
889 }
890
891 /**
892 * Check if this line represent an item in a list or description (i.e.,
893 * check that the first non-space char is "-").
894 *
895 * @param line
896 * the line to check
897 *
898 * @return TRUE if it is
899 */
900 static private boolean isItemLine(String line) {
901 String spacing = getItemSpacing(line);
902 return spacing != null && !spacing.isEmpty()
903 && line.charAt(spacing.length()) == '-';
904 }
905
906 /**
907 * Return all the spaces that start this line (or Empty if none).
908 *
909 * @param line
910 * the line to get the starting spaces from
911 *
912 * @return the left spacing
913 */
914 static private String getItemSpacing(String line) {
915 int i;
916 for (i = 0; i < line.length(); i++) {
917 if (line.charAt(i) != ' ') {
918 return line.substring(0, i);
919 }
920 }
921
922 return "";
923 }
924
925 /**
926 * This line is an horizontal spacer line.
927 *
928 * @param line
929 * the line to test
930 *
931 * @return TRUE if it is
932 */
933 static private boolean isHrLine(CharSequence line) {
934 int count = 0;
935 if (line != null) {
936 for (int i = 0; i < line.length(); i++) {
937 char car = line.charAt(i);
938 if (car == ' ' || car == '\t' || car == '*' || car == '-'
939 || car == '_' || car == '~' || car == '=' || car == '/'
940 || car == '\\') {
941 count++;
942 } else {
943 return false;
944 }
945 }
946 }
947
948 return count > 2;
949 }
950 }