first-commit

This commit is contained in:
Peter 'Pita' Martischka 2011-03-26 13:10:41 +00:00
commit 325c322a27
207 changed files with 35989 additions and 0 deletions

View file

@ -0,0 +1,159 @@
LOG.inform("XMLDOC.DomReader loaded");
XMLDOC.DomReader = function(root) {
this.dom = root;
/**
* The current node the reader is on
*/
this.node = root;
/**
* Get the current node the reader is on
* @type XMLDOC.Parser.node
*/
XMLDOC.DomReader.prototype.getNode = function() {
return this.node;
};
/**
* Set the node the reader should be positioned on.
* @param node {XMLDOC.Parser.node}
*/
XMLDOC.DomReader.prototype.setNode = function(node) {
this.node = node;
};
/**
* A helper method to make sure the current node will
* never return null, unless null is passed as the root.
* @param step {String} An expression to evaluate - should return a node or null
*/
XMLDOC.DomReader.prototype.navigate = function(step) {
var n;
if ((n = step) != null)
{
this.node = n;
return this.node;
}
return null;
};
/**
* Get the root node of the current node's document.
*/
XMLDOC.DomReader.prototype.root = function() {
this.navigate(this.dom);
};
/**
* Get the parent of the current node.
*/
XMLDOC.DomReader.prototype.parent = function() {
return this.navigate(this.node.parentNode());
};
/**
* Get the first child of the current node.
*/
XMLDOC.DomReader.prototype.firstChild = function() {
return this.navigate(this.node.firstChild());
};
/**
* Get the last child of the current node.
*/
XMLDOC.DomReader.prototype.lastChild = function() {
return this.navigate(this.node.lastChild());
};
/**
* Get the next sibling of the current node.
*/
XMLDOC.DomReader.prototype.nextSibling = function() {
return this.navigate(this.node.nextSibling());
};
/**
* Get the previous sibling of the current node.
*/
XMLDOC.DomReader.prototype.prevSibling = function() {
return this.navigate(this.node.prevSibling());
};
//===============================================================================================
// Support methods
/**
* Walk the tree starting with the current node, calling the plug-in for
* each node visited. Each time the plug-in is called, the DomReader
* is passed as the only parameter. Use the {@link XMLDOC.DomReader#getNode} method
* to access the current node. <i>This method uses a depth first traversal pattern.</i>
*
* @param srcFile {String} The source file being evaluated
*/
XMLDOC.DomReader.prototype.getSymbols = function(srcFile)
{
XMLDOC.DomReader.symbols = [];
XMLDOC.DomReader.currentFile = srcFile;
JSDOC.Symbol.srcFile = (srcFile || "");
if (defined(JSDOC.PluginManager)) {
JSDOC.PluginManager.run("onDomGetSymbols", this);
}
return XMLDOC.DomReader.symbols;
};
/**
* Find the node with the given name using a depth first traversal.
* Does not modify the DomReader's current node.
*
* @param name {String} The name of the node to find
* @return the node that was found, or null if not found
*/
XMLDOC.DomReader.prototype.findNode = function(name)
{
var findNode = null;
// Start at the current node and move into the subtree,
// looking for the node with the given name
function deeper(node, find)
{
var look = null;
if (node) {
if (node.name == find)
{
return node;
}
if (node.firstChild())
{
look = deeper(node.firstChild(), find);
}
if (!look && node.nextSibling())
{
look = deeper(node.nextSibling(), find);
}
}
return look;
}
return deeper(this.getNode().firstChild(), name);
};
/**
* Find the next node with the given name using a depth first traversal.
*
* @param name {String} The name of the node to find
*/
XMLDOC.DomReader.prototype.findPreviousNode = function(name)
{
};
};

View file

@ -0,0 +1,16 @@
LOG.inform("XMLDOC.symbolize loaded");
/**
* Convert the source file to a set of symbols
*/
XMLDOC.symbolize = function(srcFile, src) {
LOG.inform("Symbolizing file '" + srcFile + "'");
// XML files already have a defined structure, so we don't need to
// do anything but parse them. The DOM reader can create a symbol
// table from the parsed XML.
var dr = new XMLDOC.DomReader(XMLDOC.Parser.parse(src));
return dr.getSymbols(srcFile);
};

View file

@ -0,0 +1,292 @@
LOG.inform("XMLDOC.Parser loaded");
/**
* XML Parser object. Returns an {@link #XMLDOC.Parser.node} which is
* the root element of the parsed document.
* <p/>
* By default, this parser will only handle well formed XML. To
* allow the parser to handle HTML, set the <tt>XMLDOC.Parser.strictMode</tt>
* variable to <tt>false</tt> before calling <tt>XMLDOC.Parser.parse()</tt>.
* <p/>
* <i>Note: If you pass poorly formed XML, it will cause the parser to throw
* an exception.</i>
*
* @author Brett Fattori (bfattori@fry.com)
* @author $Author: micmath $
* @version $Revision: 497 $
*/
XMLDOC.Parser = {};
/**
* Strict mode setting. Setting this to false allows HTML-style source to
* be parsed. Normally, well formed XML has defined end tags, or empty tags
* are properly formed. Default: <tt>true</tt>
* @type Boolean
*/
XMLDOC.Parser.strictMode = true;
/**
* A node in an XML Document. Node types are ROOT, ELEMENT, COMMENT, PI, and TEXT.
* @param parent {XMLDOC.Parser.node} The parent node
* @param name {String} The node name
* @param type {String} One of the types
*/
XMLDOC.Parser.node = function(parent, name, type)
{
this.name = name;
this.type = type || "ELEMENT";
this.parent = parent;
this.charData = "";
this.attrs = {};
this.nodes = [];
this.cPtr = 0;
XMLDOC.Parser.node.prototype.getAttributeNames = function() {
var a = [];
for (var o in this.attrs)
{
a.push(o);
}
return a;
};
XMLDOC.Parser.node.prototype.getAttribute = function(attr) {
return this.attrs[attr];
};
XMLDOC.Parser.node.prototype.setAttribute = function(attr, val) {
this.attrs[attr] = val;
};
XMLDOC.Parser.node.prototype.getChild = function(idx) {
return this.nodes[idx];
};
XMLDOC.Parser.node.prototype.parentNode = function() {
return this.parent;
};
XMLDOC.Parser.node.prototype.firstChild = function() {
return this.nodes[0];
};
XMLDOC.Parser.node.prototype.lastChild = function() {
return this.nodes[this.nodes.length - 1];
};
XMLDOC.Parser.node.prototype.nextSibling = function() {
var p = this.parent;
if (p && (p.nodes.indexOf(this) + 1 != p.nodes.length))
{
return p.getChild(p.nodes.indexOf(this) + 1);
}
return null;
};
XMLDOC.Parser.node.prototype.prevSibling = function() {
var p = this.parent;
if (p && (p.nodes.indexOf(this) - 1 >= 0))
{
return p.getChild(p.nodes.indexOf(this) - 1);
}
return null;
};
};
/**
* Parse an XML Document from the specified source. The XML should be
* well formed, unless strict mode is disabled, then the parser will
* handle HTML-style XML documents.
* @param src {String} The source to parse
*/
XMLDOC.Parser.parse = function(src)
{
var A = [];
// Normailize whitespace
A = src.split("\r\n");
src = A.join("\n");
A = src.split("\r");
src = A.join("\n");
// Remove XML and DOCTYPE specifier
src.replace(/<\?XML .*\?>/i, "");
src.replace(/<!DOCTYPE .*\>/i, "");
// The document is the root node and cannot be modified or removed
var doc = new XMLDOC.Parser.node(null, "ROOT", "DOCUMENT");
// Let's break it down
XMLDOC.Parser.eat(doc, src);
return doc;
};
/**
* The XML fragment processing routine. This method is private and should not be called
* directly.
* @param parentNode {XMLDOC.Parser.node} The node which is the parent of this fragment
* @param src {String} The source within the fragment to process
* @private
*/
XMLDOC.Parser.eat = function(parentNode, src)
{
// A simple tag def
var reTag = new RegExp("<(!|)(\\?|--|)((.|\\s)*?)\\2>","g");
// Special tag types
var reCommentTag = /<!--((.|\s)*?)-->/;
var rePITag = /<\?((.|\s)*?)\?>/;
// A start tag (with potential empty marker)
var reStartTag = /<(.*?)( +([\w_\-]*)=(\"|')(.*)\4)*(\/)?>/;
// An empty HTML style tag (not proper XML, but we'll accept it so we can process HTML)
var reHTMLEmptyTag = /<(.*?)( +([\w_\-]*)=(\"|')(.*)\4)*>/;
// Fully enclosing tag with nested tags
var reEnclosingTag = /<(.*?)( +([\w_\-]*)=(\"|')(.*?)\4)*>((.|\s)*?)<\/\1>/;
// Breaks down attributes
var reAttributes = new RegExp(" +([\\w_\\-]*)=(\"|')(.*?)\\2","g");
// Find us a tag
var tag;
while ((tag = reTag.exec(src)) != null)
{
if (tag.index > 0)
{
// The next tag has some text before it
var text = src.substring(0, tag.index).replace(/^[ \t\n]+((.|\n)*?)[ \t\n]+$/, "$1");
if (text.length > 0 && (text != "\n"))
{
var txtnode = new XMLDOC.Parser.node(parentNode, "", "TEXT");
txtnode.charData = text;
// Append the new text node
parentNode.nodes.push(txtnode);
}
// Reset the lastIndex of reTag
reTag.lastIndex -= src.substring(0, tag.index).length;
// Eat the text
src = src.substring(tag.index);
}
if (reCommentTag.test(tag[0]))
{
// Is this a comment?
var comment = new XMLDOC.Parser.node(parentNode, "", "COMMENT");
comment.charData = reCommentTag.exec(tag[0])[1];
// Append the comment
parentNode.nodes.push(comment);
// Move the lastIndex of reTag
reTag.lastIndex -= tag[0].length;
// Eat the tag
src = src.replace(reCommentTag, "");
}
else if (rePITag.test(tag[0]))
{
// Is this a processing instruction?
var pi = new XMLDOC.Parser.node(parentNode, "", "PI");
pi.charData = rePITag.exec(tag[0])[1];
// Append the processing instruction
parentNode.nodes.push(pi);
// Move the lastIndex of reTag
reTag.lastIndex -= tag[0].length;
// Eat the tag
src = src.replace(rePITag, "");
}
else if (reStartTag.test(tag[0]))
{
// Break it down
var e = reStartTag.exec(tag[0]);
var elem = new XMLDOC.Parser.node(parentNode, e[1], "ELEMENT");
// Get attributes from the tag
var a;
while ((a = reAttributes.exec(e[2])) != null )
{
elem.attrs[a[1]] = a[3];
}
// Is this an empty XML-style tag?
if (e[6] == "/")
{
// Append the empty element
parentNode.nodes.push(elem);
// Move the lastIndex of reTag (include the start tag length)
reTag.lastIndex -= e[0].length;
// Eat the tag
src = src.replace(reStartTag, "");
}
else
{
// Check for malformed XML tags
var htmlParsed = false;
var htmlStartTag = reHTMLEmptyTag.exec(src);
// See if there isn't an end tag within this block
var reHTMLEndTag = new RegExp("</" + htmlStartTag[1] + ">");
var htmlEndTag = reHTMLEndTag.exec(src);
if (XMLDOC.Parser.strictMode && htmlEndTag == null)
{
// Poorly formed XML fails in strict mode
var err = new Error("Malformed XML passed to XMLDOC.Parser... Error contains malformed 'src'");
err.src = src;
throw err;
}
else if (htmlEndTag == null)
{
// This is an HTML-style empty tag, store the element for it in non-strict mode
parentNode.nodes.push(elem);
// Eat the tag
src = src.replace(reHTMLEmptyTag, "");
htmlParsed = true;
}
// If we didn't parse HTML-style, it must be an enclosing tag
if (!htmlParsed)
{
var enc = reEnclosingTag.exec(src);
// Go deeper into the document
XMLDOC.Parser.eat(elem, enc[6]);
// Append the new element node
parentNode.nodes.push(elem);
// Eat the tag
src = src.replace(reEnclosingTag, "");
}
}
// Reset the lastIndex of reTag
reTag.lastIndex = 0;
}
}
// No tag was found... append the text if there is any
src = src.replace(/^[ \t\n]+((.|\n)*?)[ \t\n]+$/, "$1");
if (src.length > 0 && (src != "\n"))
{
var txtNode = new XMLDOC.Parser.node(parentNode, "", "TEXT");
txtNode.charData = src;
// Append the new text node
parentNode.nodes.push(txtNode);
}
};