KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Style


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import com.gargoylesoftware.htmlunit.Assert;
41 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
42
43 import java.text.MessageFormat JavaDoc;
44 import java.text.ParseException JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.SortedMap JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49 import java.util.TreeMap JavaDoc;
50 import org.mozilla.javascript.Scriptable;
51
52 /**
53  * A javascript object for a Style
54  *
55  * @version $Revision: 100 $
56  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
58  * @author Daniel Gredler
59  * @author Chris Erskine
60  */

61 public class Style extends SimpleScriptable {
62     private static final long serialVersionUID = -1976370264911039311L;
63     private static final MessageFormat JavaDoc URL_FORMAT = new MessageFormat JavaDoc("url({0})");
64     private HTMLElement jsElement_;
65
66     /**
67      * Create an instance. Javascript objects must have a default constructor.
68      */

69     public Style() {
70     }
71
72
73     /**
74      * Initialize the object
75      * @param htmlElement The element that this style describes
76      */

77     public void initialize( final HTMLElement htmlElement ) {
78         // Initialize.
79
Assert.notNull("htmlElement", htmlElement);
80         jsElement_ = htmlElement;
81         
82         if (htmlElement.getHtmlElementOrDie().getPage().getWebClient().getBrowserVersion().isIE()) {
83             // If a behavior was specified in the style, apply the behavior.
84
for (Iterator JavaDoc i = getStyleMap().entrySet().iterator(); i.hasNext();) {
85                 final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
86                 final String JavaDoc key = (String JavaDoc) entry.getKey();
87                 if ("behavior".equals(key)) {
88                     final String JavaDoc value = (String JavaDoc) entry.getValue();
89                     try {
90                         final Object JavaDoc[] url = URL_FORMAT.parse(value);
91                         if (url.length > 0) {
92                             jsElement_.jsxFunction_addBehavior((String JavaDoc) url[0]);
93                             break;
94                         }
95                     }
96                     catch (final ParseException JavaDoc e) {
97                         getLog().warn("Invalid behavior: '" + value + "'.");
98                     }
99                 }
100             }
101         }
102     }
103
104
105     /**
106      * Return the specified property or NOT_FOUND if it could not be found.
107      * @param name The name of the property
108      * @param start The scriptable object that was originally queried for this property
109      * @return The property.
110      */

111     public Object JavaDoc get( final String JavaDoc name, final Scriptable start ) {
112         final Object JavaDoc result = super.get(name, start);
113
114         // We only handle the logic here if 1) we have been fully initialized and 2) the
115
// superclass wasn't able to find anything with the matching name.
116
if( jsElement_ == null || result != NOT_FOUND ) {
117             return result;
118         }
119
120         final Object JavaDoc value = getStyleMap().get(name);
121         if( value == null ) {
122             return "";
123         }
124         else {
125             return value;
126         }
127     }
128
129
130     /**
131      * Set the specified property
132      * @param name The name of the property
133      * @param start The scriptable object that was originally invoked for this property
134      * @param newValue The new value
135      */

136     public void put( final String JavaDoc name, final Scriptable start, final Object JavaDoc newValue ) {
137         // Some calls to put will happen during the initialization of the superclass.
138
// At this point, we don't have enough information to do our own initialization
139
// so we have to just pass this call through to the superclass.
140
if( jsElement_ == null ) {
141             super.put(name, start, newValue);
142             return;
143         }
144
145         final Map JavaDoc styleMap = getStyleMap();
146         styleMap.put( name, newValue );
147
148         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
149
150         final Iterator JavaDoc iterator = styleMap.entrySet().iterator();
151         while( iterator.hasNext() ) {
152             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
153             buffer.append( entry.getKey() );
154             buffer.append( ": " );
155             buffer.append( entry.getValue() );
156             buffer.append( "; " );
157         }
158         jsElement_.getHtmlElementOrDie().setAttributeValue("style", buffer.toString());
159     }
160
161
162     private Map JavaDoc getStyleMap() {
163         // This must be a SortedMap so that the tests get results back in a defined order.
164
final SortedMap JavaDoc styleMap = new TreeMap JavaDoc();
165
166         final String JavaDoc styleAttribute = jsElement_.getHtmlElementOrDie().getAttributeValue("style");
167         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(styleAttribute, ";");
168         while( tokenizer.hasMoreTokens() ) {
169             final String JavaDoc token = tokenizer.nextToken();
170             final int index = token.indexOf(":");
171             if( index != -1 ) {
172                 final String JavaDoc key = token.substring(0,index).trim();
173                 final String JavaDoc value = token.substring(index+1).trim();
174
175                 styleMap.put( key, value );
176             }
177         }
178
179         return styleMap;
180     }
181 }
182
Popular Tags