KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > configuration > JavaScriptConfiguration


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.configuration;
39
40 import java.io.File JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.InputStreamReader JavaDoc;
45 import java.io.Reader JavaDoc;
46 import java.lang.reflect.Method JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Set JavaDoc;
52
53 import javax.xml.parsers.DocumentBuilder JavaDoc;
54 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.w3c.dom.Document JavaDoc;
59 import org.w3c.dom.Element JavaDoc;
60 import org.w3c.dom.Node JavaDoc;
61 import org.xml.sax.InputSource JavaDoc;
62 import org.xml.sax.SAXParseException JavaDoc;
63
64 import com.gargoylesoftware.htmlunit.Assert;
65 import com.gargoylesoftware.htmlunit.BrowserVersion;
66 import com.gargoylesoftware.htmlunit.javascript.StrictErrorHandler;
67
68 /**
69  * A container for all the javascript configuration information.
70  * TODO - Need to add the logic to support the browser and javascript conditionals in the Class elements.
71  * @version $Revision: 100 $
72  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
73  * @author Chris Erskine
74  */

75 public final class JavaScriptConfiguration {
76     private static Document JavaDoc XmlDocument_;
77
78     /** Constant indicating that this function/property is used by the specified browser version */
79     public static final int ENABLED = 1;
80
81     /** Constant indicating that this function/property is not used by the specified browser version */
82     public static final int DISABLED = 2;
83
84     /** Constant indicating that this function/property is not defined in the configuration file */
85     public static final int NOT_FOUND = 3;
86
87     private static Map JavaDoc ConfigurationMap_ = new HashMap JavaDoc(11);
88     private static HashMap JavaDoc ClassnameMap_ = new HashMap JavaDoc();
89     private static Map JavaDoc HtmlJavaScriptMap_;
90     
91     private final Map JavaDoc configuration_;
92     private final BrowserVersion browser_;
93
94
95     /**
96      * C'tor is only called from {@link #getInstance(BrowserVersion)} which is synchronized.
97      * @param browser the browser version to use
98      */

99     private JavaScriptConfiguration(final BrowserVersion browser) {
100         browser_ = browser;
101         if( XmlDocument_ == null ) {
102             loadConfiguration();
103         }
104
105         if( XmlDocument_ == null ) {
106             throw new IllegalStateException JavaDoc("Configuration was not initialized - see log for details");
107         }
108         configuration_ = buildUsageMap(browser);
109     }
110
111     /**
112      * Test for a configuration having been loaded for testing
113      *
114      * @return boolean - true if the XmlDocument has been loaded;
115      */

116     protected static boolean isDocumentLoaded() {
117         return XmlDocument_ != null;
118     }
119     
120     /**
121      * Reset the this class to it's intial state. This method is
122      * used for testing only.
123      *
124      */

125     protected static void resetClassForTesting() {
126         XmlDocument_ = null;
127         ConfigurationMap_ = new HashMap JavaDoc(11);
128     }
129
130     /**
131      * Set the document configuration for testing
132      * @param document - The configuration document
133      */

134     protected static void setXmlDocument(final Document JavaDoc document) {
135         XmlDocument_ = document;
136     }
137     
138     /**
139      * Get the configuration file and make it an input reader and then pass to the method to read the file.
140      */

141     protected static void loadConfiguration() {
142         try {
143             final Reader JavaDoc reader = getConfigurationFileAsReader();
144             if( reader == null ) {
145                 getLog().error("Unable to load JavaScriptConfiguration.xml");
146             }
147             loadConfiguration(reader);
148             reader.close();
149         }
150         catch( final Exception JavaDoc e ) {
151             getLog().error("Error when loading JavascriptConfiguration.xml", e);
152             e.printStackTrace();
153         }
154     }
155
156     
157     /**
158      * Load the configuration from a supplied Reader
159      *
160      * @param configurationReader - A reader pointing to the configuration
161      */

162     protected static void loadConfiguration(final Reader JavaDoc configurationReader) {
163         final InputSource JavaDoc inputSource = new InputSource JavaDoc(configurationReader);
164
165         try {
166             final DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
167             factory.setNamespaceAware( true );
168             factory.setValidating( false );
169
170             final DocumentBuilder JavaDoc documentBuilder = factory.newDocumentBuilder();
171             documentBuilder.setErrorHandler( new StrictErrorHandler() );
172
173             XmlDocument_ = documentBuilder.parse( inputSource );
174         }
175         catch( final SAXParseException JavaDoc parseException ) {
176             getLog().error( "line=[" + parseException.getLineNumber()
177                     + "] columnNumber=[" + parseException.getColumnNumber()
178                     + "] systemId=[" + parseException.getSystemId()
179                     + "] publicId=[" + parseException.getPublicId() + "]", parseException );
180         }
181         catch( final Exception JavaDoc e ) {
182             getLog().error("Error when loading JavascriptConfiguration.xml", e);
183         }
184     }
185
186     
187     /**
188      * Return the instance that represents the configuration for the specified {@link BrowserVersion}.
189      * This method is synchronized to allow multithreaded access to the Javascript configuration.
190      * @param browserVersion The {@link BrowserVersion}
191      * @return The instance for the specified {@link BrowserVersion}
192      */

193     public static synchronized JavaScriptConfiguration getInstance( final BrowserVersion browserVersion ) {
194         if (browserVersion == null) {
195             throw new IllegalStateException JavaDoc("BrowserVersion must be defined");
196         }
197         JavaScriptConfiguration configuration =
198             (JavaScriptConfiguration)ConfigurationMap_.get(browserVersion);
199
200         if( configuration == null ) {
201             configuration = new JavaScriptConfiguration(browserVersion);
202             ConfigurationMap_.put( browserVersion, configuration );
203         }
204         return configuration;
205     }
206
207
208     /**
209      * Return the configuration that has all entries. No constraints are put on the returned
210      * entries.
211      *
212      * @return The instance containing all entries from the configuration file.
213      */

214     static JavaScriptConfiguration getAllEntries() {
215         JavaScriptConfiguration configuration = new JavaScriptConfiguration(null);
216         return configuration;
217     }
218
219
220     
221     private static Log getLog() {
222         return LogFactory.getLog(JavaScriptConfiguration.class);
223     }
224
225     
226     private static Reader JavaDoc getConfigurationFileAsReader() {
227         final String JavaDoc fileName = "/com/gargoylesoftware/htmlunit/javascript/configuration/JavaScriptConfiguration.xml";
228         return new InputStreamReader JavaDoc(getResourceAsStream(fileName));
229     }
230
231
232     private static InputStream JavaDoc getResourceAsStream( final String JavaDoc name ) {
233         Assert.notNull("name", name);
234         InputStream JavaDoc inputStream = JavaScriptConfiguration.class.getResourceAsStream(name);
235         if( inputStream == null ) {
236             try {
237                 final String JavaDoc localizedName = name.replace( '/', File.separatorChar );
238                 inputStream = new FileInputStream JavaDoc( localizedName );
239             }
240             catch( final IOException JavaDoc e ) {
241                 // Fall through
242
}
243         }
244
245         // If we are running maven tests then the path will be slightly different
246
if( inputStream == null ) {
247             try {
248                 final String JavaDoc localizedName = ("./src/java"+name).replace( '/', File.separatorChar );
249                 inputStream = new FileInputStream JavaDoc( localizedName );
250             }
251             catch( final IOException JavaDoc e ) {
252                 // Fall through
253
}
254         }
255         return inputStream;
256     }
257     
258     /**
259      * Get the set of keys for the class configurations.
260      * @return Set
261      */

262     public Set JavaDoc keySet() {
263         return configuration_.keySet();
264     }
265     
266     private Map JavaDoc buildUsageMap(final BrowserVersion browser) {
267         final Map JavaDoc classMap = new HashMap JavaDoc(30);
268         Node JavaDoc node = XmlDocument_.getDocumentElement().getFirstChild();
269         while( node != null ) {
270             if( node instanceof Element JavaDoc ) {
271                 final Element JavaDoc element = (Element JavaDoc)node;
272                 if( element.getTagName().equals("class")) {
273                     final String JavaDoc className = element.getAttribute("name");
274                     if (!testToExcludeElement(element)) {
275                         try {
276                             final ClassConfiguration classConfiguration = parseClassElement(className, element);
277                             if (classConfiguration != null) {
278                                 classMap.put(className, classConfiguration);
279                             }
280                         }
281                         catch (final ClassNotFoundException JavaDoc e) {
282                             throw new IllegalStateException JavaDoc("The class was not found for '" + className + "'");
283                         }
284                     }
285                 }
286             }
287             node = node.getNextSibling();
288         }
289         return Collections.unmodifiableMap(classMap);
290     }
291   
292     /**
293      * Parse the class element to build the class configuration
294      * @param className The name of the class element
295      * @param element The element to parse
296      * @return ClassConfiguration
297      * @throws ClassNotFoundException If the specified class could not be found
298      */

299     private ClassConfiguration parseClassElement(final String JavaDoc className, final Element JavaDoc element)
300         throws ClassNotFoundException JavaDoc {
301         final String JavaDoc notImplemented = element.getAttribute("notImplemented");
302         if ("true".equalsIgnoreCase(notImplemented)) {
303             return null;
304         }
305         final String JavaDoc linkedClassname = element.getAttribute("classname");
306         final String JavaDoc superclassName = element.getAttribute("extends");
307         final String JavaDoc htmlClassname = element.getAttribute("htmlClass");
308         boolean jsObjectFlag = false;
309         final String JavaDoc jsObjectStr = element.getAttribute("JSObject");
310         if ("true".equalsIgnoreCase(jsObjectStr)) {
311             jsObjectFlag = true;
312         }
313         final ClassConfiguration classConfiguration =
314             new ClassConfiguration(className, linkedClassname, superclassName, htmlClassname, jsObjectFlag);
315         ClassnameMap_.put(linkedClassname, className);
316         Node JavaDoc node = element.getFirstChild();
317         while( node != null ) {
318             if( node instanceof Element JavaDoc ) {
319                 final Element JavaDoc childElement = (Element JavaDoc)node;
320                 final String JavaDoc tagName = childElement.getTagName();
321                 if( tagName.equals("property")) {
322                     parsePropertyElement(classConfiguration, childElement);
323                 }
324                 else if (tagName.equals("function")) {
325                     parseFunctionElement(classConfiguration, childElement);
326                 }
327                 else if (tagName.equals("javascript")) {
328                     getLog().debug("javascript tag not yet handled for class " + linkedClassname);
329                 }
330                 else if (tagName.equals("browser")) {
331                     getLog().debug("browser tag not yet handled for class " + linkedClassname);
332                 }
333                 else if (tagName.equals("doclink")) {
334                     // ignore this link
335
}
336                 else {
337                     throw new IllegalStateException JavaDoc("Do not understand element type '"
338                         + tagName + "' in '" + linkedClassname + "'");
339                 }
340             }
341             node = node.getNextSibling();
342         }
343         return classConfiguration;
344     }
345     
346     /**
347      * Parse out the values for the property.
348      *
349      * @param classConfiguration The configuration that is being built
350      * @param element The property element
351      */

352     private void parsePropertyElement(final ClassConfiguration classConfiguration, final Element JavaDoc element) {
353         final String JavaDoc notImplemented = element.getAttribute("notImplemented");
354         if ("true".equalsIgnoreCase(notImplemented)) {
355             return;
356         }
357         if (testToExcludeElement(element)) {
358             return;
359         }
360         final String JavaDoc propertyName = element.getAttribute("name");
361         boolean readable = false;
362         boolean writeable = false;
363         final String JavaDoc readFlag = element.getAttribute("readable");
364         if ("true".equalsIgnoreCase(readFlag)) {
365             readable = true;
366         }
367         final String JavaDoc writeFlag = element.getAttribute("writable");
368         if ("true".equalsIgnoreCase(writeFlag)) {
369             writeable = true;
370         }
371         classConfiguration.addProperty(propertyName, readable, writeable);
372     }
373     
374     /**
375      * Parse out the values from the function element.
376      *
377      * @param classConfiguration The configuration that is being built
378      * @param element The function element
379      */

380     private void parseFunctionElement(final ClassConfiguration classConfiguration, final Element JavaDoc element) {
381         final String JavaDoc notImplemented = element.getAttribute("notImplemented");
382         if ("true".equalsIgnoreCase(notImplemented)) {
383             return;
384         }
385         final String JavaDoc propertyName = element.getAttribute("name");
386         if (testToExcludeElement(element)) {
387             return;
388         }
389         classConfiguration.addFunction(propertyName);
390     }
391
392     /**
393      * Test for the browser and javascript constraints. Returns true if any constraints are present
394      * and the browser does not meet the constraints.
395      * @param element The element to scan the children of
396      * @return true to exclude this element
397      */

398     private boolean testToExcludeElement(final Element JavaDoc element) {
399         if (browser_ == null) {
400             return false;
401         }
402         Node JavaDoc node = element.getFirstChild();
403         boolean browserConstraint = false;
404         boolean allowBrowser = false;
405         boolean javascriptConstraint = false;
406         boolean allowJavascriptConstraint = false;
407         while( node != null ) {
408             if( node instanceof Element JavaDoc ) {
409                 final Element JavaDoc childElement = (Element JavaDoc)node;
410                 if( childElement.getTagName().equals("browser")) {
411                     browserConstraint = true;
412                     if (testToIncludeForBrowserConstraint(childElement, browser_)) {
413                         allowBrowser = true;
414                     }
415                 }
416                 else if( childElement.getTagName().equals("javascript")) {
417                     javascriptConstraint = true;
418                     if (testToIncludeForJSConstraint(childElement, browser_)) {
419                         allowJavascriptConstraint = true;
420                     }
421                 }
422             }
423             node = node.getNextSibling();
424         }
425         if (browserConstraint && !allowBrowser) {
426             return true;
427         }
428         if (javascriptConstraint && !allowJavascriptConstraint) {
429             return true;
430         }
431         return false;
432     }
433     
434     /**
435      * Test to see if the supplied configuration matches for the parsed configuration for the nameds class
436      * This is a method for testing.
437      *
438      * @param classname - the parsed classname to test
439      * @param config - the expected configuration
440      * @return true if they match
441      */

442     protected boolean classConfigEquals(final String JavaDoc classname, final ClassConfiguration config) {
443         final ClassConfiguration myConfig = (ClassConfiguration) configuration_.get(classname);
444         return config.equals(myConfig);
445     }
446     /**
447      * @return Returns the browser.
448      */

449     public BrowserVersion getBrowser() {
450         return browser_;
451     }
452     
453     /**
454      * Get the class configuration for the supplied javascript class name
455      * @param classname The js class name
456      * @return ClassConfiguration
457      */

458     public ClassConfiguration getClassConfiguration(final String JavaDoc classname) {
459         return (ClassConfiguration) configuration_.get(classname);
460     }
461         
462     private boolean testToIncludeForBrowserConstraint(final Element JavaDoc element, final BrowserVersion browser) {
463         if (!browser.getApplicationName().equals(element.getAttribute("name"))) {
464             return false;
465         }
466         final String JavaDoc max = element.getAttribute("max-version");
467         float maxVersion;
468         if (max.length() == 0) {
469             maxVersion = 0;
470         }
471         else {
472             maxVersion = Float.parseFloat(max);
473         }
474         if ((maxVersion > 0) && (browser.getBrowserVersionNumeric() > maxVersion)) {
475             return false;
476         }
477
478         float minVersion;
479         final String JavaDoc min = element.getAttribute("min-version");
480         if (min.length() == 0) {
481             minVersion = 0;
482         }
483         else {
484             minVersion = Float.parseFloat(min);
485         }
486         if ((minVersion > 0) && (browser.getBrowserVersionNumeric() < minVersion)) {
487             return false;
488         }
489         return true;
490     }
491     
492     
493     private boolean testToIncludeForJSConstraint(final Element JavaDoc element, final BrowserVersion browser) {
494         final String JavaDoc max = element.getAttribute("max-version");
495         float maxVersion;
496         if (max.length() == 0) {
497             maxVersion = 0;
498         }
499         else {
500             maxVersion = Float.parseFloat(max);
501         }
502         if ((maxVersion > 0) && (browser.getJavaScriptVersionNumeric() > maxVersion)) {
503             return false;
504         }
505         
506         float minVersion;
507         final String JavaDoc min = element.getAttribute("min-version");
508         if (min.length() == 0) {
509             minVersion = 0;
510         }
511         else {
512             minVersion = Float.parseFloat(min);
513         }
514         if ((minVersion > 0) && (browser.getJavaScriptVersionNumeric() < minVersion)) {
515             return false;
516         }
517         return true;
518     }
519     
520     /**
521      * Get an iterator over the keys in the configuration - For testing only
522      * @return Iterator
523      */

524     protected Iterator JavaDoc keyIterator() {
525         return configuration_.keySet().iterator();
526     }
527     
528     /**
529      * Return the class for the given class name
530      * @param classname The classname that you want the implementing class for. For testing only.
531      * @return Class
532      */

533     protected Class JavaDoc getClassObject(final String JavaDoc classname) {
534         final ClassConfiguration config = (ClassConfiguration) configuration_.get(classname);
535         return config.getLinkedClass();
536     }
537     
538     /**
539      * Get the method that implements the getter for the given property based upon the class object.
540      * @param clazz The actual class to use as reference
541      * @param propertyName The property to find the getter for
542      * @return Method
543      */

544     public Method JavaDoc getPropertyReadMethod(final Class JavaDoc clazz, final String JavaDoc propertyName) {
545         final String JavaDoc classname = getClassnameForClass(clazz);
546         return getPropertyReadMethod(classname, propertyName);
547     }
548     
549     /**
550      * Return the method that implements the get function for in the class for the given class
551      *
552      * @param classname The name of the class to work with
553      * @param propertyName The property to find the getter for
554      * @return Method
555      */

556     public Method JavaDoc getPropertyReadMethod(String JavaDoc classname, final String JavaDoc propertyName) {
557         ClassConfiguration config;
558         Method JavaDoc theMethod;
559         while (classname.length() > 0) {
560             config = (ClassConfiguration) configuration_.get(classname);
561             if (config == null) {
562                 return null;
563             }
564             theMethod = config.getPropertyReadMethod(propertyName);
565             if (theMethod != null) {
566                 return theMethod;
567             }
568             classname = config.getExtendedClass();
569         }
570         return null;
571     }
572
573     
574     private ClassConfiguration.PropertyInfo findPropertyInChain(final String JavaDoc classname, final String JavaDoc propertyName) {
575         String JavaDoc workname = classname;
576         ClassConfiguration config;
577         while (workname.length() > 0) {
578             config = (ClassConfiguration) configuration_.get(workname);
579             final ClassConfiguration.PropertyInfo info = config.getPropertyInfo(propertyName);
580             if (info != null) {
581                 return info;
582             }
583             workname = config.getExtendedClass();
584         }
585         return null;
586     }
587     /**
588      * Get the method that implements the setter for the given property based upon the class object.
589      * @param clazz The actual class to use as reference
590      * @param propertyName The property to find the getter for
591      * @return Method
592      */

593     public Method JavaDoc getPropertyWriteMethod(final Class JavaDoc clazz, final String JavaDoc propertyName) {
594         final String JavaDoc classname = getClassnameForClass(clazz);
595         return getPropertyWriteMethod(classname, propertyName);
596     }
597     
598     /**
599      * Return the method that implements the set function in the class for the given class
600      *
601      * @param classname The name of the class to work with
602      * @param propertyName The property to find the setter for
603      * @return Method
604      */

605     public Method JavaDoc getPropertyWriteMethod(String JavaDoc classname, final String JavaDoc propertyName) {
606         ClassConfiguration config;
607         Method JavaDoc theMethod;
608         while (classname.length() > 0) {
609             config = (ClassConfiguration) configuration_.get(classname);
610             theMethod = config.getPropertyWriteMethod(propertyName);
611             if (theMethod != null) {
612                 return theMethod;
613             }
614             classname = config.getExtendedClass();
615         }
616         return null;
617     }
618
619
620     /**
621      * Get the method that implements the setter for the given property based upon the class object.
622      * @param clazz The actual class to use as reference
623      * @param functionName The function to find the method for
624      * @return Method
625      */

626     public Method JavaDoc getFunctionMethod(final Class JavaDoc clazz, final String JavaDoc functionName) {
627         final String JavaDoc classname = getClassnameForClass(clazz);
628         return getFunctionMethod(classname, functionName);
629     }
630     
631     /**
632      * Return the method that implements the given function in the class for the given class
633      *
634      * @param classname The name of the class to work with
635      * @param functionName The function to find the method for
636      * @return Method
637      */

638     public Method JavaDoc getFunctionMethod(String JavaDoc classname, final String JavaDoc functionName) {
639         ClassConfiguration config;
640         Method JavaDoc theMethod;
641         while (classname.length() > 0) {
642             config = (ClassConfiguration) configuration_.get(classname);
643             theMethod = config.getFunctionMethod(functionName);
644             if (theMethod != null) {
645                 return theMethod;
646             }
647             classname = config.getExtendedClass();
648         }
649         return null;
650     }
651     
652     /**
653      * Check to see if there is an entry for the given property.
654      * @param clazz The class the property is for
655      * @param propertyName The name of the property
656      * @return boolean True if the property exists
657      */

658     public boolean propertyExists(final Class JavaDoc clazz, final String JavaDoc propertyName) {
659         final String JavaDoc classname = getClassnameForClass(clazz);
660         return propertyExists(classname, propertyName);
661     }
662
663
664     /**
665      * Check to see if there is an entry for the given property.
666      * @param classname The class the property is for
667      * @param propertyName The name of the property
668      * @return boolean True if the property exists
669      */

670     public boolean propertyExists(final String JavaDoc classname, final String JavaDoc propertyName) {
671         final ClassConfiguration.PropertyInfo info = findPropertyInChain(classname, propertyName);
672         if (info == null) {
673             return false;
674         }
675         return true;
676     }
677     
678     /**
679      * Return the classname that the given class implements. If the class is
680      * the input class, then the name is extracted from the type that the Input class
681      * is masquerading as.
682      * FIXME - Implement the Input class processing
683      * @param clazz
684      * @return the classname
685      */

686     private String JavaDoc getClassnameForClass(final Class JavaDoc clazz) {
687         String JavaDoc name = (String JavaDoc) ClassnameMap_.get(clazz.getName());
688         if (name == null) {
689             throw new IllegalStateException JavaDoc("Did not find the mapping of the class to the classname for " +
690                 clazz.getName());
691         }
692         return name;
693     }
694
695     /**
696      * Return an immutable map containing the html to javascript mappings. Keys are
697      * java classes for the various html classes (ie HtmlInput.class) and the values
698      * are the javascript class names (ie "Anchor").
699      * @return the mappings
700      */

701     public static Map JavaDoc getHtmlJavaScriptMapping() {
702         if( HtmlJavaScriptMap_ != null ) {
703             return HtmlJavaScriptMap_;
704         }
705         final JavaScriptConfiguration configuration = JavaScriptConfiguration.getAllEntries();
706         
707         final Iterator JavaDoc it = configuration.keyIterator();
708         String JavaDoc jsClassname;
709         String JavaDoc htmlClassname;
710         ClassConfiguration classConfig;
711         Class JavaDoc htmlClass;
712         final Map JavaDoc map = new HashMap JavaDoc();
713
714         while (it.hasNext()) {
715             jsClassname = (String JavaDoc) it.next();
716             classConfig = configuration.getClassConfiguration(jsClassname);
717             htmlClassname = classConfig.getHtmlClassname();
718             if (htmlClassname != null) {
719                 try {
720                     htmlClass = Class.forName(htmlClassname);
721                     // preload and validate that the class exists
722
getLog().debug("Mapping " + htmlClass.getName() + " to " + jsClassname);
723                     while (!classConfig.isJsObject()) {
724                         jsClassname = classConfig.getExtendedClass();
725                         classConfig = configuration.getClassConfiguration(jsClassname);
726                         getLog().debug(" testing to use " + jsClassname);
727                     }
728
729                     map.put( htmlClass, jsClassname );
730                 }
731                 catch( final ClassNotFoundException JavaDoc e ) {
732                     throw new NoClassDefFoundError JavaDoc(e.getMessage());
733                 }
734             }
735         }
736         HtmlJavaScriptMap_ = Collections.unmodifiableMap(map);
737         return HtmlJavaScriptMap_;
738     }
739 }
740
Popular Tags