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 meta
.setPublisher(getType().getSourceName());
212 if (meta
.getCreationDate() == null
213 || meta
.getCreationDate().trim().isEmpty()) {
214 meta
.setCreationDate(bsHelper
.formatDate(
215 StringUtils
.fromTime(new Date().getTime())));
218 pg
.put("meta", meta
);
222 if (meta
.getCover() == null) {
223 meta
.setCover(getDefaultCover(meta
.getSubject()));
229 String descChapterName
= Instance
.getInstance().getTrans().getString(StringId
.DESCRIPTION
);
230 story
.getMeta().setResume(makeChapter(url
, 0, descChapterName
, 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 pg
.put("meta", story
.getMeta());
274 if (!pgMeta
.isDone()) {
275 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
278 setCurrentReferer(url
);
280 Progress pgGetChapters
= new Progress();
281 pg
.addProgress(pgGetChapters
, 10);
282 story
.setChapters(new ArrayList
<Chapter
>());
283 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
285 if (!pgGetChapters
.isDone()) {
286 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
289 if (chapters
!= null) {
290 Progress pgChaps
= new Progress("Extracting chapters", 0,
291 chapters
.size() * 300);
292 pg
.addProgress(pgChaps
, 80);
296 for (Entry
<String
, URL
> chap
: chapters
) {
297 pgChaps
.setName("Extracting chapter " + i
);
298 InputStream chapIn
= null;
299 if (chap
.getValue() != null) {
300 setCurrentReferer(chap
.getValue());
301 chapIn
= Instance
.getInstance().getCache().open(chap
.getValue(), this, false);
303 pgChaps
.setProgress(i
* 100);
305 Progress pgGetChapterContent
= new Progress();
306 Progress pgMakeChapter
= new Progress();
307 pgChaps
.addProgress(pgGetChapterContent
, 100);
308 pgChaps
.addProgress(pgMakeChapter
, 100);
310 String content
= getChapterContent(url
, chapIn
, i
,
311 pgGetChapterContent
);
312 if (!pgGetChapterContent
.isDone()) {
313 pgGetChapterContent
.setProgress(pgGetChapterContent
317 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
318 content
, pgMakeChapter
);
319 if (!pgMakeChapter
.isDone()) {
320 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
323 words
+= cc
.getWords();
324 story
.getChapters().add(cc
);
326 if (chapIn
!= null) {
334 story
.getMeta().setWords(words
);
336 pgChaps
.setName("Extracting chapters");
341 // Check for "no chapters" stories
342 if (story
.getChapters().isEmpty()
343 && story
.getMeta().getResume() != null
344 && !story
.getMeta().getResume().getParagraphs().isEmpty()) {
345 Chapter resume
= story
.getMeta().getResume();
348 story
.getChapters().add(resume
);
349 story
.getMeta().setWords(resume
.getWords());
351 String descChapterName
= Instance
.getInstance().getTrans()
352 .getString(StringId
.DESCRIPTION
);
353 resume
= new Chapter(0, descChapterName
);
354 story
.getMeta().setResume(resume
);
368 * Prepare the support if needed before processing.
371 * the source of the story
373 * the input (the main resource)
375 * @throws IOException
378 @SuppressWarnings("unused")
379 protected void preprocess(URL source
, InputStream in
) throws IOException
{
383 * Create a {@link Chapter} object from the given information, formatting
384 * the content as it should be.
387 * the source of the story
393 * the chapter content
395 * the optional progress reporter
397 * @return the {@link Chapter}, never NULL
399 * @throws IOException
400 * in case of I/O error
402 protected Chapter
makeChapter(URL source
, int number
, String name
,
403 String content
, Progress pg
) throws IOException
{
404 // Chapter name: process it correctly, then remove the possible
405 // redundant "Chapter x: " in front of it, or "-" (as in
406 // "Chapter 5: - Fun!" after the ": " was automatically added)
407 String chapterName
= processPara(name
).getContent().trim();
408 for (String lang
: Instance
.getInstance().getConfig().getList(Config
.CONF_CHAPTER
)) {
409 String chapterWord
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_CHAPTER
, lang
);
410 if (chapterName
.startsWith(chapterWord
)) {
411 chapterName
= chapterName
.substring(chapterWord
.length())
417 if (chapterName
.startsWith(Integer
.toString(number
))) {
418 chapterName
= chapterName
.substring(
419 Integer
.toString(number
).length()).trim();
422 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
423 chapterName
= chapterName
.substring(1).trim();
427 Chapter chap
= new Chapter(number
, chapterName
);
429 if (content
!= null) {
430 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
432 for (Paragraph para
: paras
) {
433 words
+= para
.getWords();
435 chap
.setParagraphs(paras
);
436 chap
.setWords(words
);
444 * Convert the given content into {@link Paragraph}s.
447 * the source URL of the story
449 * the textual content
451 * the optional progress reporter
453 * @return the {@link Paragraph}s (can be empty, but never NULL)
455 * @throws IOException
456 * in case of I/O error
458 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
459 Progress pg
) throws IOException
{
465 // Special <HR> processing:
466 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
470 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
471 if (content
!= null && !content
.trim().isEmpty()) {
473 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
474 pg
.setMinMax(0, tab
.length
);
476 for (String line
: tab
) {
477 if (line
.startsWith("[") && line
.endsWith("]")) {
478 pg
.setName("Extracting image " + i
);
480 paras
.add(makeParagraph(source
, line
.trim()));
485 List
<String
> lines
= new ArrayList
<String
>();
486 BufferedReader buff
= null;
488 buff
= new BufferedReader(
489 new InputStreamReader(new ByteArrayInputStream(
490 content
.getBytes("UTF-8")), "UTF-8"));
491 for (String line
= buff
.readLine(); line
!= null; line
= buff
493 lines
.add(line
.trim());
501 pg
.setMinMax(0, lines
.size());
503 for (String line
: lines
) {
504 if (line
.startsWith("[") && line
.endsWith("]")) {
505 pg
.setName("Extracting image " + i
);
507 paras
.add(makeParagraph(source
, line
));
513 // Check quotes for "bad" format
514 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
515 for (Paragraph para
: paras
) {
516 newParas
.addAll(requotify(para
));
520 // Remove double blanks/brks
521 fixBlanksBreaks(paras
);
528 * Convert the given line into a single {@link Paragraph}.
531 * the source URL of the story
533 * the textual content of the paragraph
535 * @return the {@link Paragraph}, never NULL
537 private Paragraph
makeParagraph(URL source
, String line
) {
539 if (line
.startsWith("[") && line
.endsWith("]")) {
540 image
= getImage(this, source
, line
.substring(1, line
.length() - 1)
545 return new Paragraph(image
);
548 return processPara(line
);
552 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
553 * those {@link Paragraph}s.
555 * The resulting list will not contain a starting or trailing blank/break
556 * nor 2 blanks or breaks following each other.
559 * the list of {@link Paragraph}s to fix
561 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
562 boolean space
= false;
564 for (int i
= 0; i
< paras
.size(); i
++) {
565 Paragraph para
= paras
.get(i
);
566 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
567 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
569 if (i
> 0 && space
&& thisBrk
) {
572 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
581 // Remove blank/brk at start
583 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
584 0).getType() == ParagraphType
.BREAK
)) {
588 // Remove blank/brk at end
589 int last
= paras
.size() - 1;
591 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
592 .get(last
).getType() == ParagraphType
.BREAK
)) {
598 * Get the default cover related to this subject (see <tt>.info</tt> files).
603 * @return the cover if any, or NULL
605 static Image
getDefaultCover(String subject
) {
606 if (subject
!= null && !subject
.isEmpty() && Instance
.getInstance().getCoverDir() != null) {
608 File fileCover
= new File(Instance
.getInstance().getCoverDir(), subject
);
609 return getImage(null, fileCover
.toURI().toURL(), subject
);
610 } catch (MalformedURLException e
) {
618 * Return the list of supported image extensions.
620 * @param emptyAllowed
621 * TRUE to allow an empty extension on first place, which can be
622 * used when you may already have an extension in your input but
623 * are not sure about it
625 * @return the extensions
627 static String
[] getImageExt(boolean emptyAllowed
) {
629 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
632 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
636 * Check if the given resource can be a local image or a remote image, then
637 * refresh the cache with it if it is.
642 * the resource to check
644 * @return the image if found, or NULL
647 static Image
getImage(BasicSupport_Deprecated support
, URL source
,
649 URL url
= getImageUrl(support
, source
, line
);
651 if ("file".equals(url
.getProtocol())) {
652 if (new File(url
.getPath()).isDirectory()) {
656 InputStream in
= null;
658 in
= Instance
.getInstance().getCache().open(url
, getSupport(url
), true);
659 Image img
= new Image(in
);
660 if (img
.getSize() == 0) {
662 throw new IOException(
663 "Empty image not accepted");
666 } catch (IOException e
) {
671 } catch (IOException e
) {
681 * Check if the given resource can be a local image or a remote image, then
682 * refresh the cache with it if it is.
687 * the resource to check
689 * @return the image URL if found, or NULL
692 static URL
getImageUrl(BasicSupport_Deprecated support
, URL source
,
698 if (source
!= null) {
700 String relPath
= null;
701 String absPath
= null;
703 String path
= new File(source
.getFile()).getParent();
704 relPath
= new File(new File(path
), line
.trim())
706 } catch (Exception e
) {
707 // Cannot be converted to path (one possibility to take
708 // into account: absolute path on Windows)
711 absPath
= new File(line
.trim()).getAbsolutePath();
712 } catch (Exception e
) {
713 // Cannot be converted to path (at all)
716 for (String ext
: getImageExt(true)) {
717 File absFile
= new File(absPath
+ ext
);
718 File relFile
= new File(relPath
+ ext
);
719 if (absPath
!= null && absFile
.exists()
720 && absFile
.isFile()) {
721 url
= absFile
.toURI().toURL();
722 } else if (relPath
!= null && relFile
.exists()
723 && relFile
.isFile()) {
724 url
= relFile
.toURI().toURL();
727 } catch (Exception e
) {
728 // Should not happen since we control the correct arguments
735 for (String ext
: getImageExt(true)) {
736 if (Instance
.getInstance().getCache().check(new URL(line
+ ext
), true)) {
737 url
= new URL(line
+ ext
);
744 for (String ext
: getImageExt(true)) {
746 url
= new URL(line
+ ext
);
747 Instance
.getInstance().getCache().refresh(url
, support
, true);
749 } catch (IOException e
) {
750 // no image with this ext
755 } catch (MalformedURLException e
) {
760 // refresh the cached file
763 Instance
.getInstance().getCache().refresh(url
, support
, true);
764 } catch (IOException e
) {
765 // woops, broken image
775 * Open the input file that will be used through the support.
777 * Can return NULL, in which case you are supposed to work without an
778 * {@link InputStream}.
781 * the source {@link URL}
783 * @return the {@link InputStream}
785 * @throws IOException
786 * in case of I/O error
788 protected InputStream
openInput(URL source
) throws IOException
{
789 return Instance
.getInstance().getCache().open(source
, this, false);
793 * Reset then return {@link BasicSupport_Deprecated#in}.
795 * @return {@link BasicSupport_Deprecated#in}
797 protected InputStream
getInput() {
802 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
803 * and requotify them (i.e., separate them into QUOTE paragraphs and other
804 * paragraphs (quotes or not)).
807 * the paragraph to requotify (not necessarily a quote)
809 * @return the correctly (or so we hope) quotified paragraphs
811 protected List
<Paragraph
> requotify(Paragraph para
) {
812 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
814 if (para
.getType() == ParagraphType
.QUOTE
815 && para
.getContent().length() > 2) {
816 String line
= para
.getContent();
817 boolean singleQ
= line
.startsWith("" + openQuote
);
818 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
820 // Do not try when more than one quote at a time
821 // (some stories are not easily readable if we do)
823 && line
.indexOf(closeQuote
, 1) < line
824 .lastIndexOf(closeQuote
)) {
829 && line
.indexOf(closeDoubleQuote
, 1) < line
830 .lastIndexOf(closeDoubleQuote
)) {
836 if (!singleQ
&& !doubleQ
) {
837 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
838 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
841 char open
= singleQ ? openQuote
: openDoubleQuote
;
842 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
845 boolean inQuote
= false;
847 for (char car
: line
.toCharArray()) {
850 } else if (car
== close
) {
852 } else if (car
== '.' && !inQuote
) {
860 String rest
= line
.substring(posDot
+ 1).trim();
861 line
= line
.substring(0, posDot
+ 1).trim();
863 for (char car
: line
.toCharArray()) {
868 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
869 if (!rest
.isEmpty()) {
870 newParas
.addAll(requotify(processPara(rest
)));
884 * Process a {@link Paragraph} from a raw line of text.
886 * Will also fix quotes and HTML encoding if needed.
891 * @return the processed {@link Paragraph}, never NULL
893 protected Paragraph
processPara(String line
) {
894 line
= ifUnhtml(line
).trim();
896 boolean space
= true;
898 boolean quote
= false;
899 boolean tentativeCloseQuote
= false;
904 StringBuilder builder
= new StringBuilder();
905 for (char car
: line
.toCharArray()) {
908 // dash, ndash and mdash: - – —
909 // currently: always use mdash
910 builder
.append(dashCount
== 1 ?
'-' : '—');
915 if (tentativeCloseQuote
) {
916 tentativeCloseQuote
= false;
917 if (Character
.isLetterOrDigit(car
)) {
920 // handle double-single quotes as double quotes
922 builder
.append(closeDoubleQuote
);
926 builder
.append(closeQuote
);
931 case ' ': // note: unbreakable space
934 case '\n': // just in case
935 case '\r': // just in case
936 if (builder
.length() > 0
937 && builder
.charAt(builder
.length() - 1) != ' ') {
944 if (space
|| (brk
&& quote
)) {
946 // handle double-single quotes as double quotes
948 builder
.deleteCharAt(builder
.length() - 1);
949 builder
.append(openDoubleQuote
);
951 builder
.append(openQuote
);
953 } else if (prev
== ' ' || prev
== car
) {
954 // handle double-single quotes as double quotes
956 builder
.deleteCharAt(builder
.length() - 1);
957 builder
.append(openDoubleQuote
);
959 builder
.append(openQuote
);
962 // it is a quote ("I'm off") or a 'quote' ("This
963 // 'good' restaurant"...)
964 tentativeCloseQuote
= true;
969 if (space
|| (brk
&& quote
)) {
971 builder
.append(openDoubleQuote
);
972 } else if (prev
== ' ') {
973 builder
.append(openDoubleQuote
);
975 builder
.append(closeDoubleQuote
);
1000 builder
.append(car
);
1009 if (space
|| (brk
&& quote
)) {
1011 builder
.append(openQuote
);
1013 // handle double-single quotes as double quotes
1015 builder
.deleteCharAt(builder
.length() - 1);
1016 builder
.append(openDoubleQuote
);
1018 builder
.append(openQuote
);
1032 // handle double-single quotes as double quotes
1034 builder
.deleteCharAt(builder
.length() - 1);
1035 builder
.append(closeDoubleQuote
);
1037 builder
.append(closeQuote
);
1046 if (space
|| (brk
&& quote
)) {
1048 builder
.append(openDoubleQuote
);
1050 builder
.append(openDoubleQuote
);
1063 builder
.append(closeDoubleQuote
);
1069 builder
.append(car
);
1076 if (tentativeCloseQuote
) {
1077 tentativeCloseQuote
= false;
1078 builder
.append(closeQuote
);
1081 line
= builder
.toString().trim();
1083 ParagraphType type
= ParagraphType
.NORMAL
;
1085 type
= ParagraphType
.BLANK
;
1087 type
= ParagraphType
.BREAK
;
1089 type
= ParagraphType
.QUOTE
;
1092 return new Paragraph(type
, line
, words
);
1096 * Remove the HTML from the input <b>if</b>
1097 * {@link BasicSupport_Deprecated#isHtml()} is true.
1102 * @return the no html version if needed
1104 private String
ifUnhtml(String input
) {
1105 if (isHtml() && input
!= null) {
1106 return StringUtils
.unhtml(input
);
1113 * Reset the given {@link InputStream} and return it.
1116 * the {@link InputStream} to reset
1118 * @return the same {@link InputStream} after reset
1120 static protected InputStream
reset(InputStream in
) {
1125 } catch (IOException e
) {
1132 * Return the first line from the given input which correspond to the given
1138 * a string that must be found inside the target line (also
1139 * supports "^" at start to say "only if it starts with" the
1141 * @param relativeLine
1142 * the line to return based upon the target line position (-1 =
1143 * the line before, 0 = the target line...)
1145 * @return the line, or NULL if not found
1147 static protected String
getLine(InputStream in
, String needle
,
1149 return getLine(in
, needle
, relativeLine
, true);
1153 * Return a line from the given input which correspond to the given
1159 * a string that must be found inside the target line (also
1160 * supports "^" at start to say "only if it starts with" the
1162 * @param relativeLine
1163 * the line to return based upon the target line position (-1 =
1164 * the line before, 0 = the target line...)
1166 * takes the first result (as opposed to the last one, which will
1167 * also always spend the input)
1169 * @return the line, or NULL if not found
1171 static protected String
getLine(InputStream in
, String needle
,
1172 int relativeLine
, boolean first
) {
1177 List
<String
> lines
= new ArrayList
<String
>();
1178 @SuppressWarnings("resource")
1179 Scanner scan
= new Scanner(in
, "UTF-8");
1181 scan
.useDelimiter("\\n");
1182 while (scan
.hasNext()) {
1183 lines
.add(scan
.next());
1186 if (needle
.startsWith("^")) {
1187 if (lines
.get(lines
.size() - 1).startsWith(
1188 needle
.substring(1))) {
1189 index
= lines
.size() - 1;
1193 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1194 index
= lines
.size() - 1;
1199 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1200 rep
= lines
.get(index
+ relativeLine
);
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).
1215 * Will only match the first line with the given key if more than one are
1216 * possible. Which also means that if the subKey or endKey is not found on
1217 * that line, NULL will be returned.
1222 * the key to match (also supports "^" at start to say
1223 * "only if it starts with" the key)
1225 * the sub key or NULL if none
1227 * the end key or NULL for "up to the end"
1228 * @return the text or NULL if not found
1230 static protected String
getKeyLine(InputStream in
, String key
,
1231 String subKey
, String endKey
) {
1232 return getKeyText(getLine(in
, key
, 0), key
, subKey
, endKey
);
1236 * Return the text between the key and the endKey (and optional subKey can
1237 * be passed, in this case we will look for the key first, then take the
1238 * text between the subKey and the endKey).
1243 * the key to match (also supports "^" at start to say
1244 * "only if it starts with" the key)
1246 * the sub key or NULL if none
1248 * the end key or NULL for "up to the end"
1249 * @return the text or NULL if not found
1251 static protected String
getKeyText(String in
, String key
, String subKey
,
1253 String result
= null;
1256 if (line
!= null && line
.contains(key
)) {
1257 line
= line
.substring(line
.indexOf(key
) + key
.length());
1258 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1259 if (subKey
!= null) {
1260 line
= line
.substring(line
.indexOf(subKey
)
1263 if (endKey
== null || line
.contains(endKey
)) {
1264 if (endKey
!= null) {
1265 line
= line
.substring(0, line
.indexOf(endKey
));
1276 * Return the text between the key and the endKey (optional subKeys can be
1277 * passed, in this case we will look for the subKeys first, then take the
1278 * text between the key and the endKey).
1285 * the end key or NULL for "up to the end"
1287 * the sub-keys to find before checking for key/endKey
1289 * @return the text or NULL if not found
1291 static protected String
getKeyTextAfter(String in
, String key
,
1292 String endKey
, String
... afters
) {
1294 if (in
!= null && !in
.isEmpty()) {
1295 int pos
= indexOfAfter(in
, 0, afters
);
1300 in
= in
.substring(pos
);
1303 return getKeyText(in
, key
, null, endKey
);
1307 * Return the first index after all the given "afters" have been found in
1308 * the {@link String}, or -1 if it was not possible.
1313 * start at this position in the string
1315 * the sub-keys to find before checking for key/endKey
1317 * @return the text or NULL if not found
1319 static protected int indexOfAfter(String in
, int startAt
, String
... afters
) {
1321 if (in
!= null && !in
.isEmpty()) {
1323 if (afters
!= null) {
1324 for (int i
= 0; pos
>= 0 && i
< afters
.length
; i
++) {
1325 String subKey
= afters
[i
];
1326 if (!subKey
.isEmpty()) {
1327 pos
= in
.indexOf(subKey
, pos
);
1329 pos
+= subKey
.length();