001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Enumeration;
025: import java.util.List;
026:
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.Element;
029: import javax.swing.text.StyleConstants;
030:
031: class SelectorMatcher {
032: static List findMatching(final Enumeration ruleNames,
033: final Element element) {
034: List matched = new ArrayList();
035:
036: SimpleSelector ruleLast;
037:
038: while (ruleNames.hasMoreElements()) {
039: final String ruleName = (String) ruleNames.nextElement();
040: Selector rule = new Selector(ruleName);
041:
042: int rIndex = rule.simpleSelectors.length - 1;
043: ruleLast = rule.simpleSelectors[rIndex];
044: if (ruleLast.matches(getTagName(element), getID(element),
045: getClass(element))) {
046: boolean result = true;
047: Element current = element;
048: for (rIndex--; rIndex >= 0 && result && current != null; rIndex--) {
049:
050: ruleLast = rule.simpleSelectors[rIndex];
051: current = current.getParentElement();
052: while (current != null) {
053: if (ruleLast.matches(getTagName(current),
054: getID(current), getClass(current))) {
055: break;
056: }
057: current = current.getParentElement();
058: }
059: result = current != null;
060: }
061:
062: if (result) {
063: matched.add(rule);
064: }
065: }
066: }
067: Collections.sort(matched, SpecificityComparator.compator);
068: return matched;
069: }
070:
071: static List findMatching(final Enumeration ruleNames,
072: final String sel) {
073: final List matched = new ArrayList();
074: final Selector selector = new Selector(sel);
075:
076: SimpleSelector ruleLast;
077:
078: while (ruleNames.hasMoreElements()) {
079: final String ruleName = (String) ruleNames.nextElement();
080: Selector rule = new Selector(ruleName);
081:
082: int sIndex = selector.simpleSelectors.length - 1;
083: SimpleSelector last = selector.simpleSelectors[sIndex];
084:
085: int rIndex = rule.simpleSelectors.length - 1;
086: ruleLast = rule.simpleSelectors[rIndex];
087: if (ruleLast.matches(last.tag, last.id, last.clazz)) {
088: boolean result = true;
089: for (rIndex--; rIndex >= 0 && result; rIndex--) {
090: ruleLast = rule.simpleSelectors[rIndex];
091: result = false;
092: while (sIndex > 0 && !result) {
093: SimpleSelector current = selector.simpleSelectors[--sIndex];
094: result = ruleLast.matches(current.tag,
095: current.id, current.clazz);
096: }
097: }
098:
099: if (result) {
100: matched.add(rule);
101: }
102: }
103: }
104: Collections.sort(matched, SpecificityComparator.compator);
105: return matched;
106: }
107:
108: static String getID(final Element element) {
109: return getID(getTag(element), element);
110: }
111:
112: static String getID(final HTML.Tag tag, final Element element) {
113: if (element.isLeaf()) {
114: return getID((AttributeSet) element.getAttributes()
115: .getAttribute(tag));
116: }
117: return getID(element.getAttributes());
118: }
119:
120: static String getID(final AttributeSet attr) {
121: return attr != null ? (String) attr
122: .getAttribute(HTML.Attribute.ID) : null;
123: }
124:
125: static String getClass(final Element element) {
126: return (String) element.getAttributes().getAttribute(
127: HTML.Attribute.CLASS);
128: }
129:
130: static String getClass(final HTML.Tag tag, final Element element) {
131: if (element.isLeaf()) {
132: return getClass((AttributeSet) element.getAttributes()
133: .getAttribute(tag));
134: }
135: return getClass(element.getAttributes());
136: }
137:
138: static String getClass(final AttributeSet attr) {
139: return attr != null ? (String) attr
140: .getAttribute(HTML.Attribute.CLASS) : null;
141: }
142:
143: static HTML.Tag getTag(final Element element) {
144: return getTag(element.getAttributes());
145: }
146:
147: static HTML.Tag getTag(final AttributeSet attr) {
148: Object name = attr.getAttribute(StyleConstants.NameAttribute);
149: if (name instanceof HTML.Tag && name != HTML.Tag.CONTENT) {
150: return (HTML.Tag) name;
151: }
152:
153: Enumeration keys = attr.getAttributeNames();
154: while (keys.hasMoreElements()) {
155: Object key = keys.nextElement();
156: if (key instanceof HTML.Tag) {
157: return (HTML.Tag) key;
158: }
159: }
160: return null;
161: }
162:
163: static String getTagName(final Element element) {
164: return getTag(element).toString();
165: }
166:
167: static String getTagName(final AttributeSet attr) {
168: return getTag(attr).toString();
169: }
170: }
|