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();
206 MetaData meta
= getMeta(url
, getInput());
207 if (meta
.getCreationDate() == null
208 || meta
.getCreationDate().isEmpty()) {
209 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
212 pg
.put("meta", meta
);
216 if (meta
.getCover() == null) {
217 meta
.setCover(getDefaultCover(meta
.getSubject()));
223 String descChapterName
= Instance
.getInstance().getTrans().getString(StringId
.DESCRIPTION
);
224 story
.getMeta().setResume(makeChapter(url
, 0, descChapterName
, getDesc(url
, getInput()), null));
241 * Process the given story resource into a fully filled {@link Story}
247 * the optional progress reporter
249 * @return the {@link Story}, never NULL
251 * @throws IOException
252 * in case of I/O error
254 protected Story
process(URL url
, Progress pg
) throws IOException
{
258 pg
.setMinMax(0, 100);
261 url
= getCanonicalUrl(url
);
264 Progress pgMeta
= new Progress();
265 pg
.addProgress(pgMeta
, 10);
266 Story story
= processMeta(url
, false, true, pgMeta
);
267 pg
.put("meta", story
.getMeta());
268 if (!pgMeta
.isDone()) {
269 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
272 pg
.setName("Retrieving " + story
.getMeta().getTitle());
274 setCurrentReferer(url
);
276 Progress pgGetChapters
= new Progress();
277 pg
.addProgress(pgGetChapters
, 10);
278 story
.setChapters(new ArrayList
<Chapter
>());
279 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
281 if (!pgGetChapters
.isDone()) {
282 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
285 if (chapters
!= null) {
286 Progress pgChaps
= new Progress("Extracting chapters", 0,
287 chapters
.size() * 300);
288 pg
.addProgress(pgChaps
, 80);
292 for (Entry
<String
, URL
> chap
: chapters
) {
293 pgChaps
.setName("Extracting chapter " + i
);
294 InputStream chapIn
= null;
295 if (chap
.getValue() != null) {
296 setCurrentReferer(chap
.getValue());
297 chapIn
= Instance
.getInstance().getCache().open(chap
.getValue(), this, false);
299 pgChaps
.setProgress(i
* 100);
301 Progress pgGetChapterContent
= new Progress();
302 Progress pgMakeChapter
= new Progress();
303 pgChaps
.addProgress(pgGetChapterContent
, 100);
304 pgChaps
.addProgress(pgMakeChapter
, 100);
306 String content
= getChapterContent(url
, chapIn
, i
,
307 pgGetChapterContent
);
308 if (!pgGetChapterContent
.isDone()) {
309 pgGetChapterContent
.setProgress(pgGetChapterContent
313 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
314 content
, pgMakeChapter
);
315 if (!pgMakeChapter
.isDone()) {
316 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
319 words
+= cc
.getWords();
320 story
.getChapters().add(cc
);
321 story
.getMeta().setWords(words
);
323 if (chapIn
!= null) {
331 pgChaps
.setName("Extracting chapters");
348 * Prepare the support if needed before processing.
351 * the source of the story
353 * the input (the main resource)
355 * @throws IOException
358 @SuppressWarnings("unused")
359 protected void preprocess(URL source
, InputStream in
) throws IOException
{
363 * Create a {@link Chapter} object from the given information, formatting
364 * the content as it should be.
367 * the source of the story
373 * the chapter content
375 * the optional progress reporter
377 * @return the {@link Chapter}
379 * @throws IOException
380 * in case of I/O error
382 protected Chapter
makeChapter(URL source
, int number
, String name
,
383 String content
, Progress pg
) throws IOException
{
384 // Chapter name: process it correctly, then remove the possible
385 // redundant "Chapter x: " in front of it, or "-" (as in
386 // "Chapter 5: - Fun!" after the ": " was automatically added)
387 String chapterName
= processPara(name
).getContent().trim();
388 for (String lang
: Instance
.getInstance().getConfig().getList(Config
.CONF_CHAPTER
)) {
389 String chapterWord
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_CHAPTER
, lang
);
390 if (chapterName
.startsWith(chapterWord
)) {
391 chapterName
= chapterName
.substring(chapterWord
.length())
397 if (chapterName
.startsWith(Integer
.toString(number
))) {
398 chapterName
= chapterName
.substring(
399 Integer
.toString(number
).length()).trim();
402 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
403 chapterName
= chapterName
.substring(1).trim();
407 Chapter chap
= new Chapter(number
, chapterName
);
409 if (content
!= null) {
410 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
412 for (Paragraph para
: paras
) {
413 words
+= para
.getWords();
415 chap
.setParagraphs(paras
);
416 chap
.setWords(words
);
424 * Convert the given content into {@link Paragraph}s.
427 * the source URL of the story
429 * the textual content
431 * the optional progress reporter
433 * @return the {@link Paragraph}s
435 * @throws IOException
436 * in case of I/O error
438 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
439 Progress pg
) throws IOException
{
445 // Special <HR> processing:
446 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
450 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
452 if (content
!= null && !content
.trim().isEmpty()) {
454 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
455 pg
.setMinMax(0, tab
.length
);
457 for (String line
: tab
) {
458 if (line
.startsWith("[") && line
.endsWith("]")) {
459 pg
.setName("Extracting image " + i
);
461 paras
.add(makeParagraph(source
, line
.trim()));
466 List
<String
> lines
= new ArrayList
<String
>();
467 BufferedReader buff
= null;
469 buff
= new BufferedReader(
470 new InputStreamReader(new ByteArrayInputStream(
471 content
.getBytes("UTF-8")), "UTF-8"));
472 for (String line
= buff
.readLine(); line
!= null; line
= buff
474 lines
.add(line
.trim());
482 pg
.setMinMax(0, lines
.size());
484 for (String line
: lines
) {
485 if (line
.startsWith("[") && line
.endsWith("]")) {
486 pg
.setName("Extracting image " + i
);
488 paras
.add(makeParagraph(source
, line
));
494 // Check quotes for "bad" format
495 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
496 for (Paragraph para
: paras
) {
497 newParas
.addAll(requotify(para
));
501 // Remove double blanks/brks
502 fixBlanksBreaks(paras
);
509 * Convert the given line into a single {@link Paragraph}.
512 * the source URL of the story
514 * the textual content of the paragraph
516 * @return the {@link Paragraph}
518 private Paragraph
makeParagraph(URL source
, String line
) {
520 if (line
.startsWith("[") && line
.endsWith("]")) {
521 image
= getImage(this, source
, line
.substring(1, line
.length() - 1)
526 return new Paragraph(image
);
529 return processPara(line
);
533 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
534 * those {@link Paragraph}s.
536 * The resulting list will not contain a starting or trailing blank/break
537 * nor 2 blanks or breaks following each other.
540 * the list of {@link Paragraph}s to fix
542 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
543 boolean space
= false;
545 for (int i
= 0; i
< paras
.size(); i
++) {
546 Paragraph para
= paras
.get(i
);
547 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
548 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
550 if (i
> 0 && space
&& thisBrk
) {
553 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
562 // Remove blank/brk at start
564 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
565 0).getType() == ParagraphType
.BREAK
)) {
569 // Remove blank/brk at end
570 int last
= paras
.size() - 1;
572 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
573 .get(last
).getType() == ParagraphType
.BREAK
)) {
579 * Get the default cover related to this subject (see <tt>.info</tt> files).
584 * @return the cover if any, or NULL
586 static Image
getDefaultCover(String subject
) {
587 if (subject
!= null && !subject
.isEmpty() && Instance
.getInstance().getCoverDir() != null) {
589 File fileCover
= new File(Instance
.getInstance().getCoverDir(), subject
);
590 return getImage(null, fileCover
.toURI().toURL(), subject
);
591 } catch (MalformedURLException e
) {
599 * Return the list of supported image extensions.
601 * @param emptyAllowed
602 * TRUE to allow an empty extension on first place, which can be
603 * used when you may already have an extension in your input but
604 * are not sure about it
606 * @return the extensions
608 static String
[] getImageExt(boolean emptyAllowed
) {
610 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
613 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
617 * Check if the given resource can be a local image or a remote image, then
618 * refresh the cache with it if it is.
623 * the resource to check
625 * @return the image if found, or NULL
628 static Image
getImage(BasicSupport_Deprecated support
, URL source
,
630 URL url
= getImageUrl(support
, source
, line
);
632 if ("file".equals(url
.getProtocol())) {
633 if (new File(url
.getPath()).isDirectory()) {
637 InputStream in
= null;
639 in
= Instance
.getInstance().getCache().open(url
, getSupport(url
), true);
640 return new Image(in
);
641 } catch (IOException e
) {
646 } catch (IOException e
) {
656 * Check if the given resource can be a local image or a remote image, then
657 * refresh the cache with it if it is.
662 * the resource to check
664 * @return the image URL if found, or NULL
667 static URL
getImageUrl(BasicSupport_Deprecated support
, URL source
,
673 if (source
!= null) {
675 String relPath
= null;
676 String absPath
= null;
678 String path
= new File(source
.getFile()).getParent();
679 relPath
= new File(new File(path
), line
.trim())
681 } catch (Exception e
) {
682 // Cannot be converted to path (one possibility to take
683 // into account: absolute path on Windows)
686 absPath
= new File(line
.trim()).getAbsolutePath();
687 } catch (Exception e
) {
688 // Cannot be converted to path (at all)
691 for (String ext
: getImageExt(true)) {
692 File absFile
= new File(absPath
+ ext
);
693 File relFile
= new File(relPath
+ ext
);
694 if (absPath
!= null && absFile
.exists()
695 && absFile
.isFile()) {
696 url
= absFile
.toURI().toURL();
697 } else if (relPath
!= null && relFile
.exists()
698 && relFile
.isFile()) {
699 url
= relFile
.toURI().toURL();
702 } catch (Exception e
) {
703 // Should not happen since we control the correct arguments
710 for (String ext
: getImageExt(true)) {
711 if (Instance
.getInstance().getCache().check(new URL(line
+ ext
), true)) {
712 url
= new URL(line
+ ext
);
719 for (String ext
: getImageExt(true)) {
721 url
= new URL(line
+ ext
);
722 Instance
.getInstance().getCache().refresh(url
, support
, true);
724 } catch (IOException e
) {
725 // no image with this ext
730 } catch (MalformedURLException e
) {
735 // refresh the cached file
738 Instance
.getInstance().getCache().refresh(url
, support
, true);
739 } catch (IOException e
) {
740 // woops, broken image
750 * Open the input file that will be used through the support.
752 * Can return NULL, in which case you are supposed to work without an
753 * {@link InputStream}.
756 * the source {@link URL}
758 * @return the {@link InputStream}
760 * @throws IOException
761 * in case of I/O error
763 protected InputStream
openInput(URL source
) throws IOException
{
764 return Instance
.getInstance().getCache().open(source
, this, false);
768 * Reset then return {@link BasicSupport_Deprecated#in}.
770 * @return {@link BasicSupport_Deprecated#in}
772 protected InputStream
getInput() {
777 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
778 * and requotify them (i.e., separate them into QUOTE paragraphs and other
779 * paragraphs (quotes or not)).
782 * the paragraph to requotify (not necessarily a quote)
784 * @return the correctly (or so we hope) quotified paragraphs
786 protected List
<Paragraph
> requotify(Paragraph para
) {
787 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
789 if (para
.getType() == ParagraphType
.QUOTE
790 && para
.getContent().length() > 2) {
791 String line
= para
.getContent();
792 boolean singleQ
= line
.startsWith("" + openQuote
);
793 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
795 // Do not try when more than one quote at a time
796 // (some stories are not easily readable if we do)
798 && line
.indexOf(closeQuote
, 1) < line
799 .lastIndexOf(closeQuote
)) {
804 && line
.indexOf(closeDoubleQuote
, 1) < line
805 .lastIndexOf(closeDoubleQuote
)) {
811 if (!singleQ
&& !doubleQ
) {
812 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
813 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
816 char open
= singleQ ? openQuote
: openDoubleQuote
;
817 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
820 boolean inQuote
= false;
822 for (char car
: line
.toCharArray()) {
825 } else if (car
== close
) {
827 } else if (car
== '.' && !inQuote
) {
835 String rest
= line
.substring(posDot
+ 1).trim();
836 line
= line
.substring(0, posDot
+ 1).trim();
838 for (char car
: line
.toCharArray()) {
843 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
844 if (!rest
.isEmpty()) {
845 newParas
.addAll(requotify(processPara(rest
)));
859 * Process a {@link Paragraph} from a raw line of text.
861 * Will also fix quotes and HTML encoding if needed.
866 * @return the processed {@link Paragraph}
868 protected Paragraph
processPara(String line
) {
869 line
= ifUnhtml(line
).trim();
871 boolean space
= true;
873 boolean quote
= false;
874 boolean tentativeCloseQuote
= false;
879 StringBuilder builder
= new StringBuilder();
880 for (char car
: line
.toCharArray()) {
883 // dash, ndash and mdash: - – —
884 // currently: always use mdash
885 builder
.append(dashCount
== 1 ?
'-' : '—');
890 if (tentativeCloseQuote
) {
891 tentativeCloseQuote
= false;
892 if (Character
.isLetterOrDigit(car
)) {
895 // handle double-single quotes as double quotes
897 builder
.append(closeDoubleQuote
);
901 builder
.append(closeQuote
);
906 case ' ': // note: unbreakable space
909 case '\n': // just in case
910 case '\r': // just in case
911 if (builder
.length() > 0
912 && builder
.charAt(builder
.length() - 1) != ' ') {
919 if (space
|| (brk
&& quote
)) {
921 // handle double-single quotes as double quotes
923 builder
.deleteCharAt(builder
.length() - 1);
924 builder
.append(openDoubleQuote
);
926 builder
.append(openQuote
);
928 } else if (prev
== ' ' || prev
== car
) {
929 // handle double-single quotes as double quotes
931 builder
.deleteCharAt(builder
.length() - 1);
932 builder
.append(openDoubleQuote
);
934 builder
.append(openQuote
);
937 // it is a quote ("I'm off") or a 'quote' ("This
938 // 'good' restaurant"...)
939 tentativeCloseQuote
= true;
944 if (space
|| (brk
&& quote
)) {
946 builder
.append(openDoubleQuote
);
947 } else if (prev
== ' ') {
948 builder
.append(openDoubleQuote
);
950 builder
.append(closeDoubleQuote
);
984 if (space
|| (brk
&& quote
)) {
986 builder
.append(openQuote
);
988 // handle double-single quotes as double quotes
990 builder
.deleteCharAt(builder
.length() - 1);
991 builder
.append(openDoubleQuote
);
993 builder
.append(openQuote
);
1007 // handle double-single quotes as double quotes
1009 builder
.deleteCharAt(builder
.length() - 1);
1010 builder
.append(closeDoubleQuote
);
1012 builder
.append(closeQuote
);
1021 if (space
|| (brk
&& quote
)) {
1023 builder
.append(openDoubleQuote
);
1025 builder
.append(openDoubleQuote
);
1038 builder
.append(closeDoubleQuote
);
1044 builder
.append(car
);
1051 if (tentativeCloseQuote
) {
1052 tentativeCloseQuote
= false;
1053 builder
.append(closeQuote
);
1056 line
= builder
.toString().trim();
1058 ParagraphType type
= ParagraphType
.NORMAL
;
1060 type
= ParagraphType
.BLANK
;
1062 type
= ParagraphType
.BREAK
;
1064 type
= ParagraphType
.QUOTE
;
1067 return new Paragraph(type
, line
, words
);
1071 * Remove the HTML from the input <b>if</b>
1072 * {@link BasicSupport_Deprecated#isHtml()} is true.
1077 * @return the no html version if needed
1079 private String
ifUnhtml(String input
) {
1080 if (isHtml() && input
!= null) {
1081 return StringUtils
.unhtml(input
);
1088 * Reset the given {@link InputStream} and return it.
1091 * the {@link InputStream} to reset
1093 * @return the same {@link InputStream} after reset
1095 static protected InputStream
reset(InputStream in
) {
1100 } catch (IOException e
) {
1107 * Return the first line from the given input which correspond to the given
1113 * a string that must be found inside the target line (also
1114 * supports "^" at start to say "only if it starts with" the
1116 * @param relativeLine
1117 * the line to return based upon the target line position (-1 =
1118 * the line before, 0 = the target line...)
1120 * @return the line, or NULL if not found
1122 static protected String
getLine(InputStream in
, String needle
,
1124 return getLine(in
, needle
, relativeLine
, true);
1128 * Return a line from the given input which correspond to the given
1134 * a string that must be found inside the target line (also
1135 * supports "^" at start to say "only if it starts with" the
1137 * @param relativeLine
1138 * the line to return based upon the target line position (-1 =
1139 * the line before, 0 = the target line...)
1141 * takes the first result (as opposed to the last one, which will
1142 * also always spend the input)
1144 * @return the line, or NULL if not found
1146 static protected String
getLine(InputStream in
, String needle
,
1147 int relativeLine
, boolean first
) {
1152 List
<String
> lines
= new ArrayList
<String
>();
1153 @SuppressWarnings("resource")
1154 Scanner scan
= new Scanner(in
, "UTF-8");
1156 scan
.useDelimiter("\\n");
1157 while (scan
.hasNext()) {
1158 lines
.add(scan
.next());
1161 if (needle
.startsWith("^")) {
1162 if (lines
.get(lines
.size() - 1).startsWith(
1163 needle
.substring(1))) {
1164 index
= lines
.size() - 1;
1168 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1169 index
= lines
.size() - 1;
1174 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1175 rep
= lines
.get(index
+ relativeLine
);
1186 * Return the text between the key and the endKey (and optional subKey can
1187 * be passed, in this case we will look for the key first, then take the
1188 * text between the subKey and the endKey).
1190 * Will only match the first line with the given key if more than one are
1191 * possible. Which also means that if the subKey or endKey is not found on
1192 * that line, NULL will be returned.
1197 * the key to match (also supports "^" at start to say
1198 * "only if it starts with" the key)
1200 * the sub key or NULL if none
1202 * the end key or NULL for "up to the end"
1203 * @return the text or NULL if not found
1205 static protected String
getKeyLine(InputStream in
, String key
,
1206 String subKey
, String endKey
) {
1207 return getKeyText(getLine(in
, key
, 0), key
, subKey
, endKey
);
1211 * Return the text between the key and the endKey (and optional subKey can
1212 * be passed, in this case we will look for the key first, then take the
1213 * text between the subKey and the endKey).
1218 * the key to match (also supports "^" at start to say
1219 * "only if it starts with" the key)
1221 * the sub key or NULL if none
1223 * the end key or NULL for "up to the end"
1224 * @return the text or NULL if not found
1226 static protected String
getKeyText(String in
, String key
, String subKey
,
1228 String result
= null;
1231 if (line
!= null && line
.contains(key
)) {
1232 line
= line
.substring(line
.indexOf(key
) + key
.length());
1233 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1234 if (subKey
!= null) {
1235 line
= line
.substring(line
.indexOf(subKey
)
1238 if (endKey
== null || line
.contains(endKey
)) {
1239 if (endKey
!= null) {
1240 line
= line
.substring(0, line
.indexOf(endKey
));
1251 * Return the text between the key and the endKey (optional subKeys can be
1252 * passed, in this case we will look for the subKeys first, then take the
1253 * text between the key and the endKey).
1260 * the end key or NULL for "up to the end"
1262 * the sub-keys to find before checking for key/endKey
1264 * @return the text or NULL if not found
1266 static protected String
getKeyTextAfter(String in
, String key
,
1267 String endKey
, String
... afters
) {
1269 if (in
!= null && !in
.isEmpty()) {
1270 int pos
= indexOfAfter(in
, 0, afters
);
1275 in
= in
.substring(pos
);
1278 return getKeyText(in
, key
, null, endKey
);
1282 * Return the first index after all the given "afters" have been found in
1283 * the {@link String}, or -1 if it was not possible.
1288 * start at this position in the string
1290 * the sub-keys to find before checking for key/endKey
1292 * @return the text or NULL if not found
1294 static protected int indexOfAfter(String in
, int startAt
, String
... afters
) {
1296 if (in
!= null && !in
.isEmpty()) {
1298 if (afters
!= null) {
1299 for (int i
= 0; pos
>= 0 && i
< afters
.length
; i
++) {
1300 String subKey
= afters
[i
];
1301 if (!subKey
.isEmpty()) {
1302 pos
= in
.indexOf(subKey
, pos
);
1304 pos
+= subKey
.length();