1 /**
2 * JSpiff
3 * -----------------
4 * Copyright (c) 2005-2006 Emil A. Lefkof III
5 *
6 * I always give it my best shot to make a program useful and solid, but
7 * remeber that there is absolutely no warranty for using this program as
8 * stated in the following terms:
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22 package com.melloware.jspiff.jaxp;
23
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NamedNodeMap;
35 import org.xml.sax.SAXException;
36
37 /**
38 * RNameClass
39 *
40 * @since May. 19, 2002
41 * @version May. 17, 2003
42 * @author ASAMI, Tomoharu (asami@relaxer.org)
43 * @author Emil A. Lefkof III <info@melloware.com>
44 */
45 public final class RNameClass {
46 private Node pattern_;
47
48 public RNameClass(String config) {
49 try {
50 Document doc = UJAXP.getDocument(new StringReader(config));
51 pattern_ = _makePattern(doc.getDocumentElement());
52 } catch (IOException e) {
53 throw (new IllegalArgumentException());
54 } catch (SAXException e) {
55 throw (new IllegalArgumentException());
56 } catch (ParserConfigurationException e) {
57 throw (new IllegalArgumentException());
58 }
59 }
60
61 public String[] getAttributeHungry(RStack stack) {
62 Element element = stack.getContextElement();
63 NamedNodeMap attrs = element.getAttributes();
64 int size = attrs.getLength();
65 for (int i = 0; i < size; i++) {
66 Attr attr = (Attr)attrs.item(i);
67 if (stack.isConsumedAttribute(attr)) {
68 continue;
69 }
70 String attrName = attr.getName();
71 if (pattern_.eval("", attrName)) {
72 stack.consumeAttribute(attr);
73 String[] result = new String[2];
74 result[0] = attrName;
75 result[1] = element.getAttribute(attrName);
76 return (result);
77 }
78 }
79 return (null);
80 }
81
82
83
84
85
86 public boolean isMatchAttributeHungry(RStack stack) {
87 Element element = stack.getContextElement();
88 NamedNodeMap attrs = element.getAttributes();
89 int size = attrs.getLength();
90 for (int i = 0; i < size; i++) {
91 Attr attr = (Attr)attrs.item(i);
92 String attrName = attr.getName();
93 if (stack.isConsumedAttribute(attr)) {
94 continue;
95 }
96 if (pattern_.eval("", attrName)) {
97 stack.consumeAttribute(attr);
98 return (true);
99 }
100 }
101 return (false);
102 }
103
104 public boolean isMatchElement(Element element) {
105 return (pattern_.eval("", element.getTagName()));
106 }
107
108 private Node _makePattern(Element element) {
109 String tagName = element.getTagName();
110 Node node;
111 if ("anyName".equals(tagName)) {
112 node = new AnyName();
113 } else if ("nsName".equals(tagName)) {
114 node = new NsName(element.getAttribute("ns"));
115 } else if ("name".equals(tagName)) {
116 String ns = element.getAttribute("ns");
117 String name = URelaxer.getElementPropertyAsString(element);
118 node = new Name(ns, name);
119 return (node);
120 } else if ("choice".equals(tagName)) {
121 node = new Choice();
122 } else if ("except".equals(tagName)) {
123 node = new Except();
124 } else {
125 throw (new IllegalArgumentException());
126 }
127 Element[] children = URelaxer.getElements(element);
128 for (int i = 0; i < children.length; i++) {
129 node.addChild(_makePattern(children[i]));
130 }
131 return (node);
132 }
133
134 public static class AnyName
135 extends Node {
136 }
137
138 public static class Choice
139 extends Node {
140 public boolean eval(String ns, String local) {
141 int size = children_.size();
142 for (int i = 0; i < size; i++) {
143 Node node = (Node)children_.get(i);
144 if (node.eval(ns, local)) {
145 return (true);
146 }
147 }
148 return (false);
149 }
150 }
151
152 public static class Except
153 extends Node {
154 public boolean eval(String ns, String local) {
155 return (!_evalChildren(ns, local));
156 }
157 }
158
159 public static class Name
160 extends Node {
161 private String name_;
162 private String ns_;
163
164 public Name(String ns, String name) {
165 ns_ = ns;
166 name_ = name;
167 }
168
169 protected boolean _evalBody(String ns, String local) {
170 return (ns_.equals(ns) && name_.equals(local));
171 }
172 }
173
174 public static class Node {
175 protected List children_ = new ArrayList();
176
177 public void addChild(Node child) {
178 children_.add(child);
179 }
180
181 public boolean eval(String ns, String local) {
182 if (!_evalBody(ns, local)) {
183 return (false);
184 }
185 return (_evalChildren(ns, local));
186 }
187
188 protected boolean _evalBody(String ns, String local) {
189 return (true);
190 }
191
192 protected boolean _evalChildren(String ns, String local) {
193 int size = children_.size();
194 for (int i = 0; i < size; i++) {
195 Node node = (Node)children_.get(i);
196 if (!node.eval(ns, local)) {
197 return (false);
198 }
199 }
200 return (true);
201 }
202 }
203
204 public static class NsName
205 extends Node {
206 private String ns_;
207
208 public NsName(String ns) {
209 ns_ = ns;
210 }
211
212 protected boolean _evalBody(String ns, String local) {
213 return (ns_.equals(ns));
214 }
215 }
216 }