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
.getTrans().getCharacter(
46 StringId
.OPEN_SINGLE_QUOTE
);
47 private char closeQuote
= Instance
.getTrans().getCharacter(
48 StringId
.CLOSE_SINGLE_QUOTE
);
49 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
50 StringId
.OPEN_DOUBLE_QUOTE
);
51 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
52 StringId
.CLOSE_DOUBLE_QUOTE
);
54 // New methods not used in Deprecated mode
56 protected String
getDesc() throws IOException
{
57 throw new RuntimeException("should not be used by legacy code");
61 protected MetaData
getMeta() throws IOException
{
62 throw new RuntimeException("should not be used by legacy code");
66 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
)
68 throw new RuntimeException("should not be used by legacy code");
72 protected String
getChapterContent(URL chapUrl
, int number
, Progress pg
)
74 throw new RuntimeException("should not be used by legacy code");
78 public Story
process(Progress pg
) throws IOException
{
79 return process(getSource(), pg
);
85 * Return the {@link MetaData} of this story.
88 * the source of the story
90 * the input (the main resource)
92 * @return the associated {@link MetaData}, never NULL
95 * in case of I/O error
97 protected abstract MetaData
getMeta(URL source
, InputStream in
)
101 * Return the story description.
104 * the source of the story
106 * the input (the main resource)
108 * @return the description
110 * @throws IOException
111 * in case of I/O error
113 protected abstract String
getDesc(URL source
, InputStream in
)
117 * Return the list of chapters (name and resource).
120 * the source of the story
122 * the input (the main resource)
124 * the optional progress reporter
126 * @return the chapters
128 * @throws IOException
129 * in case of I/O error
131 protected abstract List
<Entry
<String
, URL
>> getChapters(URL source
,
132 InputStream in
, Progress pg
) throws IOException
;
135 * Return the content of the chapter (possibly HTML encoded, if
136 * {@link BasicSupport_Deprecated#isHtml()} is TRUE).
139 * the source of the story
141 * the input (the main resource)
145 * the optional progress reporter
147 * @return the content
149 * @throws IOException
150 * in case of I/O error
152 protected abstract String
getChapterContent(URL source
, InputStream in
,
153 int number
, Progress pg
) throws IOException
;
156 * Process the given story resource into a partially filled {@link Story}
157 * object containing the name and metadata, except for the description.
162 * @return the {@link Story}
164 * @throws IOException
165 * in case of I/O error
167 public Story
processMeta(URL url
) throws IOException
{
168 return processMeta(url
, true, false, null);
172 * Process the given story resource into a partially filled {@link Story}
173 * object containing the name and metadata.
178 * close "this" and "in" when done
180 * retrieve the description of the story, or not
182 * the optional progress reporter
184 * @return the {@link Story}, never NULL
186 * @throws IOException
187 * in case of I/O error
189 protected Story
processMeta(URL url
, boolean close
, boolean getDesc
,
190 Progress pg
) throws IOException
{
194 pg
.setMinMax(0, 100);
200 url
= getCanonicalUrl(url
);
202 setCurrentReferer(url
);
204 in
= openInput(url
); // NULL allowed here
206 preprocess(url
, getInput());
209 Story story
= new Story();
210 MetaData meta
= getMeta(url
, getInput());
211 if (meta
.getCreationDate() == null
212 || meta
.getCreationDate().isEmpty()) {
213 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
219 if (meta
.getCover() == null) {
220 meta
.setCover(getDefaultCover(meta
.getSubject()));
226 String descChapterName
= Instance
.getTrans().getString(
227 StringId
.DESCRIPTION
);
228 story
.getMeta().setResume(
229 makeChapter(url
, 0, descChapterName
,
230 getDesc(url
, getInput()), null));
247 * Process the given story resource into a fully filled {@link Story}
253 * the optional progress reporter
255 * @return the {@link Story}, never NULL
257 * @throws IOException
258 * in case of I/O error
260 protected Story
process(URL url
, Progress pg
) throws IOException
{
264 pg
.setMinMax(0, 100);
267 url
= getCanonicalUrl(url
);
270 Progress pgMeta
= new Progress();
271 pg
.addProgress(pgMeta
, 10);
272 Story story
= processMeta(url
, false, true, pgMeta
);
273 if (!pgMeta
.isDone()) {
274 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
277 pg
.setName("Retrieving " + story
.getMeta().getTitle());
279 setCurrentReferer(url
);
281 Progress pgGetChapters
= new Progress();
282 pg
.addProgress(pgGetChapters
, 10);
283 story
.setChapters(new ArrayList
<Chapter
>());
284 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
286 if (!pgGetChapters
.isDone()) {
287 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
290 if (chapters
!= null) {
291 Progress pgChaps
= new Progress("Extracting chapters", 0,
292 chapters
.size() * 300);
293 pg
.addProgress(pgChaps
, 80);
297 for (Entry
<String
, URL
> chap
: chapters
) {
298 pgChaps
.setName("Extracting chapter " + i
);
299 InputStream chapIn
= null;
300 if (chap
.getValue() != null) {
301 setCurrentReferer(chap
.getValue());
302 chapIn
= Instance
.getCache().open(chap
.getValue(),
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
);
327 story
.getMeta().setWords(words
);
329 if (chapIn
!= null) {
337 pgChaps
.setName("Extracting chapters");
354 * Prepare the support if needed before processing.
357 * the source of the story
359 * the input (the main resource)
361 * @throws IOException
364 @SuppressWarnings("unused")
365 protected void preprocess(URL source
, InputStream in
) throws IOException
{
369 * Create a {@link Chapter} object from the given information, formatting
370 * the content as it should be.
373 * the source of the story
379 * the chapter content
381 * the optional progress reporter
383 * @return the {@link Chapter}
385 * @throws IOException
386 * in case of I/O error
388 protected Chapter
makeChapter(URL source
, int number
, String name
,
389 String content
, Progress pg
) throws IOException
{
390 // Chapter name: process it correctly, then remove the possible
391 // redundant "Chapter x: " in front of it, or "-" (as in
392 // "Chapter 5: - Fun!" after the ": " was automatically added)
393 String chapterName
= processPara(name
).getContent().trim();
394 for (String lang
: Instance
.getConfig().getString(Config
.CHAPTER
)
396 String chapterWord
= Instance
.getConfig().getStringX(
397 Config
.CHAPTER
, lang
);
398 if (chapterName
.startsWith(chapterWord
)) {
399 chapterName
= chapterName
.substring(chapterWord
.length())
405 if (chapterName
.startsWith(Integer
.toString(number
))) {
406 chapterName
= chapterName
.substring(
407 Integer
.toString(number
).length()).trim();
410 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
411 chapterName
= chapterName
.substring(1).trim();
415 Chapter chap
= new Chapter(number
, chapterName
);
417 if (content
!= null) {
418 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
420 for (Paragraph para
: paras
) {
421 words
+= para
.getWords();
423 chap
.setParagraphs(paras
);
424 chap
.setWords(words
);
432 * Convert the given content into {@link Paragraph}s.
435 * the source URL of the story
437 * the textual content
439 * the optional progress reporter
441 * @return the {@link Paragraph}s
443 * @throws IOException
444 * in case of I/O error
446 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
447 Progress pg
) throws IOException
{
453 // Special <HR> processing:
454 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
458 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
460 if (content
!= null && !content
.trim().isEmpty()) {
462 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
463 pg
.setMinMax(0, tab
.length
);
465 for (String line
: tab
) {
466 if (line
.startsWith("[") && line
.endsWith("]")) {
467 pg
.setName("Extracting image " + i
);
469 paras
.add(makeParagraph(source
, line
.trim()));
474 List
<String
> lines
= new ArrayList
<String
>();
475 BufferedReader buff
= null;
477 buff
= new BufferedReader(
478 new InputStreamReader(new ByteArrayInputStream(
479 content
.getBytes("UTF-8")), "UTF-8"));
480 for (String line
= buff
.readLine(); line
!= null; line
= buff
482 lines
.add(line
.trim());
490 pg
.setMinMax(0, lines
.size());
492 for (String line
: lines
) {
493 if (line
.startsWith("[") && line
.endsWith("]")) {
494 pg
.setName("Extracting image " + i
);
496 paras
.add(makeParagraph(source
, line
));
502 // Check quotes for "bad" format
503 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
504 for (Paragraph para
: paras
) {
505 newParas
.addAll(requotify(para
));
509 // Remove double blanks/brks
510 fixBlanksBreaks(paras
);
517 * Convert the given line into a single {@link Paragraph}.
520 * the source URL of the story
522 * the textual content of the paragraph
524 * @return the {@link Paragraph}
526 private Paragraph
makeParagraph(URL source
, String line
) {
528 if (line
.startsWith("[") && line
.endsWith("]")) {
529 image
= getImage(this, source
, line
.substring(1, line
.length() - 1)
534 return new Paragraph(image
);
537 return processPara(line
);
541 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
542 * those {@link Paragraph}s.
544 * The resulting list will not contain a starting or trailing blank/break
545 * nor 2 blanks or breaks following each other.
548 * the list of {@link Paragraph}s to fix
550 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
551 boolean space
= false;
553 for (int i
= 0; i
< paras
.size(); i
++) {
554 Paragraph para
= paras
.get(i
);
555 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
556 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
558 if (i
> 0 && space
&& thisBrk
) {
561 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
570 // Remove blank/brk at start
572 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
573 0).getType() == ParagraphType
.BREAK
)) {
577 // Remove blank/brk at end
578 int last
= paras
.size() - 1;
580 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
581 .get(last
).getType() == ParagraphType
.BREAK
)) {
587 * Get the default cover related to this subject (see <tt>.info</tt> files).
592 * @return the cover if any, or NULL
594 static Image
getDefaultCover(String subject
) {
595 if (subject
!= null && !subject
.isEmpty()
596 && Instance
.getCoverDir() != null) {
598 File fileCover
= new File(Instance
.getCoverDir(), subject
);
599 return getImage(null, fileCover
.toURI().toURL(), subject
);
600 } catch (MalformedURLException e
) {
608 * Return the list of supported image extensions.
610 * @param emptyAllowed
611 * TRUE to allow an empty extension on first place, which can be
612 * used when you may already have an extension in your input but
613 * are not sure about it
615 * @return the extensions
617 static String
[] getImageExt(boolean emptyAllowed
) {
619 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
622 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
626 * Check if the given resource can be a local image or a remote image, then
627 * refresh the cache with it if it is.
632 * the resource to check
634 * @return the image if found, or NULL
637 static Image
getImage(BasicSupport_Deprecated support
, URL source
,
639 URL url
= getImageUrl(support
, source
, line
);
641 if ("file".equals(url
.getProtocol())) {
642 if (new File(url
.getPath()).isDirectory()) {
646 InputStream in
= null;
648 in
= Instance
.getCache().open(url
, getSupport(url
), true);
649 return new Image(in
);
650 } catch (IOException e
) {
655 } catch (IOException e
) {
665 * Check if the given resource can be a local image or a remote image, then
666 * refresh the cache with it if it is.
671 * the resource to check
673 * @return the image URL if found, or NULL
676 static URL
getImageUrl(BasicSupport_Deprecated support
, URL source
,
682 if (source
!= null) {
684 String relPath
= null;
685 String absPath
= null;
687 String path
= new File(source
.getFile()).getParent();
688 relPath
= new File(new File(path
), line
.trim())
690 } catch (Exception e
) {
691 // Cannot be converted to path (one possibility to take
692 // into account: absolute path on Windows)
695 absPath
= new File(line
.trim()).getAbsolutePath();
696 } catch (Exception e
) {
697 // Cannot be converted to path (at all)
700 for (String ext
: getImageExt(true)) {
701 File absFile
= new File(absPath
+ ext
);
702 File relFile
= new File(relPath
+ ext
);
703 if (absPath
!= null && absFile
.exists()
704 && absFile
.isFile()) {
705 url
= absFile
.toURI().toURL();
706 } else if (relPath
!= null && relFile
.exists()
707 && relFile
.isFile()) {
708 url
= relFile
.toURI().toURL();
711 } catch (Exception e
) {
712 // Should not happen since we control the correct arguments
719 for (String ext
: getImageExt(true)) {
720 if (Instance
.getCache()
721 .check(new URL(line
+ ext
), true)) {
722 url
= new URL(line
+ ext
);
729 for (String ext
: getImageExt(true)) {
731 url
= new URL(line
+ ext
);
732 Instance
.getCache().refresh(url
, support
, true);
734 } catch (IOException e
) {
735 // no image with this ext
740 } catch (MalformedURLException e
) {
745 // refresh the cached file
748 Instance
.getCache().refresh(url
, support
, true);
749 } catch (IOException e
) {
750 // woops, broken image
760 * Open the input file that will be used through the support.
762 * Can return NULL, in which case you are supposed to work without an
763 * {@link InputStream}.
766 * the source {@link URL}
768 * @return the {@link InputStream}
770 * @throws IOException
771 * in case of I/O error
773 protected InputStream
openInput(URL source
) throws IOException
{
774 return Instance
.getCache().open(source
, this, false);
778 * Reset then return {@link BasicSupport_Deprecated#in}.
780 * @return {@link BasicSupport_Deprecated#in}
782 protected InputStream
getInput() {
787 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
788 * and requotify them (i.e., separate them into QUOTE paragraphs and other
789 * paragraphs (quotes or not)).
792 * the paragraph to requotify (not necessarily a quote)
794 * @return the correctly (or so we hope) quotified paragraphs
796 protected List
<Paragraph
> requotify(Paragraph para
) {
797 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
799 if (para
.getType() == ParagraphType
.QUOTE
800 && para
.getContent().length() > 2) {
801 String line
= para
.getContent();
802 boolean singleQ
= line
.startsWith("" + openQuote
);
803 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
805 // Do not try when more than one quote at a time
806 // (some stories are not easily readable if we do)
808 && line
.indexOf(closeQuote
, 1) < line
809 .lastIndexOf(closeQuote
)) {
814 && line
.indexOf(closeDoubleQuote
, 1) < line
815 .lastIndexOf(closeDoubleQuote
)) {
821 if (!singleQ
&& !doubleQ
) {
822 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
823 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
826 char open
= singleQ ? openQuote
: openDoubleQuote
;
827 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
830 boolean inQuote
= false;
832 for (char car
: line
.toCharArray()) {
835 } else if (car
== close
) {
837 } else if (car
== '.' && !inQuote
) {
845 String rest
= line
.substring(posDot
+ 1).trim();
846 line
= line
.substring(0, posDot
+ 1).trim();
848 for (char car
: line
.toCharArray()) {
853 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
854 if (!rest
.isEmpty()) {
855 newParas
.addAll(requotify(processPara(rest
)));
869 * Process a {@link Paragraph} from a raw line of text.
871 * Will also fix quotes and HTML encoding if needed.
876 * @return the processed {@link Paragraph}
878 protected Paragraph
processPara(String line
) {
879 line
= ifUnhtml(line
).trim();
881 boolean space
= true;
883 boolean quote
= false;
884 boolean tentativeCloseQuote
= false;
889 StringBuilder builder
= new StringBuilder();
890 for (char car
: line
.toCharArray()) {
893 // dash, ndash and mdash: - – —
894 // currently: always use mdash
895 builder
.append(dashCount
== 1 ?
'-' : '—');
900 if (tentativeCloseQuote
) {
901 tentativeCloseQuote
= false;
902 if (Character
.isLetterOrDigit(car
)) {
905 // handle double-single quotes as double quotes
907 builder
.append(closeDoubleQuote
);
911 builder
.append(closeQuote
);
916 case ' ': // note: unbreakable space
919 case '\n': // just in case
920 case '\r': // just in case
921 if (builder
.length() > 0
922 && builder
.charAt(builder
.length() - 1) != ' ') {
929 if (space
|| (brk
&& quote
)) {
931 // handle double-single quotes as double quotes
933 builder
.deleteCharAt(builder
.length() - 1);
934 builder
.append(openDoubleQuote
);
936 builder
.append(openQuote
);
938 } else if (prev
== ' ' || prev
== car
) {
939 // handle double-single quotes as double quotes
941 builder
.deleteCharAt(builder
.length() - 1);
942 builder
.append(openDoubleQuote
);
944 builder
.append(openQuote
);
947 // it is a quote ("I'm off") or a 'quote' ("This
948 // 'good' restaurant"...)
949 tentativeCloseQuote
= true;
954 if (space
|| (brk
&& quote
)) {
956 builder
.append(openDoubleQuote
);
957 } else if (prev
== ' ') {
958 builder
.append(openDoubleQuote
);
960 builder
.append(closeDoubleQuote
);
994 if (space
|| (brk
&& quote
)) {
996 builder
.append(openQuote
);
998 // handle double-single quotes as double quotes
1000 builder
.deleteCharAt(builder
.length() - 1);
1001 builder
.append(openDoubleQuote
);
1003 builder
.append(openQuote
);
1017 // handle double-single quotes as double quotes
1019 builder
.deleteCharAt(builder
.length() - 1);
1020 builder
.append(closeDoubleQuote
);
1022 builder
.append(closeQuote
);
1031 if (space
|| (brk
&& quote
)) {
1033 builder
.append(openDoubleQuote
);
1035 builder
.append(openDoubleQuote
);
1048 builder
.append(closeDoubleQuote
);
1054 builder
.append(car
);
1061 if (tentativeCloseQuote
) {
1062 tentativeCloseQuote
= false;
1063 builder
.append(closeQuote
);
1066 line
= builder
.toString().trim();
1068 ParagraphType type
= ParagraphType
.NORMAL
;
1070 type
= ParagraphType
.BLANK
;
1072 type
= ParagraphType
.BREAK
;
1074 type
= ParagraphType
.QUOTE
;
1077 return new Paragraph(type
, line
, words
);
1081 * Remove the HTML from the input <b>if</b>
1082 * {@link BasicSupport_Deprecated#isHtml()} is true.
1087 * @return the no html version if needed
1089 private String
ifUnhtml(String input
) {
1090 if (isHtml() && input
!= null) {
1091 return StringUtils
.unhtml(input
);
1098 * Reset the given {@link InputStream} and return it.
1101 * the {@link InputStream} to reset
1103 * @return the same {@link InputStream} after reset
1105 static protected InputStream
reset(InputStream in
) {
1110 } catch (IOException e
) {
1117 * Return the first line from the given input which correspond to the given
1123 * a string that must be found inside the target line (also
1124 * supports "^" at start to say "only if it starts with" the
1126 * @param relativeLine
1127 * the line to return based upon the target line position (-1 =
1128 * the line before, 0 = the target line...)
1130 * @return the line, or NULL if not found
1132 static protected String
getLine(InputStream in
, String needle
,
1134 return getLine(in
, needle
, relativeLine
, true);
1138 * Return a line from the given input which correspond to the given
1144 * a string that must be found inside the target line (also
1145 * supports "^" at start to say "only if it starts with" the
1147 * @param relativeLine
1148 * the line to return based upon the target line position (-1 =
1149 * the line before, 0 = the target line...)
1151 * takes the first result (as opposed to the last one, which will
1152 * also always spend the input)
1154 * @return the line, or NULL if not found
1156 static protected String
getLine(InputStream in
, String needle
,
1157 int relativeLine
, boolean first
) {
1162 List
<String
> lines
= new ArrayList
<String
>();
1163 @SuppressWarnings("resource")
1164 Scanner scan
= new Scanner(in
, "UTF-8");
1166 scan
.useDelimiter("\\n");
1167 while (scan
.hasNext()) {
1168 lines
.add(scan
.next());
1171 if (needle
.startsWith("^")) {
1172 if (lines
.get(lines
.size() - 1).startsWith(
1173 needle
.substring(1))) {
1174 index
= lines
.size() - 1;
1178 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1179 index
= lines
.size() - 1;
1184 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1185 rep
= lines
.get(index
+ relativeLine
);
1196 * Return the text between the key and the endKey (and optional subKey can
1197 * be passed, in this case we will look for the key first, then take the
1198 * text between the subKey and the endKey).
1200 * Will only match the first line with the given key if more than one are
1201 * possible. Which also means that if the subKey or endKey is not found on
1202 * that line, NULL will be returned.
1207 * the key to match (also supports "^" at start to say
1208 * "only if it starts with" the key)
1210 * the sub key or NULL if none
1212 * the end key or NULL for "up to the end"
1213 * @return the text or NULL if not found
1215 static protected String
getKeyLine(InputStream in
, String key
,
1216 String subKey
, String endKey
) {
1217 return getKeyText(getLine(in
, key
, 0), key
, subKey
, endKey
);
1221 * Return the text between the key and the endKey (and optional subKey can
1222 * be passed, in this case we will look for the key first, then take the
1223 * text between the subKey and the endKey).
1228 * the key to match (also supports "^" at start to say
1229 * "only if it starts with" the key)
1231 * the sub key or NULL if none
1233 * the end key or NULL for "up to the end"
1234 * @return the text or NULL if not found
1236 static protected String
getKeyText(String in
, String key
, String subKey
,
1238 String result
= null;
1241 if (line
!= null && line
.contains(key
)) {
1242 line
= line
.substring(line
.indexOf(key
) + key
.length());
1243 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1244 if (subKey
!= null) {
1245 line
= line
.substring(line
.indexOf(subKey
)
1248 if (endKey
== null || line
.contains(endKey
)) {
1249 if (endKey
!= null) {
1250 line
= line
.substring(0, line
.indexOf(endKey
));
1261 * Return the text between the key and the endKey (optional subKeys can be
1262 * passed, in this case we will look for the subKeys first, then take the
1263 * text between the key and the endKey).
1270 * the end key or NULL for "up to the end"
1272 * the sub-keys to find before checking for key/endKey
1274 * @return the text or NULL if not found
1276 static protected String
getKeyTextAfter(String in
, String key
,
1277 String endKey
, String
... afters
) {
1279 if (in
!= null && !in
.isEmpty()) {
1280 int pos
= indexOfAfter(in
, 0, afters
);
1285 in
= in
.substring(pos
);
1288 return getKeyText(in
, key
, null, endKey
);
1292 * Return the first index after all the given "afters" have been found in
1293 * the {@link String}, or -1 if it was not possible.
1298 * start at this position in the string
1300 * the sub-keys to find before checking for key/endKey
1302 * @return the text or NULL if not found
1304 static protected int indexOfAfter(String in
, int startAt
, String
... afters
) {
1306 if (in
!= null && !in
.isEmpty()) {
1308 if (afters
!= null) {
1309 for (int i
= 0; pos
>= 0 && i
< afters
.length
; i
++) {
1310 String subKey
= afters
[i
];
1311 if (!subKey
.isEmpty()) {
1312 pos
= in
.indexOf(subKey
, pos
);
1314 pos
+= subKey
.length();