KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > runtime > Selection


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.runtime;
24
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.xerces.impl.xpath.regex.RegularExpression;
30 import org.xquark.extractor.common.Constants;
31
32 /**
33  * Path tree loaded from configuration file featuring a filter on database
34  * objects allowing to narrow objects seen by Extractor.
35  */

36 // QUESTION : Why XTreeNode not used ?
37
public class Selection implements Constants {
38     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41     private static RegularExpression regexNCName =
42         new RegularExpression("[\\i-[:]][\\c-[:]]*", "X");
43     private static RegularExpression regexEntry =
44         new RegularExpression("", "X");
45
46     private byte type = NONE;
47     private String JavaDoc name = null;
48     private boolean nameIsRegex = false;
49     // private RegularExpression regExpr = null;
50
private String JavaDoc targetNamespace = null;
51     private HashMap JavaDoc map = null;
52     private String JavaDoc alias = null;
53     private int elementFormDefault = UNQUALIFIED;
54
55     /* for a column element, pkColumn values true means that this column takes
56      * part in primary key
57      */

58     private boolean pkColumn = false;
59
60
61     public Selection(
62         String JavaDoc name,
63         String JavaDoc targetNamespace,
64         byte type,
65         HashMap JavaDoc map) {
66         this.name = name;
67         this.nameIsRegex = false;
68         this.targetNamespace = targetNamespace;
69         this.type = type;
70         this.map = map;
71     }
72
73     public Selection(
74         String JavaDoc name,
75         boolean nameIsRegex,
76         String JavaDoc targetNamespace,
77         byte type,
78         HashMap JavaDoc map) {
79         this.name = name;
80         this.nameIsRegex = nameIsRegex;
81         this.targetNamespace = targetNamespace;
82         this.type = type;
83         this.map = map;
84     }
85
86     public String JavaDoc getName() {
87         return name;
88     }
89     
90     public void setNameForDefault(String JavaDoc name) {
91         if (this.name == null)
92             this.name = name;
93     }
94     
95     public String JavaDoc getTargetNamespace() {
96         return targetNamespace;
97     }
98     public byte getType() {
99         return type;
100     }
101     public HashMap JavaDoc getMap() {
102         return map;
103     }
104
105     public void setElementFormDefault(int elementFormDefault) {
106         this.elementFormDefault = elementFormDefault;
107     }
108
109     public int getElementFormDefault() {
110         return elementFormDefault;
111     }
112
113     public void addToMap(String JavaDoc name, Selection selection) {
114         if (map == null)
115             map = new HashMap JavaDoc();
116         map.put(name, selection);
117     }
118
119     public void setAlias(String JavaDoc alias) {
120         this.alias = alias;
121     }
122
123     public String JavaDoc getAlias() {
124         return this.alias;
125     }
126
127     // public void setRegularExpression(String regularExpression) {
128
// this.regExpr = new RegularExpression(regularExpression);
129
// }
130

131     // public boolean isSelected(String subElementName) {
132
// boolean retVal = false;
133
// retVal = (null == includesSubElement(subElementName));
134
// if (!retVal &&
135
// null != subElementName
136
// && null != regExpr) {
137
// retVal = regExpr.matches(subElementName);
138
// }
139
// return retVal;
140
// }
141

142     public boolean noSubElementDef() {
143         return (null == map || map.isEmpty());
144     }
145
146     public Selection includesSubElement(String JavaDoc str) {
147         if (map == null || map.isEmpty())
148             return null;
149         if (null == str) {
150             return (Selection) this.map.get(null);
151         }
152         // if (!verifyName(str)) return null;
153
Collection JavaDoc values = map.values();
154         Iterator JavaDoc it = values.iterator();
155         Selection retSelection = null;
156         while (it.hasNext()) {
157             Selection value = (Selection) it.next();
158             // TODO: explodes if schema element contains nothing... <=> all tables ? Add a constraint in schema ? A default include all ?
159
if (value == null || value.getName() == null)
160                 continue;
161             // TODO: Store regular expression statically
162
regexEntry.setPattern(value.getName());
163             if (regexEntry.matches(str)) {
164                 if (value.getType() == Selection.EXCLUDES)
165                     return null;
166                 else if (value.getType() == Selection.INCLUDES)
167                     if (retSelection == null)
168                         retSelection = value;
169             }
170         }
171         return retSelection;
172     }
173
174     public void setPKColumn(boolean pkColumn) {
175         this.pkColumn = pkColumn;
176     }
177
178     public boolean getPKColumn() {
179         return this.pkColumn;
180     }
181
182     static public boolean verifyName(String JavaDoc name) {
183         return regexNCName.matches(name);
184     }
185
186     /*
187     private boolean includesElement(String str) {
188         if (type != INCLUDES) return false;
189         if (name != null && name.equals(str)) return true;
190         return false;
191     }
192     */

193
194     public String JavaDoc print() {
195         return print(0);
196     }
197     
198     public String JavaDoc print(int tab) {
199         StringBuffer JavaDoc bufTab = new StringBuffer JavaDoc();
200         for (int i=0;i<tab;i++)
201             bufTab.append("\t");
202         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
203         buf.append("\n");
204         buf.append(bufTab.toString());
205         buf.append("Name : ");
206         buf.append(name);
207         buf.append("\n");
208         buf.append(bufTab.toString());
209         buf.append("TargetNamespace : ");
210         buf.append(targetNamespace);
211         buf.append("\n");
212         buf.append(bufTab.toString());
213         buf.append("Type : ");
214         buf.append(type);
215         if (map != null) {
216             Collection JavaDoc col = map.values();
217             Iterator JavaDoc it = col.iterator();
218             while (it.hasNext()) {
219                 buf.append("\n");
220                 buf.append(bufTab.toString());
221                 buf.append("Map entry -->");
222                 buf.append(((Selection) it.next()).print(tab+1));
223             }
224         }
225         return buf.toString();
226     }
227
228     public String JavaDoc toString() {
229         return print();
230     }
231 }
232
Popular Tags