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: final class SimpleSelector {
023: final String tag;
024: final String id;
025: final String clazz;
026: final String pseudo;
027:
028: SimpleSelector(final String selector) {
029: tag = getTagName(selector);
030: id = getID(selector);
031: clazz = getClass(selector);
032: pseudo = getPseudo(selector);
033: }
034:
035: public String toString() {
036: final StringBuffer result = new StringBuffer();
037: if (tag != null) {
038: result.append(tag);
039: }
040: if (id != null) {
041: result.append('#').append(id);
042: }
043: if (clazz != null) {
044: result.append('.').append(clazz);
045: }
046: if (pseudo != null) {
047: result.append(':').append(pseudo);
048: }
049: return result.toString();
050: }
051:
052: static String getTagName(final String selector) {
053: String tag = selector.split("#|\\.|:")[0];
054: return tag.length() > 0 ? tag.toLowerCase() : null;
055: }
056:
057: static String getID(final String selector) {
058: int hashIndex = selector.indexOf('#');
059: if (hashIndex < 0) {
060: return null;
061: }
062:
063: return selector.substring(hashIndex + 1).split("\\.|:")[0];
064: }
065:
066: static String getClass(final String selector) {
067: int dotIndex = selector.indexOf('.');
068: if (dotIndex < 0) {
069: return null;
070: }
071: return selector.substring(dotIndex + 1).split(":")[0];
072: }
073:
074: static String getPseudo(final String selector) {
075: int colonIndex = selector.indexOf(':');
076: if (colonIndex < 0) {
077: return null;
078: }
079: return selector.substring(colonIndex + 1)/*.split(":")[0]*/;
080: }
081:
082: public boolean matches(final SimpleSelector another) {
083: return matches(another.tag, another.id, another.clazz);
084: }
085:
086: public boolean matches(final String eName, final String eID,
087: final String eClass) {
088: if (id != null) {
089: if (eID == null) {
090: return false;
091: }
092: if (!id.equals(eID)) {
093: return false;
094: }
095: }
096:
097: if (clazz != null) {
098: if (eClass == null) {
099: return false;
100: }
101: if (!clazz.equals(eClass)) {
102: return false;
103: }
104: }
105:
106: if (tag != null) {
107: if (eName == null) {
108: return false;
109: }
110: if (!tag.equals(eName)) {
111: return false;
112: }
113: }
114:
115: return true;
116: }
117:
118: public boolean applies(final SimpleSelector another) {
119: return applies(another.tag, another.id, another.clazz);
120: }
121:
122: public boolean applies(final String eName, final String eID,
123: final String eClass) {
124: if (id != null && eID != null && !id.equals(eID)) {
125: return false;
126: }
127:
128: if (clazz != null && eClass != null && !clazz.equals(eClass)) {
129: return false;
130: }
131:
132: if (tag != null && eName != null && !tag.equals(eName)) {
133: return false;
134: }
135:
136: return true;
137: }
138: }
|