001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx.css;
006:
007: import java.io.*;
008:
009: import jaxx.compiler.*;
010: import jaxx.reflect.*;
011:
012: public class Selector implements Serializable, Comparable {
013: public static final int NEVER_APPLIES = 0;
014: public static final int PSEUDOCLASS_APPLIES_INHERIT_ONLY = 1;
015: public static final int PSEUDOCLASS_APPLIES = 2;
016: public static final int ALWAYS_APPLIES_INHERIT_ONLY = 3;
017: public static final int ALWAYS_APPLIES = 4;
018:
019: private String javaClassName;
020: private String styleClass;
021: private String pseudoClass;
022: private String id;
023: private boolean inline;
024:
025: public Selector(String javaClassName, String styleClass,
026: String pseudoClass, String id) {
027: this (javaClassName, styleClass, pseudoClass, id, false);
028: }
029:
030: public Selector(String javaClassName, String styleClass,
031: String pseudoClass, String id, boolean inline) {
032: this .javaClassName = javaClassName;
033: this .styleClass = styleClass;
034: this .pseudoClass = pseudoClass;
035: this .id = id;
036: this .inline = inline;
037: }
038:
039: public int appliesTo(CompiledObject object) {
040: boolean inheritOnly = false;
041: CompiledObject parent = object;
042: while (parent != null) {
043: boolean classMatch = (javaClassName == null);
044: if (!classMatch) {
045: ClassDescriptor javaClass = parent.getObjectClass();
046: do {
047: String name = javaClass.getName();
048: if (name.equals(javaClassName)
049: || name
050: .substring(
051: name.lastIndexOf(".") + 1)
052: .equals(javaClassName)) {
053: classMatch = true;
054: break;
055: }
056: javaClass = javaClass.getSuperclass();
057: } while (javaClass != null);
058: }
059:
060: boolean styleClassMatch = (styleClass == null || styleClass
061: .equals(parent.getStyleClass()));
062:
063: String objectId = parent.getId();
064: objectId = objectId
065: .substring(objectId.lastIndexOf(".") + 1);
066: boolean idMatch = (id == null || (' ' + objectId + ' ')
067: .indexOf(' ' + id + ' ') > -1);
068:
069: if (classMatch && styleClassMatch && idMatch) {
070: if (pseudoClass != null)
071: return inheritOnly ? PSEUDOCLASS_APPLIES_INHERIT_ONLY
072: : PSEUDOCLASS_APPLIES;
073: else
074: return inheritOnly ? ALWAYS_APPLIES_INHERIT_ONLY
075: : ALWAYS_APPLIES;
076: }
077:
078: parent = parent.getParent();
079: inheritOnly = true;
080: }
081: return NEVER_APPLIES;
082: }
083:
084: public String getJavaClassName() {
085: return javaClassName;
086: }
087:
088: public String getStyleClass() {
089: return styleClass;
090: }
091:
092: public String getPseudoClass() {
093: return pseudoClass;
094: }
095:
096: public String getId() {
097: return id;
098: }
099:
100: public boolean isInline() {
101: return inline;
102: }
103:
104: public int compareTo(Object o) {
105: Selector selector = (Selector) o;
106: if (inline && !selector.inline)
107: return 1;
108: if (!inline && selector.inline)
109: return -1;
110: if (pseudoClass != null && selector.pseudoClass == null)
111: return 1;
112: if (pseudoClass == null && selector.pseudoClass != null)
113: return -1;
114: if (id != null && selector.id == null)
115: return 1;
116: if (id == null && selector.id != null)
117: return -1;
118: if (styleClass != null && selector.styleClass == null)
119: return 1;
120: if (styleClass == null && selector.styleClass != null)
121: return -1;
122: if (javaClassName != null && selector.javaClassName == null)
123: return 1;
124: if (javaClassName == null && selector.javaClassName != null)
125: return -1;
126: return 0;
127: }
128:
129: public String toString() {
130: return "Selector[" + javaClassName + ", " + styleClass + ", "
131: + pseudoClass + ", " + id + "]";
132: }
133: }
|