1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayInputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
8 import java
.io
.InputStreamReader
;
9 import java
.net
.MalformedURLException
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Date
;
13 import java
.util
.List
;
14 import java
.util
.Map
.Entry
;
15 import java
.util
.Scanner
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.bundles
.Config
;
19 import be
.nikiroo
.fanfix
.bundles
.StringId
;
20 import be
.nikiroo
.fanfix
.data
.Chapter
;
21 import be
.nikiroo
.fanfix
.data
.MetaData
;
22 import be
.nikiroo
.fanfix
.data
.Paragraph
;
23 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
24 import be
.nikiroo
.fanfix
.data
.Story
;
25 import be
.nikiroo
.utils
.Image
;
26 import be
.nikiroo
.utils
.Progress
;
27 import be
.nikiroo
.utils
.StringUtils
;
30 * DEPRECATED: use the new Jsoup 'Node' system.
32 * This class is the base class used by the other support classes. It can be
33 * used outside of this package, and have static method that you can use to get
34 * access to the correct support class.
36 * It will be used with 'resources' (usually web pages or files).
41 public abstract class BasicSupport_Deprecated
extends BasicSupport
{
42 private InputStream in
;
45 private char openQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_SINGLE_QUOTE
);
46 private char closeQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_SINGLE_QUOTE
);
47 private char openDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
48 private char closeDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
50 // New methods not used in Deprecated mode
52 protected String
getDesc() throws IOException
{
53 throw new RuntimeException("should not be used by legacy code");
57 protected MetaData
getMeta() throws IOException
{
58 throw new RuntimeException("should not be used by legacy code");
62 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
)
64 throw new RuntimeException("should not be used by legacy code");
68 protected String
getChapterContent(URL chapUrl
, int number
, Progress pg
)
70 throw new RuntimeException("should not be used by legacy code");
74 public Story
process(Progress pg
) throws IOException
{
75 return process(getSource(), pg
);
81 * Return the {@link MetaData} of this story.
84 * the source of the story
86 * the input (the main resource)
88 * @return the associated {@link MetaData}, never NULL
91 * in case of I/O error
93 protected abstract MetaData
getMeta(URL source
, InputStream in
)
97 * Return the story description.
100 * the source of the story
102 * the input (the main resource)
104 * @return the description
106 * @throws IOException
107 * in case of I/O error
109 protected abstract String
getDesc(URL source
, InputStream in
)
113 * Return the list of chapters (name and resource).
116 * the source of the story
118 * the input (the main resource)
120 * the optional progress reporter
122 * @return the chapters
124 * @throws IOException
125 * in case of I/O error
127 protected abstract List
<Entry
<String
, URL
>> getChapters(URL source
,
128 InputStream in
, Progress pg
) throws IOException
;
131 * Return the content of the chapter (possibly HTML encoded, if
132 * {@link BasicSupport_Deprecated#isHtml()} is TRUE).
135 * the source of the story
137 * the input (the main resource)
141 * the optional progress reporter
143 * @return the content
145 * @throws IOException
146 * in case of I/O error
148 protected abstract String
getChapterContent(URL source
, InputStream in
,
149 int number
, Progress pg
) throws IOException
;
152 * Process the given story resource into a partially filled {@link Story}
153 * object containing the name and metadata, except for the description.
158 * @return the {@link Story}
160 * @throws IOException
161 * in case of I/O error
163 public Story
processMeta(URL url
) throws IOException
{
164 return processMeta(url
, true, false, null);
168 * Process the given story resource into a partially filled {@link Story}
169 * object containing the name and metadata.
174 * close "this" and "in" when done
176 * retrieve the description of the story, or not
178 * the optional progress reporter
180 * @return the {@link Story}, never NULL
182 * @throws IOException
183 * in case of I/O error
185 protected Story
processMeta(URL url
, boolean close
, boolean getDesc
,
186 Progress pg
) throws IOException
{
190 pg
.setMinMax(0, 100);
196 url
= getCanonicalUrl(url
);
198 setCurrentReferer(url
);
200 in
= openInput(url
); // NULL allowed here
202 preprocess(url
, getInput());
205 Story story
= new Story();
207 MetaData meta
= getMeta(url
, getInput());
208 meta
.setType(getType().toString());
209 meta
.setSource(getType().getSourceName());
210 if (meta
.getPublisher() == null) {
211 meta
.setPublisher(getType().getSourceName());
214 if (meta
.getCreationDate() == null
215 || meta
.getCreationDate().trim().isEmpty()) {
216 meta
.setCreationDate(bsHelper
.formatDate(
217 StringUtils
.fromTime(new Date().getTime())));
220 pg
.put("meta", meta
);
224 if (meta
.getCover() == null) {
225 meta
.setCover(getDefaultCover(meta
.getSubject()));
231 String descChapterName
= Instance
.getInstance().getTrans().getString(StringId
.DESCRIPTION
);
232 story
.getMeta().setResume(makeChapter(url
, 0, descChapterName
, getDesc(url
, getInput()), null));
249 * Process the given story resource into a fully filled {@link Story}
255 * the optional progress reporter
257 * @return the {@link Story}, never NULL
259 * @throws IOException
260 * in case of I/O error
262 protected Story
process(URL url
, Progress pg
) throws IOException
{
266 pg
.setMinMax(0, 100);
269 url
= getCanonicalUrl(url
);
272 Progress pgMeta
= new Progress();
273 pg
.addProgress(pgMeta
, 10);
274 Story story
= processMeta(url
, false, true, pgMeta
);
275 pg
.put("meta", story
.getMeta());
276 if (!pgMeta
.isDone()) {
277 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
280 setCurrentReferer(url
);
282 Progress pgGetChapters
= new Progress();
283 pg
.addProgress(pgGetChapters
, 10);
284 story
.setChapters(new ArrayList
<Chapter
>());
285 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
287 if (!pgGetChapters
.isDone()) {
288 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
291 if (chapters
!= null) {
292 Progress pgChaps
= new Progress("Extracting chapters", 0,
293 chapters
.size() * 300);
294 pg
.addProgress(pgChaps
, 80);
298 for (Entry
<String
, URL
> chap
: chapters
) {
299 pgChaps
.setName("Extracting chapter " + i
);
300 InputStream chapIn
= null;
301 if (chap
.getValue() != null) {
302 setCurrentReferer(chap
.getValue());
303 chapIn
= Instance
.getInstance().getCache().open(chap
.getValue(), this, false);
305 pgChaps
.setProgress(i
* 100);
307 Progress pgGetChapterContent
= new Progress();
308 Progress pgMakeChapter
= new Progress();
309 pgChaps
.addProgress(pgGetChapterContent
, 100);
310 pgChaps
.addProgress(pgMakeChapter
, 100);
312 String content
= getChapterContent(url
, chapIn
, i
,
313 pgGetChapterContent
);
314 if (!pgGetChapterContent
.isDone()) {
315 pgGetChapterContent
.setProgress(pgGetChapterContent
319 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
320 content
, pgMakeChapter
);
321 if (!pgMakeChapter
.isDone()) {
322 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
325 words
+= cc
.getWords();
326 story
.getChapters().add(cc
);
328 if (chapIn
!= null) {
336 story
.getMeta().setWords(words
);
338 pgChaps
.setName("Extracting chapters");
343 // Check for "no chapters" stories
344 if (story
.getChapters().isEmpty()
345 && story
.getMeta().getResume() != null
346 && !story
.getMeta().getResume().getParagraphs().isEmpty()) {
347 Chapter resume
= story
.getMeta().getResume();
350 story
.getChapters().add(resume
);
351 story
.getMeta().setWords(resume
.getWords());
353 String descChapterName
= Instance
.getInstance().getTrans()
354 .getString(StringId
.DESCRIPTION
);
355 resume
= new Chapter(0, descChapterName
);
356 story
.getMeta().setResume(resume
);
370 * Prepare the support if needed before processing.
373 * the source of the story
375 * the input (the main resource)
377 * @throws IOException
380 @SuppressWarnings("unused")
381 protected void preprocess(URL source
, InputStream in
) throws IOException
{
385 * Create a {@link Chapter} object from the given information, formatting
386 * the content as it should be.
389 * the source of the story
395 * the chapter content
397 * the optional progress reporter
399 * @return the {@link Chapter}, never NULL
401 * @throws IOException
402 * in case of I/O error
404 protected Chapter
makeChapter(URL source
, int number
, String name
,
405 String content
, Progress pg
) throws IOException
{
406 // Chapter name: process it correctly, then remove the possible
407 // redundant "Chapter x: " in front of it, or "-" (as in
408 // "Chapter 5: - Fun!" after the ": " was automatically added)
409 String chapterName
= processPara(name
).getContent().trim();
410 for (String lang
: Instance
.getInstance().getConfig().getList(Config
.CONF_CHAPTER
)) {
411 String chapterWord
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_CHAPTER
, lang
);
412 if (chapterName
.startsWith(chapterWord
)) {
413 chapterName
= chapterName
.substring(chapterWord
.length())
419 if (chapterName
.startsWith(Integer
.toString(number
))) {
420 chapterName
= chapterName
.substring(
421 Integer
.toString(number
).length()).trim();
424 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
425 chapterName
= chapterName
.substring(1).trim();
429 Chapter chap
= new Chapter(number
, chapterName
);
431 if (content
!= null) {
432 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
434 for (Paragraph para
: paras
) {
435 words
+= para
.getWords();
437 chap
.setParagraphs(paras
);
438 chap
.setWords(words
);
446 * Convert the given content into {@link Paragraph}s.
449 * the source URL of the story
451 * the textual content
453 * the optional progress reporter
455 * @return the {@link Paragraph}s (can be empty, but never NULL)
457 * @throws IOException
458 * in case of I/O error
460 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
461 Progress pg
) throws IOException
{
467 // Special <HR> processing:
468 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
472 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
473 if (content
!= null && !content
.trim().isEmpty()) {
475 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
476 pg
.setMinMax(0, tab
.length
);
478 for (String line
: tab
) {
479 if (line
.startsWith("[") && line
.endsWith("]")) {
480 pg
.setName("Extracting image " + i
);
482 paras
.add(makeParagraph(source
, line
.trim()));
487 List
<String
> lines
= new ArrayList
<String
>();
488 BufferedReader buff
= null;
490 buff
= new BufferedReader(
491 new InputStreamReader(new ByteArrayInputStream(
492 content
.getBytes("UTF-8")), "UTF-8"));
493 for (String line
= buff
.readLine(); line
!= null; line
= buff
495 lines
.add(line
.trim());
503 pg
.setMinMax(0, lines
.size());
505 for (String line
: lines
) {
506 if (line
.startsWith("[") && line
.endsWith("]")) {
507 pg
.setName("Extracting image " + i
);
509 paras
.add(makeParagraph(source
, line
));
515 // Check quotes for "bad" format
516 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
517 for (Paragraph para
: paras
) {
518 newParas
.addAll(requotify(para
));
522 // Remove double blanks/brks
523 fixBlanksBreaks(paras
);
530 * Convert the given line into a single {@link Paragraph}.
533 * the source URL of the story
535 * the textual content of the paragraph
537 * @return the {@link Paragraph}, never NULL
539 private Paragraph
makeParagraph(URL source
, String line
) {
541 if (line
.startsWith("[") && line
.endsWith("]")) {
542 image
= getImage(this, source
, line
.substring(1, line
.length() - 1)
547 return new Paragraph(image
);
550 return processPara(line
);
554 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
555 * those {@link Paragraph}s.
557 * The resulting list will not contain a starting or trailing blank/break
558 * nor 2 blanks or breaks following each other.
561 * the list of {@link Paragraph}s to fix
563 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
564 boolean space
= false;
566 for (int i
= 0; i
< paras
.size(); i
++) {
567 Paragraph para
= paras
.get(i
);
568 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
569 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
571 if (i
> 0 && space
&& thisBrk
) {
574 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
583 // Remove blank/brk at start
585 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
586 0).getType() == ParagraphType
.BREAK
)) {
590 // Remove blank/brk at end
591 int last
= paras
.size() - 1;
593 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
594 .get(last
).getType() == ParagraphType
.BREAK
)) {
600 * Get the default cover related to this subject (see <tt>.info</tt> files).
605 * @return the cover if any, or NULL
607 static Image
getDefaultCover(String subject
) {
608 if (subject
!= null && !subject
.isEmpty() && Instance
.getInstance().getCoverDir() != null) {
610 File fileCover
= new File(Instance
.getInstance().getCoverDir(), subject
);
611 return getImage(null, fileCover
.toURI().toURL(), subject
);
612 } catch (MalformedURLException e
) {
620 * Return the list of supported image extensions.
622 * @param emptyAllowed
623 * TRUE to allow an empty extension on first place, which can be
624 * used when you may already have an extension in your input but
625 * are not sure about it
627 * @return the extensions
629 static String
[] getImageExt(boolean emptyAllowed
) {
631 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
634 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
638 * Check if the given resource can be a local image or a remote image, then
639 * refresh the cache with it if it is.
644 * the resource to check
646 * @return the image if found, or NULL
649 static Image
getImage(BasicSupport_Deprecated support
, URL source
,
651 URL url
= getImageUrl(support
, source
, line
);
653 if ("file".equals(url
.getProtocol())) {
654 if (new File(url
.getPath()).isDirectory()) {
658 InputStream in
= null;
660 in
= Instance
.getInstance().getCache().open(url
, getSupport(url
), true);
661 Image img
= new Image(in
);
662 if (img
.getSize() == 0) {
664 throw new IOException(
665 "Empty image not accepted");
668 } catch (IOException e
) {
673 } catch (IOException e
) {
683 * Check if the given resource can be a local image or a remote image, then
684 * refresh the cache with it if it is.
689 * the resource to check
691 * @return the image URL if found, or NULL
694 static URL
getImageUrl(BasicSupport_Deprecated support
, URL source
,
700 if (source
!= null) {
702 String relPath
= null;
703 String absPath
= null;
705 String path
= new File(source
.getFile()).getParent();
706 relPath
= new File(new File(path
), line
.trim())
708 } catch (Exception e
) {
709 // Cannot be converted to path (one possibility to take
710 // into account: absolute path on Windows)
713 absPath
= new File(line
.trim()).getAbsolutePath();
714 } catch (Exception e
) {
715 // Cannot be converted to path (at all)
718 for (String ext
: getImageExt(true)) {
719 File absFile
= new File(absPath
+ ext
);
720 File relFile
= new File(relPath
+ ext
);
721 if (absPath
!= null && absFile
.exists()
722 && absFile
.isFile()) {
723 url
= absFile
.toURI().toURL();
724 } else if (relPath
!= null && relFile
.exists()
725 && relFile
.isFile()) {
726 url
= relFile
.toURI().toURL();
729 } catch (Exception e
) {
730 // Should not happen since we control the correct arguments
737 for (String ext
: getImageExt(true)) {
738 if (Instance
.getInstance().getCache().check(new URL(line
+ ext
), true)) {
739 url
= new URL(line
+ ext
);
746 for (String ext
: getImageExt(true)) {
748 url
= new URL(line
+ ext
);
749 Instance
.getInstance().getCache().refresh(url
, support
, true);
751 } catch (IOException e
) {
752 // no image with this ext
757 } catch (MalformedURLException e
) {
762 // refresh the cached file
765 Instance
.getInstance().getCache().refresh(url
, support
, true);
766 } catch (IOException e
) {
767 // woops, broken image
777 * Open the input file that will be used through the support.
779 * Can return NULL, in which case you are supposed to work without an
780 * {@link InputStream}.
783 * the source {@link URL}
785 * @return the {@link InputStream}
787 * @throws IOException
788 * in case of I/O error
790 protected InputStream
openInput(URL source
) throws IOException
{
791 return Instance
.getInstance().getCache().open(source
, this, false);
795 * Reset then return {@link BasicSupport_Deprecated#in}.
797 * @return {@link BasicSupport_Deprecated#in}
799 protected InputStream
getInput() {
804 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
805 * and requotify them (i.e., separate them into QUOTE paragraphs and other
806 * paragraphs (quotes or not)).
809 * the paragraph to requotify (not necessarily a quote)
811 * @return the correctly (or so we hope) quotified paragraphs
813 protected List
<Paragraph
> requotify(Paragraph para
) {
814 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
816 if (para
.getType() == ParagraphType
.QUOTE
817 && para
.getContent().length() > 2) {
818 String line
= para
.getContent();
819 boolean singleQ
= line
.startsWith("" + openQuote
);
820 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
822 // Do not try when more than one quote at a time
823 // (some stories are not easily readable if we do)
825 && line
.indexOf(closeQuote
, 1) < line
826 .lastIndexOf(closeQuote
)) {
831 && line
.indexOf(closeDoubleQuote
, 1) < line
832 .lastIndexOf(closeDoubleQuote
)) {
838 if (!singleQ
&& !doubleQ
) {
839 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
840 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
843 char open
= singleQ ? openQuote
: openDoubleQuote
;
844 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
847 boolean inQuote
= false;
849 for (char car
: line
.toCharArray()) {
852 } else if (car
== close
) {
854 } else if (car
== '.' && !inQuote
) {
862 String rest
= line
.substring(posDot
+ 1).trim();
863 line
= line
.substring(0, posDot
+ 1).trim();
865 for (char car
: line
.toCharArray()) {
870 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
871 if (!rest
.isEmpty()) {
872 newParas
.addAll(requotify(processPara(rest
)));
886 * Process a {@link Paragraph} from a raw line of text.
888 * Will also fix quotes and HTML encoding if needed.
893 * @return the processed {@link Paragraph}, never NULL
895 protected Paragraph
processPara(String line
) {
896 line
= ifUnhtml(line
).trim();
898 boolean space
= true;
900 boolean quote
= false;
901 boolean tentativeCloseQuote
= false;
906 StringBuilder builder
= new StringBuilder();
907 for (char car
: line
.toCharArray()) {
910 // dash, ndash and mdash: - – —
911 // currently: always use mdash
912 builder
.append(dashCount
== 1 ?
'-' : '—');
917 if (tentativeCloseQuote
) {
918 tentativeCloseQuote
= false;
919 if (Character
.isLetterOrDigit(car
)) {
922 // handle double-single quotes as double quotes
924 builder
.append(closeDoubleQuote
);
928 builder
.append(closeQuote
);
933 case ' ': // note: unbreakable space
936 case '\n': // just in case
937 case '\r': // just in case
938 if (builder
.length() > 0
939 && builder
.charAt(builder
.length() - 1) != ' ') {
946 if (space
|| (brk
&& quote
)) {
948 // handle double-single quotes as double quotes
950 builder
.deleteCharAt(builder
.length() - 1);
951 builder
.append(openDoubleQuote
);
953 builder
.append(openQuote
);
955 } else if (prev
== ' ' || prev
== car
) {
956 // handle double-single quotes as double quotes
958 builder
.deleteCharAt(builder
.length() - 1);
959 builder
.append(openDoubleQuote
);
961 builder
.append(openQuote
);
964 // it is a quote ("I'm off") or a 'quote' ("This
965 // 'good' restaurant"...)
966 tentativeCloseQuote
= true;
971 if (space
|| (brk
&& quote
)) {
973 builder
.append(openDoubleQuote
);
974 } else if (prev
== ' ') {
975 builder
.append(openDoubleQuote
);
977 builder
.append(closeDoubleQuote
);
1002 builder
.append(car
);
1011 if (space
|| (brk
&& quote
)) {
1013 builder
.append(openQuote
);
1015 // handle double-single quotes as double quotes
1017 builder
.deleteCharAt(builder
.length() - 1);
1018 builder
.append(openDoubleQuote
);
1020 builder
.append(openQuote
);
1034 // handle double-single quotes as double quotes
1036 builder
.deleteCharAt(builder
.length() - 1);
1037 builder
.append(closeDoubleQuote
);
1039 builder
.append(closeQuote
);
1048 if (space
|| (brk
&& quote
)) {
1050 builder
.append(openDoubleQuote
);
1052 builder
.append(openDoubleQuote
);
1065 builder
.append(closeDoubleQuote
);
1071 builder
.append(car
);
1078 if (tentativeCloseQuote
) {
1079 tentativeCloseQuote
= false;
1080 builder
.append(closeQuote
);
1083 line
= builder
.toString().trim();
1085 ParagraphType type
= ParagraphType
.NORMAL
;
1087 type
= ParagraphType
.BLANK
;
1089 type
= ParagraphType
.BREAK
;
1091 type
= ParagraphType
.QUOTE
;
1094 return new Paragraph(type
, line
, words
);
1098 * Remove the HTML from the input <b>if</b>
1099 * {@link BasicSupport_Deprecated#isHtml()} is true.
1104 * @return the no html version if needed
1106 private String
ifUnhtml(String input
) {
1107 if (isHtml() && input
!= null) {
1108 return StringUtils
.unhtml(input
);
1115 * Reset the given {@link InputStream} and return it.
1118 * the {@link InputStream} to reset
1120 * @return the same {@link InputStream} after reset
1122 static protected InputStream
reset(InputStream in
) {
1127 } catch (IOException e
) {
1134 * Return the first line from the given input which correspond to the given
1140 * a string that must be found inside the target line (also
1141 * supports "^" at start to say "only if it starts with" the
1143 * @param relativeLine
1144 * the line to return based upon the target line position (-1 =
1145 * the line before, 0 = the target line...)
1147 * @return the line, or NULL if not found
1149 static protected String
getLine(InputStream in
, String needle
,
1151 return getLine(in
, needle
, relativeLine
, true);
1155 * Return a line from the given input which correspond to the given
1161 * a string that must be found inside the target line (also
1162 * supports "^" at start to say "only if it starts with" the
1164 * @param relativeLine
1165 * the line to return based upon the target line position (-1 =
1166 * the line before, 0 = the target line...)
1168 * takes the first result (as opposed to the last one, which will
1169 * also always spend the input)
1171 * @return the line, or NULL if not found
1173 static protected String
getLine(InputStream in
, String needle
,
1174 int relativeLine
, boolean first
) {
1179 List
<String
> lines
= new ArrayList
<String
>();
1180 @SuppressWarnings("resource")
1181 Scanner scan
= new Scanner(in
, "UTF-8");
1183 scan
.useDelimiter("\\n");
1184 while (scan
.hasNext()) {
1185 lines
.add(scan
.next());
1188 if (needle
.startsWith("^")) {
1189 if (lines
.get(lines
.size() - 1).startsWith(
1190 needle
.substring(1))) {
1191 index
= lines
.size() - 1;
1195 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1196 index
= lines
.size() - 1;
1201 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1202 rep
= lines
.get(index
+ relativeLine
);
1213 * Return the text between the key and the endKey (and optional subKey can
1214 * be passed, in this case we will look for the key first, then take the
1215 * text between the subKey and the endKey).
1217 * Will only match the first line with the given key if more than one are
1218 * possible. Which also means that if the subKey or endKey is not found on
1219 * that line, NULL will be returned.
1224 * the key to match (also supports "^" at start to say
1225 * "only if it starts with" the key)
1227 * the sub key or NULL if none
1229 * the end key or NULL for "up to the end"
1230 * @return the text or NULL if not found
1232 static protected String
getKeyLine(InputStream in
, String key
,
1233 String subKey
, String endKey
) {
1234 return getKeyText(getLine(in
, key
, 0), key
, subKey
, endKey
);
1238 * Return the text between the key and the endKey (and optional subKey can
1239 * be passed, in this case we will look for the key first, then take the
1240 * text between the subKey and the endKey).
1245 * the key to match (also supports "^" at start to say
1246 * "only if it starts with" the key)
1248 * the sub key or NULL if none
1250 * the end key or NULL for "up to the end"
1251 * @return the text or NULL if not found
1253 static protected String
getKeyText(String in
, String key
, String subKey
,
1255 String result
= null;
1258 if (line
!= null && line
.contains(key
)) {
1259 line
= line
.substring(line
.indexOf(key
) + key
.length());
1260 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1261 if (subKey
!= null) {
1262 line
= line
.substring(line
.indexOf(subKey
)
1265 if (endKey
== null || line
.contains(endKey
)) {
1266 if (endKey
!= null) {
1267 line
= line
.substring(0, line
.indexOf(endKey
));
1278 * Return the text between the key and the endKey (optional subKeys can be
1279 * passed, in this case we will look for the subKeys first, then take the
1280 * text between the key and the endKey).
1287 * the end key or NULL for "up to the end"
1289 * the sub-keys to find before checking for key/endKey
1291 * @return the text or NULL if not found
1293 static protected String
getKeyTextAfter(String in
, String key
,
1294 String endKey
, String
... afters
) {
1296 if (in
!= null && !in
.isEmpty()) {
1297 int pos
= indexOfAfter(in
, 0, afters
);
1302 in
= in
.substring(pos
);
1305 return getKeyText(in
, key
, null, endKey
);
1309 * Return the first index after all the given "afters" have been found in
1310 * the {@link String}, or -1 if it was not possible.
1315 * start at this position in the string
1317 * the sub-keys to find before checking for key/endKey
1319 * @return the text or NULL if not found
1321 static protected int indexOfAfter(String in
, int startAt
, String
... afters
) {
1323 if (in
!= null && !in
.isEmpty()) {
1325 if (afters
!= null) {
1326 for (int i
= 0; pos
>= 0 && i
< afters
.length
; i
++) {
1327 String subKey
= afters
[i
];
1328 if (!subKey
.isEmpty()) {
1329 pos
= in
.indexOf(subKey
, pos
);
1331 pos
+= subKey
.length();