001 /*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 *
014 * See the NOTICE file distributed with this work for additional
015 * information regarding copyright ownership.
016 */
017
018 package com.osbcp.cssparser;
019
020 import java.util.ArrayList;
021 import java.util.Iterator;
022 import java.util.List;
023
024 /**
025 * Represents a CSS rule.
026 *
027 * @author <a href="mailto:christoffer@christoffer.me">Christoffer Pettersson</a>
028 */
029
030 public final class Rule {
031
032 private List<Selector> selectors;
033 private List<PropertyValue> propertyValues;
034
035 /**
036 * Creates a rule with a single selector.
037 *
038 * @param selector A selector that the rule should initial with.
039 */
040
041 public Rule(final Selector selector) {
042 this();
043 this.selectors.add(selector);
044 }
045
046 /**
047 * Creates an empty rule.
048 */
049
050 public Rule() {
051 this(new ArrayList<Selector>());
052 }
053
054 /**
055 * Creates a new rule based on a list of selectors.
056 *
057 * @param selectors A list of selectors that the rule should initial with.
058 */
059
060 public Rule(final List<Selector> selectors) {
061 this.selectors = selectors;
062 this.propertyValues = new ArrayList<PropertyValue>();
063 }
064
065 @Override
066 public String toString() {
067
068 StringBuilder out = new StringBuilder();
069
070 // for (String selectorString : selectors) {
071 //
072 // out.append(selectorString + ",");
073 // out.append(implode(selectors) + " {\n");
074 //
075 // }
076
077 out.append(implode(selectors) + " {\n");
078 for (PropertyValue propertyValue : propertyValues) {
079 out.append("\t" + propertyValue + ";\n");
080 }
081 out.append("}\n");
082
083 return out.toString();
084 }
085
086 /**
087 * Adds a property value to the rule.
088 *
089 * @param propertyValue The property value that should be attached.
090 */
091
092 public void addPropertyValue(final PropertyValue propertyValue) {
093 propertyValues.add(propertyValue);
094 }
095
096 /**
097 * Returns a list of all property values attached to the rule.
098 *
099 * @return A list of all property values attached to the rule.
100 */
101
102 public List<PropertyValue> getPropertyValues() {
103 return propertyValues;
104 }
105
106 /**
107 * Returns a list of all selectors attached to the rule.
108 *
109 * @return A list of all selectors attached to the rule.
110 */
111
112 public List<Selector> getSelectors() {
113 return selectors;
114 }
115
116 /**
117 * Adds a list of selectors to the existing list of selectors.
118 *
119 * @param selectors A list of selectors that should be appended.
120 */
121
122 public void addSelectors(final List<Selector> selectors) {
123 this.selectors.addAll(selectors);
124 }
125
126 //
127 // @Override
128 // public boolean equals(final Object object) {
129 //
130 // if (object instanceof Rule) {
131 //
132 // Rule target = (Rule) object;
133 //
134 // return target.name.equalsIgnoreCase(name);
135 //
136 // }
137 //
138 // return false;
139 //
140 // }
141 //
142 // @Override
143 // public int hashCode() {
144 // return toString().hashCode();
145 // }
146
147 /**
148 * Implodes the list of selectors into a pretty String.
149 *
150 * @param values A list of selectors.
151 * @return A fancy String.
152 */
153
154 private String implode(final List<Selector> values) {
155
156 StringBuilder sb = new StringBuilder();
157
158 Iterator<Selector> iterator = values.iterator();
159
160 while (iterator.hasNext()) {
161
162 Selector selector = iterator.next();
163
164 sb.append(selector.toString());
165
166 if (iterator.hasNext()) {
167 sb.append(", ");
168 }
169
170 }
171
172 return sb.toString();
173
174 }
175
176 /**
177 * Removes a property value from the rule.
178 *
179 * @param propertyValue The property value that should be removed.
180 */
181
182 public void removePropertyValue(final PropertyValue propertyValue) {
183 propertyValues.remove(propertyValue);
184 }
185
186 /**
187 * Adds a selector to the rule.
188 *
189 * @param selector The selector that should be attached to the rule.
190 */
191
192 public void addSelector(final Selector selector) {
193 selectors.add(selector);
194 }
195
196 /**
197 * Removes a selector from the rule.
198 *
199 * @param selector The selector that should be removed from the rule.
200 */
201
202 public void removeSelector(final Selector selector) {
203 selectors.remove(selector);
204 }
205
206 }