KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > definition > DefinitionResolver


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.definition;
38
39 import java.util.*;
40
41 import org.webharvest.exception.ConfigurationException;
42 import org.webharvest.exception.ErrMsg;
43
44 public class DefinitionResolver {
45     
46     public static IElementDef createElementDefinition(XmlNode node) {
47         IElementDef result;
48         
49         String JavaDoc nodeName = node.getName();
50         
51         if (nodeName.equalsIgnoreCase("empty")) {
52             validate(node, null, "id");
53             result = new EmptyDef(node);
54         } else if (nodeName.equalsIgnoreCase("text")) {
55             validate(node, null, "id");
56             result = new TextDef(node);
57         } else if (nodeName.equalsIgnoreCase("file")) {
58             validate(node, null, "id,!path,action,type,charset");
59             result = new FileDef(node);
60         } else if (nodeName.equalsIgnoreCase("var-def")) {
61             validate(node, null, "id,!name");
62             result = new VarDefDef(node);
63         } else if (nodeName.equalsIgnoreCase("var")) {
64             validate(node, "", "id,!name");
65             result = new VarDef(node);
66         } else if (nodeName.equalsIgnoreCase("http")) {
67             validate(node, null, "id,!url,method,charset,username,password");
68             result = new HttpDef(node);
69         } else if (nodeName.equalsIgnoreCase("http-param")) {
70             validate(node, null, "id,!name");
71             result = new HttpParamDef(node);
72         } else if (nodeName.equalsIgnoreCase("http-header")) {
73             validate(node, null, "id,!name");
74             result = new HttpHeaderDef(node);
75         } else if (nodeName.equalsIgnoreCase("html-to-xml")) {
76             validate(node, null, "id");
77             result = new HtmlToXmlDef(node);
78         } else if (nodeName.equalsIgnoreCase("regexp")) {
79             validate(node, "!regexp-pattern,!regexp-source,regexp-result", "id,replace,max");
80             result = new RegexpDef(node);
81         } else if (nodeName.equalsIgnoreCase("xpath")) {
82             validate(node, null, "id,!expression");
83             result = new XPathDef(node);
84         } else if (nodeName.equalsIgnoreCase("xquery")) {
85             validate(node, "xq-param,!xq-expression", "id");
86             result = new XQueryDef(node);
87         } else if (nodeName.equalsIgnoreCase("xslt")) {
88             validate(node, "!xml,!stylesheet", "id");
89             result = new XsltDef(node);
90         } else if (nodeName.equalsIgnoreCase("template")) {
91             validate(node, null, "id");
92             result = new TemplateDef(node);
93         } else if (nodeName.equalsIgnoreCase("case")) {
94             validate(node, "!if,else", "id");
95             result = new CaseDef(node);
96         } else if (nodeName.equalsIgnoreCase("loop")) {
97             validate(node, "!list,!body", "id,item,index,maxloops,filter");
98             result = new LoopDef(node);
99         } else if (nodeName.equalsIgnoreCase("while")) {
100             validate(node, null, "id,!condition,index,maxloops");
101             result = new WhileDef(node);
102         } else if (nodeName.equalsIgnoreCase("function")) {
103             validate(node, null, "id,!name");
104             result = new FunctionDef(node);
105         } else if (nodeName.equalsIgnoreCase("return")) {
106             validate(node, null, "id");
107             result = new ReturnDef(node);
108         } else if (nodeName.equalsIgnoreCase("call")) {
109             validate(node, null, "id,!name");
110             result = new CallDef(node);
111         } else if (nodeName.equalsIgnoreCase("call-param")) {
112             validate(node, null, "id,!name");
113             result = new CallParamDef(node);
114         } else if (nodeName.equalsIgnoreCase("include")) {
115             validate(node, "", "id,!path");
116             result = new IncludeDef(node);
117         } else if (nodeName.equalsIgnoreCase("try")) {
118             validate(node, "!body,!catch", "id");
119             result = new TryDef(node);
120         } else if (nodeName.equalsIgnoreCase("script")) {
121             validate(node, null, "id");
122             result = new ScriptDef(node);
123         } else {
124             throw new ConfigurationException("Unexpected configuration element: " + nodeName + "!");
125         }
126
127         return result;
128     }
129     
130     public static void validate(XmlNode node, String JavaDoc validTags, String JavaDoc validAtts) {
131         if (node == null) {
132             return;
133         }
134
135         // check element validity
136
if (validTags != null) {
137             Set validTagsSet = new HashSet();
138             
139             // collects valid elements from given validTags comma-separated list
140
StringTokenizer tokenizer = new StringTokenizer(validTags, ",");
141             while (tokenizer.hasMoreTokens()) {
142                 String JavaDoc token = tokenizer.nextToken().toLowerCase();
143                 if (token.startsWith("!")) {
144                     String JavaDoc tagName = token.substring(1);
145                     if ( node.getElement(tagName) == null ) {
146                         throw new ConfigurationException( ErrMsg.missingTag(node.getName(), tagName) );
147                     }
148                     validTagsSet.add(tagName);
149                 } else {
150                     validTagsSet.add(token);
151                 }
152             }
153             
154             // iterates through all tags and check if they are valid
155
Iterator it = node.keySet().iterator();
156             while (it.hasNext()) {
157                 String JavaDoc tagName = ((String JavaDoc) it.next()).toLowerCase();
158                 if (!validTagsSet.contains(tagName)) {
159                     throw new ConfigurationException( ErrMsg.invalidTag(node.getName(), tagName) );
160                 }
161             }
162         }
163         
164         // check attributes validity
165
if (validAtts != null) {
166             Set validAttsSet = new HashSet();
167             
168             // collects valid attributes from given validAtts comma-separated list
169
StringTokenizer tokenizer = new StringTokenizer(validAtts, ",");
170             while (tokenizer.hasMoreTokens()) {
171                 String JavaDoc token = tokenizer.nextToken().toLowerCase();
172                 if (token.startsWith("!")) {
173                     String JavaDoc attName = token.substring(1);
174                     if ( node.getAttribute(attName) == null ) {
175                         throw new ConfigurationException( ErrMsg.missingAttribute(node.getName(), attName) );
176                     }
177                     validAttsSet.add(attName);
178                 } else {
179                     validAttsSet.add(token);
180                 }
181             }
182             
183             // iterates through all attributes and check if they are valid
184
Map attributes = node.getAttributes();
185             if (attributes != null) {
186                 Iterator it = attributes.keySet().iterator();
187                 while (it.hasNext()) {
188                     String JavaDoc attName = ((String JavaDoc) it.next()).toLowerCase();
189                     if (!validAttsSet.contains(attName)) {
190                         throw new ConfigurationException( ErrMsg.invalidAttribute(node.getName(), attName) );
191                     }
192                 }
193             }
194         }
195     }
196
197 }
Popular Tags