[open-ils-commits] r10553 - trunk/Open-ILS/web/js/dojo/openils
svn at svn.open-ils.org
svn at svn.open-ils.org
Sun Sep 7 11:02:34 EDT 2008
Author: erickson
Date: 2008-09-07 11:02:32 -0400 (Sun, 07 Sep 2008)
New Revision: 10553
Added:
trunk/Open-ILS/web/js/dojo/openils/MarcXPathParser.js
Log:
marc xpath parser and compiler. needs more testing
Added: trunk/Open-ILS/web/js/dojo/openils/MarcXPathParser.js
===================================================================
--- trunk/Open-ILS/web/js/dojo/openils/MarcXPathParser.js (rev 0)
+++ trunk/Open-ILS/web/js/dojo/openils/MarcXPathParser.js 2008-09-07 15:02:32 UTC (rev 10553)
@@ -0,0 +1,95 @@
+/* ---------------------------------------------------------------------------
+ * Copyright (C) 2008 Georgia Public Library Service
+ * Bill Erickson <erickson at esilibrary.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * ---------------------------------------------------------------------------
+ */
+
+if(!dojo._hasResource["openils.MarcXPathParser"]) {
+ dojo._hasResource["openils.MarcXPathParser"] = true;
+ dojo.provide("openils.MarcXPathParser");
+
+ dojo.declare('openils.MarcXPathParser', null, {
+ /**
+ * Parse a marc xpath expression and return the breakdown of tags, subfields, and match offset
+ */
+ parse : function(expr) {
+ // this is about as simple as it gets. will need more
+ // smarts if the expressions get more complicated
+ var tags = expr.match(/\d{3}/g);
+ var subfields = expr.match(/['"]([a-z]+)['"]/);
+ var offset = expr.match(/\[(\d+)\]$/);
+ return {
+ tags : tags,
+ subfields : (subfields) ? subfields[1].split('') : [],
+ offset : (offset) ? offset[1] : null
+ }
+ },
+
+ /**
+ * Creates an XPath expression from a set of tags/subfields/offset
+ */
+ compile : function(parts) {
+ var str = '//*[';
+ for(var i = 0; i < parts.tags.length; i++) {
+ var tag = parts.tags[i];
+ if(i > 0)
+ str += ' or ';
+ str += '@tag="'+tag+'"';
+ }
+ str += ']';
+ if(parts.subfields.length > 0) {
+ str += '/*[';
+ if(parts.subfields.length == 1) {
+ str += '@code="' + parts.subfields[0] + '"]';
+ } else {
+ str += 'contains("' + parts.subfields.join('') +'", at code)]';
+ }
+ }
+ if(parts.offset)
+ str += '[' + parts.offset + ']';
+ return str;
+ }
+ });
+}
+
+
+openils.MarcXPathParser.test = function() {
+ var expr = [
+ '//*[@tag="300"]/*[@code="a"][1]',
+ '//*[@tag="020"]/*[@code="a"]',
+ '//*[@tag="022"]/*[@code="a"]',
+ '//*[@tag="020" or @tag="022"]/*[@code="c"][1]',
+ '//*[@tag="001"]',
+ '//*[@tag="901"]/*[@code="a"]',
+ '//*[@tag="901"]/*[@code="b"]',
+ '//*[@tag="901"]/*[@code="c"]',
+ '//*[@tag="260"]/*[@code="b"][1]',
+ '//*[@tag="250"]/*[@code="a"][1]',
+ '//*[@tag="245"]/*[contains("abcmnopr", at code)]',
+ '//*[@tag="260"]/*[@code="c"][1]',
+ '//*[@tag="100" or @tag="110" or @tag="113"]/*[contains("ad", at code)]',
+ '//*[@tag="240"]/*[@code="l"][1]'
+ ];
+
+ var parser = new openils.MarcXPathParser();
+
+ for(var i = 0; i < expr.length; i++) {
+ var vals = parser.parse(expr[i]);
+ console.log(expr[i]);
+ console.log(vals.tags);
+ console.log(vals.subfields);
+ console.log(vals.offset);
+ console.log(parser.compile(vals));
+ }
+};
+
More information about the open-ils-commits
mailing list