KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > systemoptions > ContentProcessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.upgrade.systemoptions;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 class ContentProcessor {
29     private static Map JavaDoc<String JavaDoc, ContentProcessor> clsname2Delegate = new HashMap JavaDoc<String JavaDoc, ContentProcessor>();
30     protected String JavaDoc systemOptionInstanceName;
31     
32     static {
33         registerContentProcessor(new JUnitContentProcessor("org.netbeans.modules.junit.JUnitSettings"));//NOI18N
34
}
35     
36     private static void registerContentProcessor(ContentProcessor instance) {
37         if (clsname2Delegate.put(instance.systemOptionInstanceName, instance) != null) {
38             throw new IllegalArgumentException JavaDoc();
39         }
40     }
41         
42             
43     protected ContentProcessor(String JavaDoc systemOptionInstanceName) {
44         this.systemOptionInstanceName = systemOptionInstanceName;
45     }
46             
47     protected Result parseContent(final Iterator JavaDoc<Object JavaDoc> it, boolean types) {
48         Map JavaDoc<String JavaDoc, String JavaDoc> m;
49         Result result = null;
50         try {
51             Map JavaDoc<String JavaDoc, Object JavaDoc> props = parseProperties(it);
52             assert props != null;
53             //debugInfo("before: ", m);
54
m = processProperties(props, types);
55             //assert debugInfo("after: ", m);
56
result = new DefaultResult(systemOptionInstanceName, m);
57         } catch (IllegalStateException JavaDoc isx) {
58             Logger.getLogger(ContentProcessor.class.getName()).log(Level.WARNING, systemOptionInstanceName + " not parsed", isx);
59         }
60         return result;
61     }
62     
63     static Result parseContent(String JavaDoc systemOptionInstanceName, boolean types, final Iterator JavaDoc<Object JavaDoc> it) {
64         ContentProcessor cp = clsname2Delegate.get(systemOptionInstanceName);
65         if (cp == null) {
66             cp = new ContentProcessor(systemOptionInstanceName);
67         }
68         return cp.parseContent(it, types);
69     }
70     
71     private final Map JavaDoc<String JavaDoc, String JavaDoc> processProperties(final Map JavaDoc<String JavaDoc, Object JavaDoc> properties, boolean types) {
72         Map JavaDoc<String JavaDoc, String JavaDoc> allProps = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
73         for (Iterator JavaDoc<Map.Entry JavaDoc<String JavaDoc, Object JavaDoc>> it = properties.entrySet().iterator(); it.hasNext();) {
74             Map.Entry JavaDoc<String JavaDoc, Object JavaDoc> entry = it.next();
75             String JavaDoc name = entry.getKey();
76             Object JavaDoc value = entry.getValue();
77             allProps.putAll(PropertyProcessor.processProperty(name, value, types));
78         }
79         return allProps;
80     }
81     
82     private final Map JavaDoc<String JavaDoc, Object JavaDoc> parseProperties(final Iterator JavaDoc<Object JavaDoc> it) { // sequences String, Object, SerParser.ObjectWrapper
83
Map JavaDoc<String JavaDoc, Object JavaDoc> properties = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
84         for (; it.hasNext();) {
85             Object JavaDoc name = it.next();
86             if ("null".equals(name) || name == null) {
87                 //finito
88
return properties;
89             } else if (!(name instanceof String JavaDoc)) {
90                 throw new IllegalStateException JavaDoc(name.getClass().getName());
91             } else {
92                 if (!it.hasNext()) {
93                     throw new IllegalStateException JavaDoc(name.toString());
94                 }
95                 Object JavaDoc value = it.next();
96                 properties.put((String JavaDoc)name, value);
97                 Object JavaDoc propertyRead = it.next();
98                 if (!(propertyRead instanceof SerParser.ObjectWrapper )) {
99                     throw new IllegalStateException JavaDoc(propertyRead.getClass().getName());
100                 } else {
101                     SerParser.ObjectWrapper ow = (SerParser.ObjectWrapper)propertyRead;
102                     if (!ow.classdesc.name.endsWith("java.lang.Boolean;")) {//NOI18N
103
throw new IllegalStateException JavaDoc(ow.classdesc.name);
104                     }
105                 }
106             }
107         }
108         throw new IllegalStateException JavaDoc("Unexpected end");//NOI18N
109
}
110 }
111
Popular Tags