001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.value.Whitespace;
005: import net.sf.saxon.om.Validation;
006: import net.sf.saxon.trans.XPathException;
007: import org.xml.sax.XMLReader;
008:
009: import java.io.File;
010: import java.io.FilenameFilter;
011: import java.util.StringTokenizer;
012: import java.util.regex.Pattern;
013:
014: /**
015: * A set of query parameters on a URI passed to the collection() or document() function
016: */
017:
018: public class URIQueryParameters {
019:
020: FilenameFilter filter = null;
021: Boolean recurse = null;
022: Integer validation = null;
023: int strip = Whitespace.UNSPECIFIED;
024: Integer onError = null;
025: XMLReader parser = null;
026:
027: public static final int ON_ERROR_FAIL = 1;
028: public static final int ON_ERROR_WARNING = 2;
029: public static final int ON_ERROR_IGNORE = 3;
030:
031: public URIQueryParameters(String query, Configuration config) {
032: if (query != null) {
033: StringTokenizer t = new StringTokenizer(query, ";&");
034: while (t.hasMoreTokens()) {
035: String tok = t.nextToken();
036: int eq = tok.indexOf('=');
037: if (eq > 0 && eq < (tok.length() - 1)) {
038: String keyword = tok.substring(0, eq);
039: String value = tok.substring(eq + 1);
040:
041: if (keyword.equals("select")) {
042: String s = '^' + value + '$';
043: // replace everything that matches the regular expression \. (that is,
044: // every "." character) with the string "\."
045: s = s.replaceAll("\\.", "\\\\.");
046: // replace everything that matches the regular expression \* (that is,
047: // every "*" character) with the string ".*"
048: s = s.replaceAll("\\*", ".*");
049: Pattern pattern = Pattern.compile(s);
050: filter = new RegexFilter(pattern);
051: } else if (keyword.equals("recurse")) {
052: recurse = Boolean.valueOf("yes".equals(value));
053: } else if (keyword.equals("validation")) {
054: int v = Validation.getCode(value);
055: if (v != Validation.INVALID) {
056: validation = new Integer(v);
057: }
058: } else if (keyword.equals("strip-space")) {
059: if (value.equals("yes")) {
060: strip = Whitespace.ALL;
061: } else if (value.equals("ignorable")) {
062: strip = Whitespace.IGNORABLE;
063: } else if (value.equals("no")) {
064: strip = Whitespace.NONE;
065: }
066: } else if (keyword.equals("on-error")) {
067: if (value.equals("warning")) {
068: onError = new Integer(ON_ERROR_WARNING);
069: } else if (value.equals("ignore")) {
070: onError = new Integer(ON_ERROR_IGNORE);
071: } else if (value.equals("fail")) {
072: onError = new Integer(ON_ERROR_FAIL);
073: }
074: } else if (tok.startsWith("parser=")) {
075: String p = tok.substring(7);
076: try {
077: if (config == null) {
078: config = new Configuration();
079: }
080: parser = (XMLReader) config.getInstance(p,
081: null);
082: } catch (XPathException err) {
083: //
084: }
085: }
086: }
087: }
088: }
089:
090: }
091:
092: /**
093: * Get the value of the strip-space=yes|no parameter. Returns one of the values
094: * {@link Whitespace.ALL}, {@link Whitespace.IGNORABLE}, {@link Whitespace.NONE},
095: * {@link Whitespace.UNSPECIFIED}
096: */
097:
098: public int getStripSpace() {
099: return strip;
100: }
101:
102: /**
103: * Get the value of the validation=strict|lax|preserve|strip parameter, or null if unspecified
104: */
105:
106: public Integer getValidationMode() {
107: return validation;
108: }
109:
110: /**
111: * Get the file name filter (select=pattern), or null if unspecified
112: */
113:
114: public FilenameFilter getFilenameFilter() {
115: return filter;
116: }
117:
118: /**
119: * Get the value of the recurse=yes|no parameter, or null if unspecified
120: */
121:
122: public Boolean getRecurse() {
123: return recurse;
124: }
125:
126: /**
127: * Get the value of the on-error=fail|warning|ignore parameter, or null if unspecified
128: */
129:
130: public Integer getOnError() {
131: return onError;
132: }
133:
134: /**
135: * Get the selected XML parser, or null if unspecified
136: */
137:
138: public XMLReader getXMLReader() {
139: return parser;
140: }
141:
142: public static class RegexFilter implements FilenameFilter {
143:
144: private Pattern pattern;
145:
146: public RegexFilter(Pattern regex) {
147: this .pattern = regex;
148: }
149:
150: /**
151: * Tests if a specified file should be included in a file list.
152: *
153: * @param dir the directory in which the file was found.
154: * @param name the name of the file.
155: * @return <code>true</code> if and only if the name should be
156: * included in the file list; <code>false</code> otherwise.
157: */
158:
159: public boolean accept(File dir, String name) {
160: return new File(dir, name).isDirectory()
161: || pattern.matcher(name).matches();
162: }
163: }
164: }
165:
166: //
167: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
168: // you may not use this file except in compliance with the License. You may obtain a copy of the
169: // License at http://www.mozilla.org/MPL/
170: //
171: // Software distributed under the License is distributed on an "AS IS" basis,
172: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
173: // See the License for the specific language governing rights and limitations under the License.
174: //
175: // The Original Code is: all this file.
176: //
177: // The Initial Developer of the Original Code is Michael H. Kay.
178: //
179: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
180: //
181: // Contributor(s): none.
182: //
|