aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/py/javatoolkit/parser')
-rw-r--r--src/py/javatoolkit/parser/.cvsignore1
-rw-r--r--src/py/javatoolkit/parser/__init__.py14
-rw-r--r--src/py/javatoolkit/parser/buildproperties.py83
-rw-r--r--src/py/javatoolkit/parser/helpers.py61
-rw-r--r--src/py/javatoolkit/parser/manifest.py61
-rw-r--r--src/py/javatoolkit/parser/parser.py16
-rw-r--r--src/py/javatoolkit/parser/tree.py107
7 files changed, 343 insertions, 0 deletions
diff --git a/src/py/javatoolkit/parser/.cvsignore b/src/py/javatoolkit/parser/.cvsignore
new file mode 100644
index 0000000..0d20b64
--- /dev/null
+++ b/src/py/javatoolkit/parser/.cvsignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/src/py/javatoolkit/parser/__init__.py b/src/py/javatoolkit/parser/__init__.py
new file mode 100644
index 0000000..d96be04
--- /dev/null
+++ b/src/py/javatoolkit/parser/__init__.py
@@ -0,0 +1,14 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from helpers import *
+import buildproperties
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/buildproperties.py b/src/py/javatoolkit/parser/buildproperties.py
new file mode 100644
index 0000000..0c2ee53
--- /dev/null
+++ b/src/py/javatoolkit/parser/buildproperties.py
@@ -0,0 +1,83 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from tree import *
+import parser
+
+class BuildPropertiesParser(parser.Parser):
+
+ def parse(self, ins):
+ """ Parse an input stream containing an ant build.properties file. Return a
+ structured document represented by tree.Node
+
+ @param ins - input stream
+ @return tree.Node containing the structured representation
+ """
+
+ lineno = 0
+ continued_line = False
+ inside_html_comment = False
+ attrib = ""
+ value = ""
+ root = Node()
+
+ for x in ins.readlines():
+ lineno += 1
+ x = x.strip()
+
+ if inside_html_comment and x.find("-->") != -1:
+ inside_html_comment = False
+ x = x.split("-->", 1)[0]
+
+ if x.find("<!--") != -1:
+ inside_html_comment = True
+
+ if inside_html_comment:
+ continue
+
+ if continued_line:
+ continued_line = False
+ value += x.strip("\"")
+
+ if len(value) and value[-1] == "\\":
+ value = value[:-1]
+ continued_line = True
+ continue
+
+ root.add_kid(Node(attrib,value))
+ continue
+
+ if len(x) == 0 or x[:1] == "#":
+ continue
+
+ x = x.split("#", 1)[0]
+ xs = x.split("=", 2)
+
+ if len(xs) > 1:
+ attrib = xs[0].strip()
+ value = xs[1].strip().strip("\"")
+
+ if value != "" and value[-1] == "\\":
+ value = value[:-1]
+ continued_line = True
+ continue
+
+ root.add_kid(Node(attrib,value))
+
+ else:
+ raise ParseError("Malformed line " + str(lineno))
+
+ return root
+
+ def output(self, tree):
+ tree.output("", " = ", "")
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/helpers.py b/src/py/javatoolkit/parser/helpers.py
new file mode 100644
index 0000000..62815de
--- /dev/null
+++ b/src/py/javatoolkit/parser/helpers.py
@@ -0,0 +1,61 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+
+def expand(root, expr, realroot = None):
+ """Evaluates a path expression on a given tree.
+
+ @param root - the root of the tree
+ @param expr - the expression to resolve
+
+ @return the expanded string
+ """
+
+ if realroot == None:
+ realroot = root
+
+ expanded = ""
+ in_varref = False
+ varname = ""
+
+ for i in range(len(expr)):
+ x = expr[i]
+
+ if in_varref:
+
+ if x == "}":
+ in_varref = False
+ expanded += expand(root, realroot.find_node(varname).value, realroot)
+ varname = ""
+ elif x != "{":
+ varname += expr[i]
+
+ elif x == "$" and i < len(expr) and expr[i + 1] == "{":
+ in_varref = True
+
+ else:
+ expanded += x
+
+ return expanded
+
+def strip_varmarker(s):
+ """Strips away ${ and } in a variable expression. Idempotent if marker not found.
+
+ Example: "${foo}" -> "foo"
+ Example: "foo" -> "foo"
+ """
+
+ if s.startswith("${") and s.endswith("}"):
+ return s[2:-1]
+
+ return s
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/manifest.py b/src/py/javatoolkit/parser/manifest.py
new file mode 100644
index 0000000..810914a
--- /dev/null
+++ b/src/py/javatoolkit/parser/manifest.py
@@ -0,0 +1,61 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from tree import *
+import parser
+
+class ManifestParser(parser.Parser):
+
+ def parse(self, ins):
+ """ Parse an input stream containing a MANIFEST.MF file. Return a
+ structured document represented by tree.Node
+
+ @param ins - input stream
+ @return tree.Node containing the structured representation
+ """
+
+ lineno = 0
+ attrib = ""
+ value = ""
+ root = Node()
+
+ for x in ins.readlines():
+ lineno += 1
+
+ if len(x.strip()) == 0:
+ continue
+
+ if x[:1] == " ":
+ if attrib == "":
+ raise ParseError("Malformed line " + str(lineno))
+
+ value += x.strip()
+ continue
+
+ xs = x.split(": ", 2)
+
+ if len(xs) > 1:
+ if attrib != "":
+ root.add_kid(Node(attrib,value))
+
+ attrib = xs[0]
+ value = xs[1].strip()
+
+ else:
+ raise ParseError("Malformed line " + str(lineno))
+
+ if attrib != "":
+ root.add_kid(Node(attrib,value))
+
+ return root
+
+ def output(self, tree):
+ tree.output("", ": ", "", ",", " ");
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/parser.py b/src/py/javatoolkit/parser/parser.py
new file mode 100644
index 0000000..e99a7a1
--- /dev/null
+++ b/src/py/javatoolkit/parser/parser.py
@@ -0,0 +1,16 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+class Parser:
+ def parse(self, ins):
+ raise NotImplementedError
+ def output(self, tree):
+ raise NotImplementedError
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/tree.py b/src/py/javatoolkit/parser/tree.py
new file mode 100644
index 0000000..eb51874
--- /dev/null
+++ b/src/py/javatoolkit/parser/tree.py
@@ -0,0 +1,107 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+import sys
+
+class ParseError:
+ def __init__(self, error):
+ self.error = error
+
+class NodeIter:
+ def __init__(self, node):
+ self._node = node
+ self._index = 0
+ def next(self):
+ self._index += 1
+ if self._index >= len(self._node._kids):
+ raise StopIteration
+ return self._node._kids[self._index]
+
+class Node:
+ def __init__(self, name = None, value = None):
+ self.name = name
+ self.value = value
+ self._kids = []
+
+ def __iter__(self):
+ return NodeIter(self)
+
+ def add_kid(self, kid):
+ for x in self._kids:
+ if x.name == kid.name:
+ return
+
+ self._kids.append(kid)
+
+ def _dump_kids(self, indent, ous):
+ for x in self._kids:
+ x.dump(indent + 1)
+
+ """
+ Dump self as text to stream.
+ """
+ def dump(self, indent = 0, ous = sys.stdout):
+ if self.name:
+ ous.write((" " * indent) + self.name + " = " + self.value + "\n")
+
+ self._dump_kids(indent, ous)
+
+ """
+ Output self as text to stream using the given format.
+ """
+ def output(self, before, between, after, wrap = None, indent = "", ous = sys.stdout):
+ if self.name:
+ outval = self.value
+
+ if wrap != None:
+ outval = outval.replace(wrap, wrap + "\n" + indent)
+
+ ous.write(before + self.name + between + outval + after + "\n")
+
+ for x in self._kids:
+ x.output(before, between, after, wrap, indent, ous)
+
+ """
+ Returns a lists of all the node names.
+ """
+ def node_names(self):
+ names = []
+
+ if self.name:
+ names.append(self.name)
+
+ for x in self._kids:
+ names.extend(x.node_names())
+
+ return names
+
+ """
+ Find a given node name in a tree.
+
+ @param tree - the tree to search in
+ @param nodename - the name of the node to search for
+
+ @return reference to the found node, if any
+ """
+ def find_node(self, nodename):
+ if self.name == nodename:
+ return self
+
+ else:
+ for x in self._kids:
+ y = x.find_node(nodename)
+
+ if y != None:
+ return y
+
+ return None
+
+if __name__ == "__main__":
+ print "This is not an executable module"