summaryrefslogtreecommitdiff
path: root/src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java')
-rw-r--r--src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java b/src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java
new file mode 100644
index 0000000..15bc52d
--- /dev/null
+++ b/src/net/sourceforge/plantuml/preproc/StartDiagramExtractReader.java
@@ -0,0 +1,139 @@
+/* ========================================================================
+ * PlantUML : a free UML diagram generator
+ * ========================================================================
+ *
+ * (C) Copyright 2009-2014, Arnaud Roques
+ *
+ * Project Info: http://plantuml.sourceforge.net
+ *
+ * This file is part of PlantUML.
+ *
+ * Licensed under The MIT License (Massachusetts Institute of Technology License)
+ *
+ * See http://opensource.org/licenses/MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+ * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ *
+ * Original Author: Arnaud Roques
+ */
+package net.sourceforge.plantuml.preproc;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+
+import net.sourceforge.plantuml.Log;
+import net.sourceforge.plantuml.utils.StartUtils;
+
+public class StartDiagramExtractReader implements ReadLine {
+
+ private final ReadLine raw;
+ private boolean finished = false;
+
+ public StartDiagramExtractReader(File f, int num, String charset) throws IOException {
+ this(getReadLine(f, charset), num, charset);
+ }
+
+ public StartDiagramExtractReader(URL url, int num, String charset) throws IOException {
+ this(getReadLine(url, charset), num, charset);
+ }
+
+ private StartDiagramExtractReader(ReadLine raw, int num, String charset) throws IOException {
+ if (num < 0) {
+ throw new IllegalArgumentException();
+ }
+ this.raw = raw;
+ String s = null;
+ while ((s = raw.readLine()) != null) {
+ if (StartUtils.isArobaseStartDiagram(s)) {
+ if (num == 0) {
+ return;
+ }
+ num--;
+ }
+ }
+ finished = true;
+ }
+
+ private static ReadLine getReadLine(File f, String charset) throws IOException {
+
+ if (charset == null) {
+ Log.info("Using default charset");
+ return new UncommentReadLine(new ReadLineReader(new FileReader(f)));
+ }
+ Log.info("Using charset " + charset);
+ return new UncommentReadLine(new ReadLineReader(new InputStreamReader(new FileInputStream(f), charset)));
+ }
+
+ private static ReadLine getReadLine(URL url, String charset) throws IOException {
+
+ if (charset == null) {
+ Log.info("Using default charset");
+ return new UncommentReadLine(new ReadLineReader(new InputStreamReader(url.openStream())));
+ }
+ Log.info("Using charset " + charset);
+ return new UncommentReadLine(new ReadLineReader(new InputStreamReader(url.openStream(), charset)));
+ }
+
+ static public boolean containsStartDiagram(File f, String charset) throws IOException {
+ final ReadLine r = getReadLine(f, charset);
+ return containsStartDiagram(r);
+ }
+
+ static public boolean containsStartDiagram(URL url, String charset) throws IOException {
+ final ReadLine r = getReadLine(url, charset);
+ return containsStartDiagram(r);
+ }
+
+ private static boolean containsStartDiagram(final ReadLine r) throws IOException {
+ try {
+ String s = null;
+ while ((s = r.readLine()) != null) {
+ if (StartUtils.isArobaseStartDiagram(s)) {
+ return true;
+ }
+ }
+ } finally {
+ if (r != null) {
+ r.close();
+ }
+ }
+ return false;
+ }
+
+ public String readLine() throws IOException {
+ if (finished) {
+ return null;
+ }
+ final String result = raw.readLine();
+ if (result != null && StartUtils.isArobaseEndDiagram(result)) {
+ finished = true;
+ return null;
+ }
+ return result;
+ }
+
+ public void close() throws IOException {
+ raw.close();
+ }
+
+}