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().getList(Config
.CONF_CHAPTER
)) {
395 String chapterWord
= Instance
.getConfig().getStringX(
396 Config
.CONF_CHAPTER
, lang
);
397 if (chapterName
.startsWith(chapterWord
)) {
398 chapterName
= chapterName
.substring(chapterWord
.length())
404 if (chapterName
.startsWith(Integer
.toString(number
))) {
405 chapterName
= chapterName
.substring(
406 Integer
.toString(number
).length()).trim();
409 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
410 chapterName
= chapterName
.substring(1).trim();
414 Chapter chap
= new Chapter(number
, chapterName
);
416 if (content
!= null) {
417 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
419 for (Paragraph para
: paras
) {
420 words
+= para
.getWords();
422 chap
.setParagraphs(paras
);
423 chap
.setWords(words
);
431 * Convert the given content into {@link Paragraph}s.
434 * the source URL of the story
436 * the textual content
438 * the optional progress reporter
440 * @return the {@link Paragraph}s
442 * @throws IOException
443 * in case of I/O error
445 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
446 Progress pg
) throws IOException
{
452 // Special <HR> processing:
453 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
457 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
459 if (content
!= null && !content
.trim().isEmpty()) {
461 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
462 pg
.setMinMax(0, tab
.length
);
464 for (String line
: tab
) {
465 if (line
.startsWith("[") && line
.endsWith("]")) {
466 pg
.setName("Extracting image " + i
);
468 paras
.add(makeParagraph(source
, line
.trim()));
473 List
<String
> lines
= new ArrayList
<String
>();
474 BufferedReader buff
= null;
476 buff
= new BufferedReader(
477 new InputStreamReader(new ByteArrayInputStream(
478 content
.getBytes("UTF-8")), "UTF-8"));
479 for (String line
= buff
.readLine(); line
!= null; line
= buff
481 lines
.add(line
.trim());
489 pg
.setMinMax(0, lines
.size());
491 for (String line
: lines
) {
492 if (line
.startsWith("[") && line
.endsWith("]")) {
493 pg
.setName("Extracting image " + i
);
495 paras
.add(makeParagraph(source
, line
));
501 // Check quotes for "bad" format
502 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
503 for (Paragraph para
: paras
) {
504 newParas
.addAll(requotify(para
));
508 // Remove double blanks/brks
509 fixBlanksBreaks(paras
);
516 * Convert the given line into a single {@link Paragraph}.
519 * the source URL of the story
521 * the textual content of the paragraph
523 * @return the {@link Paragraph}
525 private Paragraph
makeParagraph(URL source
, String line
) {
527 if (line
.startsWith("[") && line
.endsWith("]")) {
528 image
= getImage(this, source
, line
.substring(1, line
.length() - 1)
533 return new Paragraph(image
);
536 return processPara(line
);
540 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
541 * those {@link Paragraph}s.
543 * The resulting list will not contain a starting or trailing blank/break
544 * nor 2 blanks or breaks following each other.
547 * the list of {@link Paragraph}s to fix
549 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
550 boolean space
= false;
552 for (int i
= 0; i
< paras
.size(); i
++) {
553 Paragraph para
= paras
.get(i
);
554 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
555 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
557 if (i
> 0 && space
&& thisBrk
) {
560 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
569 // Remove blank/brk at start
571 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
572 0).getType() == ParagraphType
.BREAK
)) {
576 // Remove blank/brk at end
577 int last
= paras
.size() - 1;
579 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
580 .get(last
).getType() == ParagraphType
.BREAK
)) {
586 * Get the default cover related to this subject (see <tt>.info</tt> files).
591 * @return the cover if any, or NULL
593 static Image
getDefaultCover(String subject
) {
594 if (subject
!= null && !subject
.isEmpty()
595 && Instance
.getCoverDir() != null) {
597 File fileCover
= new File(Instance
.getCoverDir(), subject
);
598 return getImage(null, fileCover
.toURI().toURL(), subject
);
599 } catch (MalformedURLException e
) {
607 * Return the list of supported image extensions.
609 * @param emptyAllowed
610 * TRUE to allow an empty extension on first place, which can be
611 * used when you may already have an extension in your input but
612 * are not sure about it
614 * @return the extensions
616 static String
[] getImageExt(boolean emptyAllowed
) {
618 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
621 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
625 * Check if the given resource can be a local image or a remote image, then
626 * refresh the cache with it if it is.
631 * the resource to check
633 * @return the image if found, or NULL
636 static Image
getImage(BasicSupport_Deprecated support
, URL source
,
638 URL url
= getImageUrl(support
, source
, line
);
640 if ("file".equals(url
.getProtocol())) {
641 if (new File(url
.getPath()).isDirectory()) {
645 InputStream in
= null;
647 in
= Instance
.getCache().open(url
, getSupport(url
), true);
648 return new Image(in
);
649 } catch (IOException e
) {
654 } catch (IOException e
) {
664 * Check if the given resource can be a local image or a remote image, then
665 * refresh the cache with it if it is.
670 * the resource to check
672 * @return the image URL if found, or NULL
675 static URL
getImageUrl(BasicSupport_Deprecated support
, URL source
,
681 if (source
!= null) {
683 String relPath
= null;
684 String absPath
= null;
686 String path
= new File(source
.getFile()).getParent();
687 relPath
= new File(new File(path
), line
.trim())
689 } catch (Exception e
) {
690 // Cannot be converted to path (one possibility to take
691 // into account: absolute path on Windows)
694 absPath
= new File(line
.trim()).getAbsolutePath();
695 } catch (Exception e
) {
696 // Cannot be converted to path (at all)
699 for (String ext
: getImageExt(true)) {
700 File absFile
= new File(absPath
+ ext
);
701 File relFile
= new File(relPath
+ ext
);
702 if (absPath
!= null && absFile
.exists()
703 && absFile
.isFile()) {
704 url
= absFile
.toURI().toURL();
705 } else if (relPath
!= null && relFile
.exists()
706 && relFile
.isFile()) {
707 url
= relFile
.toURI().toURL();
710 } catch (Exception e
) {
711 // Should not happen since we control the correct arguments
718 for (String ext
: getImageExt(true)) {
719 if (Instance
.getCache()
720 .check(new URL(line
+ ext
), true)) {
721 url
= new URL(line
+ ext
);
728 for (String ext
: getImageExt(true)) {
730 url
= new URL(line
+ ext
);
731 Instance
.getCache().refresh(url
, support
, true);
733 } catch (IOException e
) {
734 // no image with this ext
739 } catch (MalformedURLException e
) {
744 // refresh the cached file
747 Instance
.getCache().refresh(url
, support
, true);
748 } catch (IOException e
) {
749 // woops, broken image
759 * Open the input file that will be used through the support.
761 * Can return NULL, in which case you are supposed to work without an
762 * {@link InputStream}.
765 * the source {@link URL}
767 * @return the {@link InputStream}
769 * @throws IOException
770 * in case of I/O error
772 protected InputStream
openInput(URL source
) throws IOException
{
773 return Instance
.getCache().open(source
, this, false);
777 * Reset then return {@link BasicSupport_Deprecated#in}.
779 * @return {@link BasicSupport_Deprecated#in}
781 protected InputStream
getInput() {
786 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
787 * and requotify them (i.e., separate them into QUOTE paragraphs and other
788 * paragraphs (quotes or not)).
791 * the paragraph to requotify (not necessarily a quote)
793 * @return the correctly (or so we hope) quotified paragraphs
795 protected List
<Paragraph
> requotify(Paragraph para
) {
796 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
798 if (para
.getType() == ParagraphType
.QUOTE
799 && para
.getContent().length() > 2) {
800 String line
= para
.getContent();
801 boolean singleQ
= line
.startsWith("" + openQuote
);
802 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
804 // Do not try when more than one quote at a time
805 // (some stories are not easily readable if we do)
807 && line
.indexOf(closeQuote
, 1) < line
808 .lastIndexOf(closeQuote
)) {
813 && line
.indexOf(closeDoubleQuote
, 1) < line
814 .lastIndexOf(closeDoubleQuote
)) {
820 if (!singleQ
&& !doubleQ
) {
821 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
822 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
825 char open
= singleQ ? openQuote
: openDoubleQuote
;
826 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
829 boolean inQuote
= false;
831 for (char car
: line
.toCharArray()) {
834 } else if (car
== close
) {
836 } else if (car
== '.' && !inQuote
) {
844 String rest
= line
.substring(posDot
+ 1).trim();
845 line
= line
.substring(0, posDot
+ 1).trim();
847 for (char car
: line
.toCharArray()) {
852 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
853 if (!rest
.isEmpty()) {
854 newParas
.addAll(requotify(processPara(rest
)));
868 * Process a {@link Paragraph} from a raw line of text.
870 * Will also fix quotes and HTML encoding if needed.
875 * @return the processed {@link Paragraph}
877 protected Paragraph
processPara(String line
) {
878 line
= ifUnhtml(line
).trim();
880 boolean space
= true;
882 boolean quote
= false;
883 boolean tentativeCloseQuote
= false;
888 StringBuilder builder
= new StringBuilder();
889 for (char car
: line
.toCharArray()) {
892 // dash, ndash and mdash: - – —
893 // currently: always use mdash
894 builder
.append(dashCount
== 1 ?
'-' : '—');
899 if (tentativeCloseQuote
) {
900 tentativeCloseQuote
= false;
901 if (Character
.isLetterOrDigit(car
)) {
904 // handle double-single quotes as double quotes
906 builder
.append(closeDoubleQuote
);
910 builder
.append(closeQuote
);
915 case ' ': // note: unbreakable space
918 case '\n': // just in case
919 case '\r': // just in case
920 if (builder
.length() > 0
921 && builder
.charAt(builder
.length() - 1) != ' ') {
928 if (space
|| (brk
&& quote
)) {
930 // handle double-single quotes as double quotes
932 builder
.deleteCharAt(builder
.length() - 1);
933 builder
.append(openDoubleQuote
);
935 builder
.append(openQuote
);
937 } else if (prev
== ' ' || prev
== car
) {
938 // handle double-single quotes as double quotes
940 builder
.deleteCharAt(builder
.length() - 1);
941 builder
.append(openDoubleQuote
);
943 builder
.append(openQuote
);
946 // it is a quote ("I'm off") or a 'quote' ("This
947 // 'good' restaurant"...)
948 tentativeCloseQuote
= true;
953 if (space
|| (brk
&& quote
)) {
955 builder
.append(openDoubleQuote
);
956 } else if (prev
== ' ') {
957 builder
.append(openDoubleQuote
);
959 builder
.append(closeDoubleQuote
);
993 if (space
|| (brk
&& quote
)) {
995 builder
.append(openQuote
);
997 // handle double-single quotes as double quotes
999 builder
.deleteCharAt(builder
.length() - 1);
1000 builder
.append(openDoubleQuote
);
1002 builder
.append(openQuote
);
1016 // handle double-single quotes as double quotes
1018 builder
.deleteCharAt(builder
.length() - 1);
1019 builder
.append(closeDoubleQuote
);
1021 builder
.append(closeQuote
);
1030 if (space
|| (brk
&& quote
)) {
1032 builder
.append(openDoubleQuote
);
1034 builder
.append(openDoubleQuote
);
1047 builder
.append(closeDoubleQuote
);
1053 builder
.append(car
);
1060 if (tentativeCloseQuote
) {
1061 tentativeCloseQuote
= false;
1062 builder
.append(closeQuote
);
1065 line
= builder
.toString().trim();
1067 ParagraphType type
= ParagraphType
.NORMAL
;
1069 type
= ParagraphType
.BLANK
;
1071 type
= ParagraphType
.BREAK
;
1073 type
= ParagraphType
.QUOTE
;
1076 return new Paragraph(type
, line
, words
);
1080 * Remove the HTML from the input <b>if</b>
1081 * {@link BasicSupport_Deprecated#isHtml()} is true.
1086 * @return the no html version if needed
1088 private String
ifUnhtml(String input
) {
1089 if (isHtml() && input
!= null) {
1090 return StringUtils
.unhtml(input
);
1097 * Reset the given {@link InputStream} and return it.
1100 * the {@link InputStream} to reset
1102 * @return the same {@link InputStream} after reset
1104 static protected InputStream
reset(InputStream in
) {
1109 } catch (IOException e
) {
1116 * Return the first line from the given input which correspond to the given
1122 * a string that must be found inside the target line (also
1123 * supports "^" at start to say "only if it starts with" the
1125 * @param relativeLine
1126 * the line to return based upon the target line position (-1 =
1127 * the line before, 0 = the target line...)
1129 * @return the line, or NULL if not found
1131 static protected String
getLine(InputStream in
, String needle
,
1133 return getLine(in
, needle
, relativeLine
, true);
1137 * Return a line from the given input which correspond to the given
1143 * a string that must be found inside the target line (also
1144 * supports "^" at start to say "only if it starts with" the
1146 * @param relativeLine
1147 * the line to return based upon the target line position (-1 =
1148 * the line before, 0 = the target line...)
1150 * takes the first result (as opposed to the last one, which will
1151 * also always spend the input)
1153 * @return the line, or NULL if not found
1155 static protected String
getLine(InputStream in
, String needle
,
1156 int relativeLine
, boolean first
) {
1161 List
<String
> lines
= new ArrayList
<String
>();
1162 @SuppressWarnings("resource")
1163 Scanner scan
= new Scanner(in
, "UTF-8");
1165 scan
.useDelimiter("\\n");
1166 while (scan
.hasNext()) {
1167 lines
.add(scan
.next());
1170 if (needle
.startsWith("^")) {
1171 if (lines
.get(lines
.size() - 1).startsWith(
1172 needle
.substring(1))) {
1173 index
= lines
.size() - 1;
1177 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1178 index
= lines
.size() - 1;
1183 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1184 rep
= lines
.get(index
+ relativeLine
);
1195 * Return the text between the key and the endKey (and optional subKey can
1196 * be passed, in this case we will look for the key first, then take the
1197 * text between the subKey and the endKey).
1199 * Will only match the first line with the given key if more than one are
1200 * possible. Which also means that if the subKey or endKey is not found on
1201 * that line, NULL will be returned.
1206 * the key to match (also supports "^" at start to say
1207 * "only if it starts with" the key)
1209 * the sub key or NULL if none
1211 * the end key or NULL for "up to the end"
1212 * @return the text or NULL if not found
1214 static protected String
getKeyLine(InputStream in
, String key
,
1215 String subKey
, String endKey
) {
1216 return getKeyText(getLine(in
, key
, 0), key
, subKey
, endKey
);
1220 * Return the text between the key and the endKey (and optional subKey can
1221 * be passed, in this case we will look for the key first, then take the
1222 * text between the subKey and the endKey).
1227 * the key to match (also supports "^" at start to say
1228 * "only if it starts with" the key)
1230 * the sub key or NULL if none
1232 * the end key or NULL for "up to the end"
1233 * @return the text or NULL if not found
1235 static protected String
getKeyText(String in
, String key
, String subKey
,
1237 String result
= null;
1240 if (line
!= null && line
.contains(key
)) {
1241 line
= line
.substring(line
.indexOf(key
) + key
.length());
1242 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1243 if (subKey
!= null) {
1244 line
= line
.substring(line
.indexOf(subKey
)
1247 if (endKey
== null || line
.contains(endKey
)) {
1248 if (endKey
!= null) {
1249 line
= line
.substring(0, line
.indexOf(endKey
));
1260 * Return the text between the key and the endKey (optional subKeys can be
1261 * passed, in this case we will look for the subKeys first, then take the
1262 * text between the key and the endKey).
1269 * the end key or NULL for "up to the end"
1271 * the sub-keys to find before checking for key/endKey
1273 * @return the text or NULL if not found
1275 static protected String
getKeyTextAfter(String in
, String key
,
1276 String endKey
, String
... afters
) {
1278 if (in
!= null && !in
.isEmpty()) {
1279 int pos
= indexOfAfter(in
, 0, afters
);
1284 in
= in
.substring(pos
);
1287 return getKeyText(in
, key
, null, endKey
);
1291 * Return the first index after all the given "afters" have been found in
1292 * the {@link String}, or -1 if it was not possible.
1297 * start at this position in the string
1299 * the sub-keys to find before checking for key/endKey
1301 * @return the text or NULL if not found
1303 static protected int indexOfAfter(String in
, int startAt
, String
... afters
) {
1305 if (in
!= null && !in
.isEmpty()) {
1307 if (afters
!= null) {
1308 for (int i
= 0; pos
>= 0 && i
< afters
.length
; i
++) {
1309 String subKey
= afters
[i
];
1310 if (!subKey
.isEmpty()) {
1311 pos
= in
.indexOf(subKey
, pos
);
1313 pos
+= subKey
.length();