Source Code Cross Referenced for XMLPolicyCreator.java in  » Science » Cougaar12_4 » org » cougaar » mlm » plugin » ldm » 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.mlm.plugin.ldm 
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.mlm.plugin.ldm;
028:
029:        import java.util.Vector;
030:
031:        import org.cougaar.planning.ldm.PlanningFactory;
032:        import org.cougaar.planning.ldm.policy.BooleanRuleParameter;
033:        import org.cougaar.planning.ldm.policy.ClassRuleParameter;
034:        import org.cougaar.planning.ldm.policy.DoubleRuleParameter;
035:        import org.cougaar.planning.ldm.policy.EnumerationRuleParameter;
036:        import org.cougaar.planning.ldm.policy.IntegerRuleParameter;
037:        import org.cougaar.planning.ldm.policy.KeyRuleParameter;
038:        import org.cougaar.planning.ldm.policy.KeyRuleParameterEntry;
039:        import org.cougaar.planning.ldm.policy.LongRuleParameter;
040:        import org.cougaar.planning.ldm.policy.Policy;
041:        import org.cougaar.planning.ldm.policy.RangeRuleParameter;
042:        import org.cougaar.planning.ldm.policy.RangeRuleParameterEntry;
043:        import org.cougaar.planning.ldm.policy.RuleParameter;
044:        import org.cougaar.planning.ldm.policy.RuleParameterIllegalValueException;
045:        import org.cougaar.planning.ldm.policy.StringRuleParameter;
046:        import org.cougaar.util.ConfigFinder;
047:        import org.w3c.dom.Document;
048:        import org.w3c.dom.Element;
049:        import org.w3c.dom.Node;
050:        import org.w3c.dom.NodeList;
051:
052:        /** XMLPolicyCreator - creates policies from xml file
053:         *
054:         **/
055:
056:        public class XMLPolicyCreator {
057:
058:            private String xmlfilename;
059:            private Document doc;
060:            private PlanningFactory ldmf = null;
061:            private ConfigFinder configFinder;
062:
063:            public XMLPolicyCreator(String xmlfilename,
064:                    ConfigFinder configFinder) {
065:                this .xmlfilename = xmlfilename;
066:                this .configFinder = configFinder;
067:            }
068:
069:            public XMLPolicyCreator(String xmlfilename,
070:                    ConfigFinder configFinder, PlanningFactory ldmf) {
071:                this (xmlfilename, configFinder);
072:                this .ldmf = ldmf;
073:            }
074:
075:            /*
076:            public XMLPolicyCreator(PlanningFactory ldmf) {
077:              this.ldmf = ldmf;
078:            }
079:             */
080:
081:            public void setPlanningFactory(PlanningFactory ldmf) {
082:                this .ldmf = ldmf;
083:            }
084:
085:            public Policy[] getPolicies() {
086:                try {
087:                    doc = configFinder.parseXMLConfigFile(xmlfilename);
088:                    if (doc == null) {
089:                        System.err.println("XML Parser could not handle file "
090:                                + xmlfilename);
091:                        return null;
092:                    }
093:                    return parseDoc(doc);
094:                } catch (Exception e) {
095:                    e.printStackTrace();
096:                }
097:                return null;
098:            }
099:
100:            public Policy[] parseDoc(Document doc) {
101:                Element root = doc.getDocumentElement();
102:                Vector policyVector = new Vector();
103:                Policy[] pols = null;
104:                if (root.getNodeName().equals("Policies")) {
105:                    NodeList nlist = root.getChildNodes();
106:                    int nlength = nlist.getLength();
107:                    //System.out.println("There are " + nlength + " Child Nodes");
108:                    for (int i = 0; i < nlength; i++) {
109:                        Node policyNode = nlist.item(i);
110:                        if (policyNode.getNodeType() == Node.ELEMENT_NODE) {
111:                            Policy p = getPolicy(policyNode);
112:                            if (p != null) {
113:                                policyVector.addElement(p);
114:                            }
115:                        }
116:                    }
117:                    pols = new Policy[policyVector.size()];
118:                    for (int i = 0; i < policyVector.size(); i++) {
119:                        pols[i] = (Policy) policyVector.elementAt(i);
120:                    }
121:                }
122:                return pols;
123:            }
124:
125:            protected Policy createPolicy(String policyType) {
126:                Policy p = null;
127:
128:                if (ldmf != null) {
129:                    p = ldmf.newPolicy(policyType);
130:                } else {
131:                    try {
132:                        Class c = Class.forName(policyType);
133:                        Object o = c.newInstance();
134:                        p = (Policy) o;
135:                    } catch (Exception e) {
136:                        System.err.println("Couldn't instantiate policy type "
137:                                + policyType + e);
138:                        System.err
139:                                .println("Using default class org.cougaar.planning.ldm.policy.Policy");
140:                    }
141:                }
142:                if (p == null)
143:                    p = new Policy(policyType);
144:
145:                return p;
146:            }
147:
148:            public Policy getPolicy(Node policyNode) {
149:                Policy p = null;
150:
151:                if (policyNode.getNodeName().equals("Policy")) {
152:
153:                    String policyName = policyNode.getAttributes()
154:                            .getNamedItem("name").getNodeValue();
155:                    String policyType = policyNode.getAttributes()
156:                            .getNamedItem("type").getNodeValue();
157:                    //System.out.println("Creating new policy " + policyName);
158:
159:                    p = createPolicy(policyType);
160:                    p.setName(policyName);
161:
162:                    NodeList nlist = policyNode.getChildNodes();
163:                    int nlength = nlist.getLength();
164:                    //System.out.println("There are " + nlength + " Child Nodes");
165:                    for (int i = 0; i < nlength; i++) {
166:                        Node ruleParamNode = nlist.item(i);
167:                        if (ruleParamNode.getNodeType() == Node.ELEMENT_NODE) {
168:                            if (ruleParamNode.getNodeName().equals("RuleParam")) {
169:                                RuleParameter rp = parseRuleParamNode((Element) ruleParamNode);
170:                                if (rp != null)
171:                                    p.Add(rp);
172:                            } else
173:                                System.out.println(ruleParamNode.getNodeName());
174:                        }
175:                    }
176:                }
177:                return p;
178:            }
179:
180:            protected RuleParameter parseRuleParamNode(Element ruleParamNode) {
181:                RuleParameter rp = null;
182:                String paramName = ruleParamNode.getAttributes().getNamedItem(
183:                        "name").getNodeValue();
184:                NodeList nl = ruleParamNode.getChildNodes();
185:                Node child = null;
186:                for (int i = 0; i < nl.getLength(); i++) {
187:                    child = nl.item(i);
188:                    if (child.getNodeType() == Node.ELEMENT_NODE)
189:                        break;
190:                }
191:                if (child.getNodeType() != Node.ELEMENT_NODE)
192:                    return null;
193:
194:                try {
195:                    String nodeType = child.getNodeName();
196:                    //System.out.println("ParamName " + paramName + " paramType " + nodeType);
197:
198:                    if (nodeType.equals("Integer")) {
199:                        String stringval = child.getAttributes().getNamedItem(
200:                                "value").getNodeValue();
201:                        Integer val = Integer.valueOf(stringval);
202:                        stringval = child.getAttributes().getNamedItem("min")
203:                                .getNodeValue();
204:                        int min = Integer.valueOf(stringval).intValue();
205:                        stringval = child.getAttributes().getNamedItem("max")
206:                                .getNodeValue();
207:                        int max = Integer.valueOf(stringval).intValue();
208:                        IntegerRuleParameter irp = new IntegerRuleParameter(
209:                                paramName, min, max);
210:                        //System.out.println("new IntegerRuleParameter(" + paramName 
211:                        //+ ", " + min  +", " + max + ")" );
212:
213:                        try {
214:                            irp.setValue(val);
215:                        } catch (RuleParameterIllegalValueException ve) {
216:                            System.err.println(ve);
217:                        }
218:                        rp = irp;
219:
220:                    } else if (nodeType.equals("Double")) {
221:                        String stringval = child.getAttributes().getNamedItem(
222:                                "value").getNodeValue();
223:                        Double val = Double.valueOf(stringval);
224:                        stringval = child.getAttributes().getNamedItem("min")
225:                                .getNodeValue();
226:                        double min = Double.valueOf(stringval).doubleValue();
227:                        stringval = child.getAttributes().getNamedItem("max")
228:                                .getNodeValue();
229:                        double max = Double.valueOf(stringval).doubleValue();
230:                        DoubleRuleParameter drp = new DoubleRuleParameter(
231:                                paramName, min, max);
232:                        //    System.out.println("new DoubleRuleParameter(" + paramName 
233:                        // 			 + ", " + min  +", " + max + ")" );
234:
235:                        try {
236:                            drp.setValue(val);
237:                        } catch (RuleParameterIllegalValueException ve) {
238:                            System.err.println(ve);
239:                        }
240:
241:                        rp = drp;
242:                    } else if (nodeType.equals("Long")) {
243:                        String stringval = child.getAttributes().getNamedItem(
244:                                "value").getNodeValue();
245:                        Long val = Long.valueOf(stringval);
246:                        stringval = child.getAttributes().getNamedItem("min")
247:                                .getNodeValue();
248:                        long min = Long.valueOf(stringval).longValue();
249:                        stringval = child.getAttributes().getNamedItem("max")
250:                                .getNodeValue();
251:                        long max = Long.valueOf(stringval).longValue();
252:                        LongRuleParameter lrp = new LongRuleParameter(
253:                                paramName, min, max);
254:                        //    System.out.println("new LongRuleParameter(" + paramName 
255:                        // 			 + ", " + min  +", " + max + ")" );
256:
257:                        try {
258:                            lrp.setValue(val);
259:                        } catch (RuleParameterIllegalValueException ve) {
260:                            System.err.println(ve);
261:                        }
262:
263:                        rp = lrp;
264:                    } else if (nodeType.equals("String")) {
265:                        String stringval = child.getAttributes().getNamedItem(
266:                                "value").getNodeValue();
267:
268:                        StringRuleParameter srp = new StringRuleParameter(
269:                                paramName);
270:                        //       System.out.println("new StringRuleParameter(" + paramName + ")" +
271:                        // 			 "  value=" + stringval);
272:
273:                        try {
274:                            srp.setValue(stringval);
275:                        } catch (RuleParameterIllegalValueException ve) {
276:                            System.err.println(ve);
277:                        }
278:
279:                        rp = srp;
280:                    } else if (nodeType.equals("Class")) {
281:                        String interfaceType = child.getAttributes()
282:                                .getNamedItem("interface_type").getNodeValue();
283:                        String classType = child.getAttributes().getNamedItem(
284:                                "class_type").getNodeValue();
285:                        try {
286:                            Class c = Class.forName(interfaceType);
287:                            ClassRuleParameter crp = new ClassRuleParameter(
288:                                    paramName, c);
289:
290:                            c = Class.forName(classType);
291:                            crp.setValue(c);
292:                            rp = crp;
293:                        } catch (Exception e) {
294:                            System.err.println("Couldn't create class "
295:                                    + interfaceType + e);
296:                        }
297:                    } else if (nodeType.equals("Boolean")) {
298:                        String boolvalue = child.getAttributes().getNamedItem(
299:                                "value").getNodeValue();
300:                        boolvalue = boolvalue.trim();
301:                        Boolean b = null;
302:                        if (boolvalue.compareToIgnoreCase("true") == 0)
303:                            b = new Boolean(true);
304:                        else if (boolvalue.compareToIgnoreCase("false") == 0)
305:                            b = new Boolean(false);
306:
307:                        BooleanRuleParameter brp = new BooleanRuleParameter(
308:                                paramName);
309:                        if (b != null) {
310:                            try {
311:                                brp.setValue(b);
312:                            } catch (RuleParameterIllegalValueException e) {
313:                                System.err
314:                                        .println("Couldn't set value for boolean rule parameter "
315:                                                + paramName);
316:                                System.err.println(e);
317:                            }
318:                        }
319:
320:                        rp = brp;
321:
322:                    } else if (nodeType.equals("Enumeration")) {
323:                        String stringval = child.getAttributes().getNamedItem(
324:                                "value").getNodeValue();
325:
326:                        // Read the children, stuff them in an array
327:                        NodeList nlist = child.getChildNodes();
328:                        int nlength = nlist.getLength();
329:                        Vector enumOptVector = new Vector();
330:                        for (int i = 0; i < nlength; i++) {
331:                            Node enumOptionNode = nlist.item(i);
332:                            if (enumOptionNode.getNodeType() != Node.ELEMENT_NODE)
333:                                continue;
334:                            enumOptVector.addElement(enumOptionNode
335:                                    .getAttributes().getNamedItem("value")
336:                                    .getNodeValue());
337:                        }
338:
339:                        String[] enumOptions = new String[enumOptVector.size()];
340:                        for (int i = 0; i < enumOptVector.size(); i++)
341:                            enumOptions[i] = (String) enumOptVector
342:                                    .elementAt(i);
343:
344:                        EnumerationRuleParameter erp = new EnumerationRuleParameter(
345:                                paramName, enumOptions);
346:                        //       System.out.println("new EnumerationRuleParameter(" + paramName 
347:                        // 			 + enumOptions +")" );
348:
349:                        try {
350:                            erp.setValue(stringval);
351:                        } catch (RuleParameterIllegalValueException ve) {
352:                            System.err.println(ve);
353:                        }
354:
355:                        rp = erp;
356:
357:                    } else if (nodeType.equals("KeySet")) {
358:                        String default_value = child.getAttributes()
359:                                .getNamedItem("value").getNodeValue();
360:
361:                        // Read the children, stuff them in an array
362:                        NodeList nlist = child.getChildNodes();
363:                        int nlength = nlist.getLength();
364:                        Vector keyVector = new Vector();
365:                        for (int i = 0; i < nlength; i++) {
366:                            Node keyNode = nlist.item(i);
367:                            if (keyNode.getNodeType() != Node.ELEMENT_NODE)
368:                                continue;
369:                            String key = keyNode.getAttributes().getNamedItem(
370:                                    "key").getNodeValue();
371:                            String value = keyNode.getAttributes()
372:                                    .getNamedItem("value").getNodeValue();
373:                            keyVector.addElement(new KeyRuleParameterEntry(key,
374:                                    value));
375:                        }
376:                        KeyRuleParameterEntry[] keys = new KeyRuleParameterEntry[keyVector
377:                                .size()];
378:                        for (int i = 0; i < keys.length; i++)
379:                            keys[i] = (KeyRuleParameterEntry) keyVector
380:                                    .elementAt(i);
381:
382:                        KeyRuleParameter krp = new KeyRuleParameter(paramName,
383:                                keys);
384:                        try {
385:                            krp.setValue(default_value);
386:                        } catch (RuleParameterIllegalValueException ve) {
387:                            System.err.println(ve);
388:                        }
389:
390:                        rp = krp;
391:                    } else if (nodeType.equals("RangeSet")) {
392:                        String default_value = child.getAttributes()
393:                                .getNamedItem("value").getNodeValue();
394:
395:                        // Read the children, stuff them in an array
396:                        NodeList nlist = child.getChildNodes();
397:                        int nlength = nlist.getLength();
398:                        Vector rangeVector = new Vector();
399:                        for (int i = 0; i < nlength; i++) {
400:                            Node rangeNode = nlist.item(i);
401:                            if (rangeNode.getNodeType() != Node.ELEMENT_NODE)
402:                                continue;
403:                            int min = Integer.valueOf(
404:                                    rangeNode.getAttributes().getNamedItem(
405:                                            "min").getNodeValue()).intValue();
406:                            int max = Integer.valueOf(
407:                                    rangeNode.getAttributes().getNamedItem(
408:                                            "max").getNodeValue()).intValue();
409:                            NodeList valList = rangeNode.getChildNodes();
410:                            Object value = null;
411:                            for (int j = 0; j < valList.getLength(); j++) {
412:                                Node valNode = valList.item(j);
413:                                String valType = valNode.getNodeName();
414:                                switch (valNode.getNodeType()) {
415:                                case Node.ELEMENT_NODE:
416:                                    if (valType.equals("String")) {
417:                                        value = valNode.getAttributes()
418:                                                .getNamedItem("value")
419:                                                .getNodeValue();
420:                                    } else {
421:                                        value = parseRuleParamNode((Element) valNode);
422:                                    }
423:                                    break;
424:                                default:
425:                                    //System.out.println("Node type: " + valNode.getNodeType());
426:                                }
427:                            }
428:                            if (value == null) {
429:                                System.err
430:                                        .println("XMLPolicyCreator: unable to parse range entry value for "
431:                                                + paramName
432:                                                + ". Min = "
433:                                                + min
434:                                                + " , max = " + max);
435:                            } else {
436:                                rangeVector
437:                                        .addElement(new RangeRuleParameterEntry(
438:                                                value, min, max));
439:                            }
440:                        }
441:                        RangeRuleParameterEntry[] ranges = new RangeRuleParameterEntry[rangeVector
442:                                .size()];
443:                        for (int i = 0; i < ranges.length; i++)
444:                            ranges[i] = (RangeRuleParameterEntry) rangeVector
445:                                    .elementAt(i);
446:
447:                        RangeRuleParameter rrp = new RangeRuleParameter(
448:                                paramName, ranges);
449:                        try {
450:                            rrp.setValue(default_value);
451:                        } catch (RuleParameterIllegalValueException ve) {
452:                            System.err.println(ve);
453:                        }
454:
455:                        rp = rrp;
456:                    }
457:
458:                } catch (NumberFormatException nfe) {
459:                    System.err.println("Unable to parse xml for " + paramName
460:                            + " RuleParameter.");
461:                    nfe.printStackTrace();
462:
463:                    rp = null;
464:                }
465:
466:                return rp;
467:            }
468:
469:            public static void main(String[] args) {
470:                XMLPolicyCreator xmlpc = new XMLPolicyCreator(args[0],
471:                        new ConfigFinder());
472:                Policy policies[] = xmlpc.getPolicies();
473:                if (policies != null) {
474:                    System.out.println("There are " + policies.length
475:                            + " policies");
476:                    System.out.println(policies);
477:                } else
478:                    System.out.println("Couldn't parse file");
479:            }
480:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.