KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > URIQueryParameters


1 package net.sf.saxon.functions;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.om.Validation;
5 import net.sf.saxon.trans.XPathException;
6 import org.xml.sax.XMLReader JavaDoc;
7
8 import java.io.File JavaDoc;
9 import java.io.FilenameFilter JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11 import java.util.regex.Pattern JavaDoc;
12
13 /**
14  * A set of query parameters on a URI passed to the collection() or document() function
15  */

16
17 public class URIQueryParameters {
18
19     FilenameFilter JavaDoc filter = null;
20     Boolean JavaDoc recurse = null;
21     Integer JavaDoc validation = null;
22     Boolean JavaDoc strip = null;
23     Integer JavaDoc onError = null;
24     XMLReader JavaDoc parser = null;
25
26     public static final int ON_ERROR_FAIL = 1;
27     public static final int ON_ERROR_WARNING = 2;
28     public static final int ON_ERROR_IGNORE = 3;
29
30     public URIQueryParameters(String JavaDoc query, Configuration config) {
31         if (query != null) {
32             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(query, ";&");
33             while (t.hasMoreTokens()) {
34                 String JavaDoc tok = t.nextToken();
35                 int eq = tok.indexOf('=');
36                 if (eq > 0 && eq < (tok.length()-1)) {
37                     String JavaDoc keyword = tok.substring(0, eq);
38                     String JavaDoc value = tok.substring(eq+1);
39
40                     if (keyword.equals("select")) {
41                         String JavaDoc s = '^' + value + '$';
42                         // replace everything that matches the regular expression \. (that is,
43
// every "." character) with the string "\."
44
s = s.replaceAll("\\.", "\\\\.");
45                         // replace everything that matches the regular expression \* (that is,
46
// every "*" character) with the string ".*"
47
s = s.replaceAll("\\*", ".*");
48                         Pattern JavaDoc pattern = Pattern.compile(s);
49                         filter = new RegexFilter(pattern);
50                     } else if (keyword.equals("recurse")) {
51                         recurse = Boolean.valueOf("yes".equals(value));
52                     } else if (keyword.equals("validation")) {
53                         int v = Validation.getCode(value);
54                         if (v != Validation.INVALID) {
55                             validation = new Integer JavaDoc(v);
56                         }
57                     } else if (keyword.equals("strip-space")) {
58                         strip = Boolean.valueOf("yes".equals(value));
59                     } else if (keyword.equals("on-error")) {
60                         if (value.equals("warning")) {
61                             onError = new Integer JavaDoc(ON_ERROR_WARNING);
62                         } else if (value.equals("ignore")) {
63                             onError = new Integer JavaDoc(ON_ERROR_IGNORE);
64                         } else if (value.equals("fail")) {
65                             onError = new Integer JavaDoc(ON_ERROR_FAIL);
66                         }
67                     } else if (tok.startsWith("parser=")) {
68                         String JavaDoc p = tok.substring(7);
69                         try {
70                             if (config == null) {
71                                 config = new Configuration();
72                             }
73                             parser = (XMLReader JavaDoc)config.getInstance(p, null);
74                         } catch (XPathException err) {
75                             //
76
}
77                     }
78                 }
79             }
80         }
81
82     }
83
84     /**
85      * Get the value of the strip-space=yes|no parameter, or null if unspecified
86      */

87
88     public Boolean JavaDoc getStripSpace() {
89         return strip;
90     }
91
92     /**
93      * Get the value of the validation=strict|lax|preserve|strip parameter, or null if unspecified
94      */

95
96     public Integer JavaDoc getValidationMode() {
97         return validation;
98     }
99
100     /**
101      * Get the file name filter (select=pattern), or null if unspecified
102      */

103
104     public FilenameFilter JavaDoc getFilenameFilter() {
105         return filter;
106     }
107
108     /**
109      * Get the value of the recurse=yes|no parameter, or null if unspecified
110      */

111
112     public Boolean JavaDoc getRecurse() {
113         return recurse;
114     }
115
116     /**
117      * Get the value of the on-error=fail|warning|ignore parameter, or null if unspecified
118      */

119
120     public Integer JavaDoc getOnError() {
121         return onError;
122     }
123
124     /**
125      * Get the selected XML parser, or null if unspecified
126      */

127
128     public XMLReader JavaDoc getXMLReader() {
129         return parser;
130     }
131
132     public static class RegexFilter implements FilenameFilter JavaDoc {
133
134         private Pattern JavaDoc pattern;
135
136         public RegexFilter(Pattern JavaDoc regex) {
137             this.pattern = regex;
138         }
139
140         /**
141          * Tests if a specified file should be included in a file list.
142          *
143          * @param dir the directory in which the file was found.
144          * @param name the name of the file.
145          * @return <code>true</code> if and only if the name should be
146          * included in the file list; <code>false</code> otherwise.
147          */

148
149         public boolean accept(File JavaDoc dir, String JavaDoc name) {
150             return new File JavaDoc(dir, name).isDirectory() || pattern.matcher(name).matches();
151         }
152     }
153 }
154
155 //
156
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
157
// you may not use this file except in compliance with the License. You may obtain a copy of the
158
// License at http://www.mozilla.org/MPL/
159
//
160
// Software distributed under the License is distributed on an "AS IS" basis,
161
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
162
// See the License for the specific language governing rights and limitations under the License.
163
//
164
// The Original Code is: all this file.
165
//
166
// The Initial Developer of the Original Code is Michael H. Kay.
167
//
168
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
169
//
170
// Contributor(s): none.
171
//
172

173
Popular Tags