KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > pattern > IDPattern


1 package com.icl.saxon.pattern;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.DocumentInfo;
5 import com.icl.saxon.expr.XPathException;
6
7 import java.util.StringTokenizer JavaDoc;
8
9 /**
10 * An IDPattern is a pattern of the form id(literal)
11 */

12
13 public final class IDPattern extends Pattern {
14
15     private String JavaDoc id; // the id value supplied
16
private boolean containsSpaces;
17
18     public IDPattern(String JavaDoc idvalue) {
19         id = idvalue;
20         containsSpaces =
21                 (id.indexOf(' ') >= 0 ||
22                 id.indexOf(0x09) >= 0 ||
23                 id.indexOf(0x0a) >= 0 ||
24                 id.indexOf(0x0c) >= 0);
25     }
26
27     /**
28     * Determine whether this Pattern matches the given Node
29     * @param e The NodeInfo representing the Element or other node to be tested against the Pattern
30     * @return true if the node matches the Pattern, false otherwise
31     */

32
33     public boolean matches(NodeInfo e, Context c) throws XPathException {
34         if (e.getNodeType() != NodeInfo.ELEMENT) return false;
35         DocumentInfo doc = e.getDocumentRoot();
36         if (!containsSpaces) {
37             NodeInfo element = doc.selectID(id);
38             if (element==null) return false;
39             return (element.isSameNode(e));
40         } else {
41             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(id);
42             while (tokenizer.hasMoreElements()) {
43                 String JavaDoc idv = (String JavaDoc)tokenizer.nextElement();
44                 NodeInfo element = doc.selectID(idv);
45                 if (element != null && e.isSameNode(element)) {
46                     return true;
47                 }
48             }
49             return false;
50         }
51     }
52
53     /**
54     * Determine the type of nodes to which this pattern applies.
55     * @return NodeInfo.ELEMENT
56     */

57
58     public short getNodeType() {
59         return NodeInfo.ELEMENT;
60     }
61
62 }
63
64 //
65
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
66
// you may not use this file except in compliance with the License. You may obtain a copy of the
67
// License at http://www.mozilla.org/MPL/
68
//
69
// Software distributed under the License is distributed on an "AS IS" basis,
70
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
71
// See the License for the specific language governing rights and limitations under the License.
72
//
73
// The Original Code is: all this file.
74
//
75
// The Initial Developer of the Original Code is
76
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
77
//
78
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
79
//
80
// Contributor(s): none.
81
//
82
Popular Tags