added ShipDescription
authoradam <adam@megacz.com>
Mon, 12 Feb 2007 09:37:51 +0000 (10:37 +0100)
committeradam <adam@megacz.com>
Mon, 12 Feb 2007 09:37:51 +0000 (10:37 +0100)
src/edu/berkeley/fleet/doc/ShipDescription.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/fleet/doc/ShipDescription.java b/src/edu/berkeley/fleet/doc/ShipDescription.java
new file mode 100644 (file)
index 0000000..f32b7ab
--- /dev/null
@@ -0,0 +1,83 @@
+package edu.berkeley.fleet.doc;
+
+import java.io.*;
+import java.util.*;
+
+public class ShipDescription {
+
+    public String name;
+    public String texDocumentation;
+    public ArrayList<BenkoBox> benkoBoxes = new ArrayList<BenkoBox>();
+
+    public HashMap<String,String> sections =
+        new HashMap<String,String>();
+
+    public class BenkoBox {
+        public boolean inbox;
+        public boolean tokenOnly;
+        public boolean leftSide;
+        public String[] ports;
+    }
+
+    public class Literal {
+        public String name;
+    }
+
+    public ShipDescription(BufferedReader r) throws IOException {
+        String sectionName = "";
+        StringBuffer sb = new StringBuffer();
+        while(true) {
+            String s = r.readLine();
+            if (s==null) break;
+            if (s.startsWith("==")) {
+                sections.put(sectionName, sb.toString());
+                sb = new StringBuffer();
+                sectionName = s.trim();
+                while(sectionName.startsWith("="))
+                    sectionName = sectionName.substring(1);
+                while(sectionName.endsWith("="))
+                    sectionName = sectionName.substring(0, sectionName.length()-1);
+                sectionName = sectionName.trim().toLowerCase();
+                continue;
+            }
+            sb.append(s+"\n");
+        }
+        for(String s : sections.keySet())
+            processSection(s);
+    }
+
+    public void processSection(String section) throws IOException {
+        if (section.equals("")) {
+            BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
+            for(String s = br.readLine(); s != null; s = br.readLine()) {
+                if (s.trim().length()==0) continue;
+                String key = s.substring(0, s.indexOf(':')).trim();
+                String val = s.substring(s.indexOf(':')+1).trim();
+                if (key.toLowerCase().equals("ship"))
+                    name = val.trim();
+            }
+        } else if (section.equals("ports")) {
+            BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
+            boolean rightSide = false;
+            for(String s = br.readLine(); s != null; s = br.readLine()) {
+                if (s.trim().length()==0) { rightSide = true; continue; }
+                String key = s.substring(0, s.indexOf(':')).trim();
+                String val = s.substring(s.indexOf(':')+1).trim();
+                BenkoBox p = new BenkoBox();
+                key = key.replaceAll("  +", " ");
+                if      (key.equals("token in"))  { p.tokenOnly = true;   p.inbox = true;  }
+                else if (key.equals("token out")) { p.tokenOnly = true;   p.inbox = false; }
+                else if (key.equals("data in"))   { p.tokenOnly = false;  p.inbox = true;  }
+                else if (key.equals("data out"))  { p.tokenOnly = false;  p.inbox = false; }
+                else throw new RuntimeException("unknown port type: \""+key+"\"");
+                StringTokenizer st = new StringTokenizer(val, " ");
+                p.ports = new String[st.countTokens()];
+                for(int i=0; i<p.ports.length; i++)
+                    p.ports[i] = st.nextToken();
+                p.leftSide = !rightSide;
+                benkoBoxes.add(p);
+            }
+        }
+    }
+
+}
\ No newline at end of file