View Javadoc

1   /*
2    * $Header$
3    * $Revision: 1134 $
4    * $Date: 2006-04-07 10:11:52 -0700 (Fri, 07 Apr 2006) $
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id: Pattern.java 1134 2006-04-07 17:11:52Z elharo $
46   */
47  
48  package org.jaxen.pattern;
49  
50  import org.jaxen.Context;
51  import org.jaxen.JaxenException;
52  
53  /** <p><code>Pattern</code> defines the behaviour for pattern in
54    * the XSLT processing model.</p>
55    *
56    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
57    * @version $Revision: 1134 $
58    */
59  public abstract class Pattern {
60  
61      // These node numbers are compatible with both DOM and dom4j's node types
62      /** Matches Element nodes */
63      public static final short ELEMENT_NODE = 1;
64      /** Matches attribute nodes */
65      public static final short ATTRIBUTE_NODE = 2;
66      /** Matches text nodes */
67      public static final short TEXT_NODE = 3;
68      /** Matches CDATA section nodes */
69      public static final short CDATA_SECTION_NODE = 4;
70      /** Matches entity reference nodes */
71      public static final short ENTITY_REFERENCE_NODE = 5;
72      /** Matches entity nodes */
73      //public static final short ENTITY_NODE = 6;
74      /** Matches ProcessingInstruction */
75      public static final short PROCESSING_INSTRUCTION_NODE = 7;
76      /** Matches comment nodes */
77      public static final short COMMENT_NODE = 8;
78      /** Matches document nodes */
79      public static final short DOCUMENT_NODE = 9;
80      /** Matches DocumentType nodes */
81      public static final short DOCUMENT_TYPE_NODE = 10;
82      //public static final short DOCUMENT_FRAGMENT_NODE = 11;
83      //public static final short NOTATION_NODE = 12;
84      
85      /** Matches a Namespace Node */
86      // This has the same value as the DOM Level 3 XPathNamespace type
87      public static final short NAMESPACE_NODE = 13;
88      
89      /** Does not match any valid node */
90      public static final short UNKNOWN_NODE = 14;
91      
92      /** The maximum number of node types for sizing purposes */
93      public static final short MAX_NODE_TYPE = 14;
94  
95      /** Matches any node */
96      public static final short ANY_NODE = 0;
97      
98      /** Matches no nodes */
99      public static final short NO_NODE = 14;
100     
101     
102     /** 
103      * 
104      * @param node ????
105      * @param context ????
106      * @return true if the pattern matches the given node
107      * @throws JaxenException  if ????
108       */
109     public abstract boolean matches( Object node, Context context ) throws JaxenException;
110     
111     /** Returns the default resolution policy of the pattern according to the
112       * <a href="http://www.w3.org/TR/xslt#conflict">
113       * XSLT conflict resolution rules</a>.
114       *  
115      * @return 0.5; the default priority defined in XSLT
116      * 
117      * @see <a href="http://www.w3.org/TR/xslt#conflict" target="_top">Section 5.5 of the XSLT specification</a>
118       * 
119       */
120     public double getPriority() 
121     {
122         return 0.5;
123     }
124     
125     /** If this pattern is a union pattern then this
126       * method should return an array of patterns which
127       * describe the union pattern, which should contain more than one pattern.
128       * Otherwise this method should return null.
129       *
130       * @return an array of the patterns which make up this union pattern
131       * or null if this pattern is not a union pattern
132       */
133     public Pattern[] getUnionPatterns() 
134     {
135         return null;
136     }
137 
138     
139     /** 
140      * Returns the type of node the pattern matches.
141      * 
142      * @return <code>ANY_NODE</code> unless overridden
143       */
144     public short getMatchType() 
145     {
146         return ANY_NODE;
147     }
148 
149 
150     /** For patterns which only match an ATTRIBUTE_NODE or an 
151       * ELEMENT_NODE then this pattern may return the name of the
152       * element or attribute it matches. This allows a more efficient
153       * rule matching algorithm to be performed, rather than a brute 
154       * force approach of evaluating every pattern for a given Node.
155       *
156       * @return the name of the element or attribute this pattern matches
157       * or null if this pattern matches any or more than one name
158       */
159     public String getMatchesNodeName() 
160     {
161         return null;
162     }
163     
164     
165     public Pattern simplify() 
166     {
167         return this;
168     }
169     
170     /** Returns a textual representation of this pattern
171      * 
172      * @return the usual string form of this XSLT pattern
173      */
174     public abstract String getText();
175 
176 }