KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > DialectBasedContentEl


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package scriptella.configuration;
18
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import scriptella.spi.DialectIdentifier;
22 import scriptella.spi.Resource;
23 import scriptella.util.StringUtils;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 /**
30  * Represents dialect based content used inside query/script/onerror elements.
31  * <p>When the DOM is traversed the internal representation model is built.
32  * This model contains enough info to speed up selection based on a requested dialect identifier.
33  * The following example demonstrates the algorithm
34  * using pseudo markup:
35  * <pre>
36  * AAA[Dialect1 111]BBB[Dialect2 222]
37  * </pre>
38  * The returned text for default Dialect is: AAABBB
39  * <br> The returned text for Dialect1 is: AAA111BBB
40  *
41  * @author Fyodor Kupolov
42  * @version 1.0
43  */

44 public class DialectBasedContentEl extends XmlConfigurableBase {
45     private List JavaDoc<Dialect> dialects;
46
47     public DialectBasedContentEl() {
48     }
49
50     public DialectBasedContentEl(final XmlElement element) {
51         configure(element);
52     }
53
54     public void configure(final XmlElement element) {
55         Dialect defaultDialect = null;
56         dialects = new ArrayList JavaDoc<Dialect>();
57         //iterate through the child nodes of this element
58
for (Node JavaDoc node = element.getElement().getFirstChild(); node != null; node = node.getNextSibling()) {
59             if (isDialectElement(node)) {
60                 if (defaultDialect != null) {
61                     dialects.add(defaultDialect);
62                     defaultDialect = null;
63                 }
64                 Dialect d = new Dialect();
65                 d.configure(new XmlElement((Element JavaDoc) node, element));
66                 dialects.add(d);
67             } else {
68                 //Try to convert the node to resource if possible
69
Resource resource = ContentEl.asResource(element, node);
70                 //If it's a text or include
71
if (resource != null) {
72                     //check if we have default dialect instance
73
if (defaultDialect == null) {
74                         //if no - create one
75
defaultDialect = new Dialect();
76                         defaultDialect.configureDefault(element);
77                     }
78                     //append a resource to default dialect
79
defaultDialect.contentEl.append(resource);
80                 }
81             }
82         }
83         if (defaultDialect != null) {
84             dialects.add(defaultDialect); //
85
}
86
87     }
88
89     private static boolean isDialectElement(Node JavaDoc node) {
90         return node instanceof Element JavaDoc && "dialect".equals(node.getNodeName());
91     }
92
93     /**
94      * This method returns content for specified dialect id or null - if script doesn't support this dialect.
95      *
96      * @param id dialect identifier. null if any dialect.
97      * @return content for specified dialect id or null - if script doesn't support this dialect.
98      */

99     public Resource getContent(final DialectIdentifier id) {
100         ContentEl result = null;
101         for (Dialect d : dialects) {
102             if (d.matches(id)) {
103                 if (result == null) {
104                     result = new ContentEl();
105                     result.setLocation(getLocation());
106                 }
107                 result.merge(d.getContentEl());
108             }
109         }
110         return result;
111     }
112
113
114     /**
115      * For testing purposes
116      *
117      * @return internal list of dialects.
118      */

119     List JavaDoc<Dialect> getDialects() {
120         return dialects;
121     }
122
123     static class Dialect extends XmlConfigurableBase {
124         private Pattern JavaDoc name;
125         private Pattern JavaDoc version;
126         private ContentEl contentEl;
127
128         public Pattern JavaDoc getName() {
129             return name;
130         }
131
132         public void setName(final Pattern JavaDoc name) {
133             this.name = name;
134         }
135
136         public Pattern JavaDoc getVersion() {
137             return version;
138         }
139
140         public void setVersion(final Pattern JavaDoc version) {
141             this.version = version;
142         }
143
144         public ContentEl getContentEl() {
145             return contentEl;
146         }
147
148         public void configure(final XmlElement element) {
149             setPatternProperty(element, "name");
150             setPatternProperty(element, "version");
151             contentEl = new ContentEl(element);
152             setLocation(element);
153         }
154
155         /**
156          * Configures default dialect.
157          *
158          * @param parent parent element.
159          */

160         public void configureDefault(final XmlElement parent) {
161             setLocation(parent);
162             contentEl = new ContentEl();
163         }
164
165
166         boolean matches(final DialectIdentifier id) {
167             if (id == null) { //if db has no dialect identifier
168
//return true only if we have no specified restrictions
169
return name == null && version == null;
170             }
171
172             String JavaDoc idName = StringUtils.nullsafeToString(id.getName());
173             String JavaDoc idVersion = StringUtils.nullsafeToString(id.getVersion());
174
175             return ((name == null) ||
176                     name.matcher(idName).matches()) && ((version == null) ||
177                     version.matcher(idVersion).matches());
178
179         }
180
181         public String JavaDoc toString() {
182             return "Dialect{" + "name=" + name + ", version=" +
183                     version + ", contentEl=" + contentEl + "}";
184         }
185     }
186
187 }
188
Popular Tags