Source Code Cross Referenced for XMLConfigParser.java in  » Science » Cougaar12_4 » org » cougaar » core » node » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.core.node 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 1997-2004 BBNT Solutions, LLC
005:         *  under sponsorship of the Defense Advanced Research Projects
006:         *  Agency (DARPA).
007:         * 
008:         *  You can redistribute this software and/or modify it under the
009:         *  terms of the Cougaar Open Source License as published on the
010:         *  Cougaar Open Source Website (www.cougaar.org).
011:         * 
012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023:         *  
024:         * </copyright>
025:         */
026:
027:        package org.cougaar.core.node;
028:
029:        import java.io.FileNotFoundException;
030:        import java.util.Collections;
031:        import java.util.Enumeration;
032:        import java.util.HashMap;
033:        import java.util.Map;
034:        import java.util.Properties;
035:        import org.cougaar.bootstrap.SystemProperties;
036:        import org.xml.sax.ContentHandler;
037:
038:        /**
039:         * Utility class used by the {@link
040:         * XMLComponentInitializerServiceComponent} to parse the agent
041:         * configurations.
042:         * <p>
043:         * <pre>
044:         * @property org.cougaar.node.name
045:         *   The name for this Node.
046:         *
047:         * @property org.cougaar.society.xml.validate 
048:         *    Indicates if the XML parser should be validating or not.
049:         *    Defaults to "false".
050:         * @property org.cougaar.node.validate
051:         *    Same as "-Dorg.cougaar.society.xml.validate" 
052:         * @property org.cougaar.society.xsl.checkXML
053:         *    Check for an XSL stylesheet, e.g.:
054:         *      &lt;?xml-stylesheet type="text/xml" href="society.xsl"?&gt;
055:         *    Defaults to "true".
056:         * @property org.cougaar.society.xsl.default.file
057:         *    Default XSL stylesheet if "-Dorg.cougaar.society.xsl.checkXML"
058:         *    is false or an xml-stylesheet is not found.  Defaults to
059:         *    null. 
060:         * @property org.cougaar.society.xsl.dynamic.file
061:         *    Dynamic XSL stylesheet that generates the XSL stylesheet
062:         *    based upon the XML file contents, unless an XSL stylesheet
063:         *    is specified in the XML file
064:         *    (-Dorg.cougaar.society.xsl.checkXML) or specified
065:         *    (-Dorg.cougaar.society.xsl.default.file).  Defaults to
066:         *    "make_society.xsl".
067:         * @property org.cougaar.society.xsl.param.*
068:         *    XSL parameters passed to the xml-stylesheet or default XSL
069:         *    stylesheet, where the above system property prefix is
070:         *    removed.  For example, if a system property is:
071:         *       -Dorg.cougaar.society.xsl.param.foo=bar
072:         *    then the parameter "foo=bar" will be passed to the XSL
073:         *    file's optional parameter:
074:         *       &lt;xsl:param name="foo"&gt;my_default&lt;/xsl:param&gt;
075:         * </pre>
076:         */
077:        public final class XMLConfigParser {
078:
079:            // xsl options:
080:
081:            private static final boolean VALIDATE = SystemProperties
082:                    .getBoolean("org.cougaar.society.xml.validate")
083:                    || SystemProperties
084:                            .getBoolean("org.cougaar.core.node.validate");
085:
086:            private static final boolean USE_XML_STYLESHEET = SystemProperties
087:                    .getBoolean("org.cougaar.society.xsl.checkXML", true);
088:
089:            private static final String DEFAULT_XSL_FILE_NAME = SystemProperties
090:                    .getProperty("org.cougaar.society.xsl.default.file", null);
091:
092:            private static final String DYNAMIC_XSL_FILE_NAME = SystemProperties
093:                    .getProperty("org.cougaar.society.xsl.dynamic.file",
094:                            "make_society.xsl");
095:
096:            private static final String XSL_PARAM_PROP_PREFIX = "org.cougaar.society.xsl.param.";
097:            private static final Map XSL_PARAMS = getSystemPropertiesAndTrim(XSL_PARAM_PROP_PREFIX);
098:
099:            public static final Map parseAgents(String filename,
100:                    String nodename, String agentname, Map param_overrides) {
101:
102:                XSLTransformer xslt = new XSLTransformer();
103:
104:                // set xml filename
105:                xslt.setXMLFileName(filename);
106:
107:                // set defaults
108:                xslt.setValidate(VALIDATE);
109:                xslt.setUseXMLStylesheet(USE_XML_STYLESHEET);
110:                xslt.setDefaultXSLFileName(DEFAULT_XSL_FILE_NAME);
111:                xslt.setDynamicXSLFileName(DYNAMIC_XSL_FILE_NAME);
112:
113:                // override some of the default XSL params:
114:                xslt.setDefaultXSLParams(override_params(XSL_PARAMS,
115:                        param_overrides));
116:                xslt.setDynamicXSLParams(override_params(XSL_PARAMS,
117:                        param_overrides));
118:
119:                // set our sax handler
120:                XMLConfigHandler handler = new XMLConfigHandler(nodename,
121:                        agentname);
122:                xslt.setContentHandler(handler);
123:
124:                try {
125:                    // parse, call our handler
126:                    xslt.parse();
127:                } catch (Exception e) {
128:                    String msg = "Unable to parse XML file \"" + filename
129:                            + "\"";
130:                    // look for a simple file-not-found error
131:                    FileNotFoundException fnfe = null;
132:                    for (Throwable t = e; t != null; t = t.getCause()) {
133:                        if (t instanceof  FileNotFoundException) {
134:                            fnfe = (FileNotFoundException) t;
135:                            break;
136:                        }
137:                    }
138:                    if (fnfe == null) {
139:                        // xslt error?  append extra detail
140:                        msg += ", detail=" + xslt;
141:                    } else {
142:                        // simple file-not-found
143:                        if (fnfe.getMessage() != null) {
144:                            msg += ", file not found: " + fnfe.getMessage();
145:                        }
146:                    }
147:                    throw new RuntimeException(msg, e);
148:                }
149:
150:                // ask handler for agents
151:                Map ret = handler.getAgents();
152:
153:                return ret;
154:            }
155:
156:            /**
157:             * Utility method to find all system properties with the specified
158:             * prefix and create an unmodifiable map of the trailing
159:             * "name=value" pairs.
160:             * E.g. if prefix is "a.b.", and system properties are "-Da.b.x=y"
161:             * and "-Da.b.p=q", then this method will return "{x=y, p=q}".
162:             */
163:            private static final Map getSystemPropertiesAndTrim(String prefix) {
164:                Properties props = SystemProperties
165:                        .getSystemPropertiesWithPrefix(prefix);
166:                Map ret;
167:                if (props.isEmpty()) {
168:                    ret = Collections.EMPTY_MAP;
169:                } else {
170:                    ret = new HashMap(props.size());
171:                    for (Enumeration en = props.propertyNames(); en
172:                            .hasMoreElements();) {
173:                        String name = (String) en.nextElement();
174:                        String key = name.substring(prefix.length());
175:                        String value = props.getProperty(name);
176:                        ret.put(key, value);
177:                    }
178:                    ret = Collections.unmodifiableMap(ret);
179:                }
180:                return ret;
181:            }
182:
183:            /** copy the "original" map and putAll the "overrides" */
184:            private static Map override_params(Map original, Map overrides) {
185:                if (overrides == null || overrides.isEmpty()) {
186:                    return original;
187:                }
188:                Map ret = new HashMap(original);
189:                ret.putAll(overrides);
190:                ret = Collections.unmodifiableMap(ret);
191:                return ret;
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.