KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > parser > Q_PatternLiteral


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdql.parser;
7
8
9 import java.io.PrintWriter JavaDoc;
10 import org.apache.oro.text.regex.Perl5Compiler ;
11 import com.hp.hpl.jena.graph.query.IndexValues ;
12 import com.hp.hpl.jena.graph.query.PatternLiteral;
13 import com.hp.hpl.jena.rdql.*;
14
15
16 public class Q_PatternLiteral extends ExprNode implements Expr, PatternLiteral
17 {
18     String JavaDoc patternString = null ;
19     String JavaDoc modifiers = "" ;
20     int mask ;
21
22   Q_PatternLiteral(int id) {
23     super(id);
24   }
25
26   Q_PatternLiteral(RDQLParser p, int id) {
27     super(p, id);
28   }
29
30     public void setPattern(String JavaDoc str)
31     {
32         // Unescaping done in parser for the marker - the rest is done in the regex package.
33
// Don't do it again here.
34
//patternString = Q_TextLiteral.unescape(str,'\\') ;
35
patternString = str ;
36     }
37     
38     public void setModifiers(String JavaDoc str)
39     {
40         modifiers = modifiers+str ;
41         for ( int i = 0 ; i < modifiers.length() ; i++ )
42         {
43             switch(modifiers.charAt(i))
44             {
45                 case 'i' : mask |= Perl5Compiler.CASE_INSENSITIVE_MASK; break;
46                 case 'm' : mask |= Perl5Compiler.MULTILINE_MASK; break;
47                 case 's' : mask |= Perl5Compiler.SINGLELINE_MASK; break;
48                 case 'x' : mask |= Perl5Compiler.EXTENDED_MASK; break;
49                 // Parser should catch this.
50
//default :
51
}
52         }
53     }
54
55     public String JavaDoc toString()
56     {
57         // First see if we can do it without escapes
58
if ( patternString.indexOf('/') == -1 )
59             return "/"+patternString+"/"+modifiers ;
60
61         if ( patternString.indexOf('!') == -1 )
62             return "m!"+patternString+"!"+modifiers ;
63         
64         if ( patternString.indexOf('%') == -1 )
65             return "m%"+patternString+"%"+modifiers ;
66         
67         // No - need to escape
68
char marker = '!' ;
69         return "m"+marker+quote(patternString, marker)+marker+modifiers ;
70     }
71     
72     private String JavaDoc quote(String JavaDoc pString, char marker)
73     {
74         int i = 0 ;
75         while(true)
76         {
77             int j = pString.indexOf(marker,i) ;
78             if ( j == -1 )
79                    break ;
80             pString = pString.substring(0, j)+"\\"+pString.substring(j) ;
81             // +2 because pString just got longer.
82
i = j+2 ;
83         }
84         return pString ;
85     }
86     
87     public String JavaDoc getPatternString() { return patternString ; }
88     public String JavaDoc getModifiers() { return modifiers ; }
89     
90     // -----------
91
// graph.query.Expression
92

93     public boolean isConstant() { return true; }
94     public Object JavaDoc getValue() { return toString(); } // For constants
95

96     // -----------
97

98     public void print(PrintWriter JavaDoc pw, int level)
99     {
100         pw.print(toString()) ;
101     }
102     
103     public NodeValue eval(Query q, IndexValues env)
104     {
105         throw new RDQL_InternalErrorException("Q_PatternLiteral.eval called!") ;
106         //return null ;
107
}
108     
109     public String JavaDoc asPrefixString()
110     {
111         return toString() ;
112     }
113     
114     public String JavaDoc asInfixString()
115     {
116         return toString() ;
117     }
118
119     /**
120         required for PatternLiteral interface - return the modifers
121     */

122     public String JavaDoc getPatternModifiers()
123     {
124         return modifiers == null ? "" : modifiers;
125     }
126
127     /**
128          required for PatternLiteral interface - return the pattern language
129     */

130     public String JavaDoc getPatternLanguage()
131     {
132         return PatternLiteral.rdql;
133     }
134     
135 }
136
137 /*
138  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
139  * All rights reserved.
140  *
141  * Redistribution and use in source and binary forms, with or without
142  * modification, are permitted provided that the following conditions
143  * are met:
144  * 1. Redistributions of source code must retain the above copyright
145  * notice, this list of conditions and the following disclaimer.
146  * 2. Redistributions in binary form must reproduce the above copyright
147  * notice, this list of conditions and the following disclaimer in the
148  * documentation and/or other materials provided with the distribution.
149  * 3. The name of the author may not be used to endorse or promote products
150  * derived from this software without specific prior written permission.
151  *
152  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162  */

163
Popular Tags