KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.upgrade.systemoptions;
21
22 import java.io.*;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.openide.filesystems.FileObject;
26
27 /**
28  *
29  * @author Radek Matous
30  */

31 public class SystemOptionsParser {
32     static final String JavaDoc EXPECTED_INSTANCE = "org.openide.options.SystemOption";//NOI18N
33

34     private String JavaDoc systemOptionInstanceName;
35     private boolean types;
36     
37     private SystemOptionsParser(final String JavaDoc systemOptionInstanceName, final boolean types) {
38         this.systemOptionInstanceName = systemOptionInstanceName;
39         this.types = types;
40     }
41     
42     public static DefaultResult parse(FileObject settingsFo, boolean types) throws IOException, ClassNotFoundException JavaDoc {
43         SettingsRecognizer instance = getRecognizer(settingsFo);
44         
45         SystemOptionsParser rImpl = null;
46         InputStream is = instance.getSerializedInstance();
47         try {
48             SerParser sp = new SerParser(is);
49             SerParser.Stream s = sp.parse();
50             rImpl = new SystemOptionsParser(instance.instanceName(), types);
51             DefaultResult ret = (DefaultResult)rImpl.processContent(s.contents.iterator(), false);
52             ret.setModuleName(instance.getCodeNameBase().replace('.','/'));
53             return ret;
54         } finally {
55             is.close();
56         }
57     }
58     
59     private Result processContent(final Iterator JavaDoc<Object JavaDoc> it, final boolean reachedWriteReplace) {
60         for (; it.hasNext();) {
61             Object JavaDoc elem = it.next();
62             if (!reachedWriteReplace && elem instanceof SerParser.ObjectWrapper) {
63                 SerParser.ObjectWrapper ow = (SerParser.ObjectWrapper)elem;
64                 String JavaDoc name = ow.classdesc.name;
65                 if (name.endsWith("org.openide.util.SharedClassObject$WriteReplace;")) {//NOI18N
66
return processContent(ow.data.iterator(), true);
67                 }
68             } else if (reachedWriteReplace && elem instanceof SerParser.NameValue ) {
69                 SerParser.NameValue nv = (SerParser.NameValue)elem;
70                 if (systemOptionInstanceName.equals(nv.value)) {
71                         Result result = ContentProcessor.parseContent(systemOptionInstanceName, types, it);
72                     return result;
73                 }
74             }
75         }
76         return null;
77     }
78             
79     private static SettingsRecognizer getRecognizer(final FileObject settingsFo) throws IOException {
80         SettingsRecognizer recognizer = new SettingsRecognizer(false, settingsFo);
81         recognizer.parse();
82         
83         Set JavaDoc instances = recognizer.getInstanceOf();
84         String JavaDoc iName = recognizer.instanceName();
85         if (!instances.contains(EXPECTED_INSTANCE)) {
86             throw new IOException(iName);
87         }
88         return recognizer;
89     }
90 }
91
92
93
94
95
Popular Tags