001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.dom;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.batik.css.engine.CSSEngine;
025: import org.apache.batik.css.engine.CSSStylableElement;
026: import org.apache.batik.css.engine.value.Value;
027: import org.w3c.dom.DOMException;
028: import org.w3c.dom.css.CSSRule;
029: import org.w3c.dom.css.CSSStyleDeclaration;
030: import org.w3c.dom.css.CSSValue;
031:
032: /**
033: * This class represents the computed style of an element.
034: *
035: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
036: * @version $Id: CSSOMComputedStyle.java 475685 2006-11-16 11:16:05Z cam $
037: */
038: public class CSSOMComputedStyle implements CSSStyleDeclaration {
039:
040: /**
041: * The CSS engine used to compute the values.
042: */
043: protected CSSEngine cssEngine;
044:
045: /**
046: * The associated element.
047: */
048: protected CSSStylableElement element;
049:
050: /**
051: * The optional pseudo-element.
052: */
053: protected String pseudoElement;
054:
055: /**
056: * The CSS values.
057: */
058: protected Map values = new HashMap();
059:
060: /**
061: * Creates a new computed style.
062: */
063: public CSSOMComputedStyle(CSSEngine e, CSSStylableElement elt,
064: String pseudoElt) {
065: cssEngine = e;
066: element = elt;
067: pseudoElement = pseudoElt;
068: }
069:
070: /**
071: * <b>DOM</b>: Implements {@link
072: * org.w3c.dom.css.CSSStyleDeclaration#getCssText()}.
073: */
074: public String getCssText() {
075: StringBuffer sb = new StringBuffer();
076: for (int i = 0; i < cssEngine.getNumberOfProperties(); i++) {
077: sb.append(cssEngine.getPropertyName(i));
078: sb.append(": ");
079: sb.append(cssEngine.getComputedStyle(element,
080: pseudoElement, i).getCssText());
081: sb.append(";\n");
082: }
083: return sb.toString();
084: }
085:
086: /**
087: * <b>DOM</b>: Implements {@link
088: * org.w3c.dom.css.CSSStyleDeclaration#setCssText(String)}.
089: * Throws a NO_MODIFICATION_ALLOWED_ERR {@link org.w3c.dom.DOMException}.
090: */
091: public void setCssText(String cssText) throws DOMException {
092: throw new DOMException(
093: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
094: }
095:
096: /**
097: * <b>DOM</b>: Implements {@link
098: * org.w3c.dom.css.CSSStyleDeclaration#getPropertyValue(String)}.
099: */
100: public String getPropertyValue(String propertyName) {
101: int idx = cssEngine.getPropertyIndex(propertyName);
102: if (idx == -1) {
103: return "";
104: }
105: Value v = cssEngine.getComputedStyle(element, pseudoElement,
106: idx);
107: return v.getCssText();
108: }
109:
110: /**
111: * <b>DOM</b>: Implements {@link
112: * org.w3c.dom.css.CSSStyleDeclaration#getPropertyCSSValue(String)}.
113: */
114: public CSSValue getPropertyCSSValue(String propertyName) {
115: CSSValue result = (CSSValue) values.get(propertyName);
116: if (result == null) {
117: int idx = cssEngine.getPropertyIndex(propertyName);
118: if (idx != -1) {
119: result = createCSSValue(idx);
120: values.put(propertyName, result);
121: }
122: }
123: return result;
124: }
125:
126: /**
127: * <b>DOM</b>: Implements {@link
128: * org.w3c.dom.css.CSSStyleDeclaration#removeProperty(String)}.
129: */
130: public String removeProperty(String propertyName)
131: throws DOMException {
132: throw new DOMException(
133: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
134: }
135:
136: /**
137: * <b>DOM</b>: Implements {@link
138: * org.w3c.dom.css.CSSStyleDeclaration#getPropertyPriority(String)}.
139: */
140: public String getPropertyPriority(String propertyName) {
141: return "";
142: }
143:
144: /**
145: * <b>DOM</b>: Implements {@link
146: * org.w3c.dom.css.CSSStyleDeclaration#setProperty(String,String,String)}.
147: */
148: public void setProperty(String propertyName, String value,
149: String prio) throws DOMException {
150: throw new DOMException(
151: DOMException.NO_MODIFICATION_ALLOWED_ERR, "");
152: }
153:
154: /**
155: * <b>DOM</b>: Implements {@link
156: * org.w3c.dom.css.CSSStyleDeclaration#getLength()}.
157: */
158: public int getLength() {
159: return cssEngine.getNumberOfProperties();
160: }
161:
162: /**
163: * <b>DOM</b>: Implements {@link
164: * org.w3c.dom.css.CSSStyleDeclaration#item(int)}.
165: */
166: public String item(int index) {
167: if (index < 0 || index >= cssEngine.getNumberOfProperties()) {
168: return "";
169: }
170: return cssEngine.getPropertyName(index);
171: }
172:
173: /**
174: * <b>DOM</b>: Implements {@link
175: * org.w3c.dom.css.CSSStyleDeclaration#getParentRule()}.
176: * @return null.
177: */
178: public CSSRule getParentRule() {
179: return null;
180: }
181:
182: /**
183: * Creates a CSSValue to manage the value at the given index.
184: */
185: protected CSSValue createCSSValue(int idx) {
186: return new ComputedCSSValue(idx);
187: }
188:
189: /**
190: * To manage a computed CSSValue.
191: */
192: public class ComputedCSSValue extends CSSOMValue implements
193: CSSOMValue.ValueProvider {
194:
195: /**
196: * The index of the associated value.
197: */
198: protected int index;
199:
200: /**
201: * Creates a new ComputedCSSValue.
202: */
203: public ComputedCSSValue(int idx) {
204: super (null);
205: valueProvider = this ;
206: index = idx;
207: }
208:
209: /**
210: * Returns the Value associated with this object.
211: */
212: public Value getValue() {
213: return cssEngine.getComputedStyle(element, pseudoElement,
214: index);
215: }
216: }
217: }
|