KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > settings > ConvertorResolver


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.spi.settings;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.Repository;
27
28 /** Look up proper settings convertor registrations.
29  *
30  * @author Jan Pokorsky
31  */

32 final class ConvertorResolver {
33     private static final String JavaDoc LOOKUP_PREFIX = "/xml/lookups"; // NOI18N
34
private final static ConvertorResolver DEFAULT = new ConvertorResolver();
35
36     /** Creates a new instance of ConvertorResolver */
37     private ConvertorResolver() {
38     }
39     
40     protected static ConvertorResolver getDefault() {
41         return DEFAULT;
42     }
43     
44     /** look up a convertor registered under xml/memory; used by the storing operation;
45      * can return <code>null</code>
46      */

47     protected Convertor getConvertor(Class JavaDoc clazz) {
48         try {
49             FileObject fo = org.netbeans.modules.settings.Env.findProvider(clazz);
50             if (fo == null) {
51                 fo = org.netbeans.modules.settings.Env.findProvider(Object JavaDoc.class);
52             }
53             return getConvertor(fo);
54         } catch (IOException JavaDoc ex) {
55             Logger.getLogger(ConvertorResolver.class.getName()).log(Level.WARNING, null, ex);
56             return null;
57         }
58     }
59     
60     String JavaDoc getPublicID(Class JavaDoc clazz) {
61         try {
62             FileObject fo = org.netbeans.modules.settings.Env.findProvider(clazz);
63             if (fo == null) {
64                 fo = org.netbeans.modules.settings.Env.findProvider(Object JavaDoc.class);
65             }
66             
67             fo = org.netbeans.modules.settings.Env.findEntityRegistration(fo);
68             Object JavaDoc attrib = fo.getAttribute(org.netbeans.modules.settings.Env.EA_PUBLICID);
69             return (attrib == null || !(attrib instanceof String JavaDoc))? null: (String JavaDoc) attrib;
70         } catch (IOException JavaDoc ex) {
71             Logger.getLogger(ConvertorResolver.class.getName()).log(Level.WARNING, null, ex);
72             return null;
73         }
74     }
75
76     /** look up a convertor registered under xml/lookups; used by reading operation;
77      * can return <code>null</code>
78      */

79     protected Convertor getConvertor(String JavaDoc publicID) {
80         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
81         sb.append(LOOKUP_PREFIX);
82         sb.append(convertPublicId(publicID));
83         // at least for now
84
sb.append (".instance"); // NOI18N
85

86         FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(sb.toString());
87         return (fo == null)? null: getConvertor(fo);
88     }
89     
90     /** extract convertor from file attributes */
91     private Convertor getConvertor(FileObject fo) {
92         Object JavaDoc attrb = fo.getAttribute(org.netbeans.modules.settings.Env.EA_CONVERTOR);
93         return (attrb == null || !(attrb instanceof Convertor))? null: (Convertor) attrb;
94     }
95     
96     /** Converts the publicID into filesystem friendly name.
97      * <p>
98      * It expects that PUBLIC has at maximum three "//" parts
99      * (standard // vendor // entity name // language). It is basically
100      * converted to "vendor/entity_name" resource name.
101      *
102      * @see EntityCatalog
103      */

104     private static String JavaDoc convertPublicId (String JavaDoc publicID) {
105         char[] arr = publicID.toCharArray ();
106
107
108         int numberofslashes = 0;
109         int state = 0;
110         int write = 0;
111         OUT: for (int i = 0; i < arr.length; i++) {
112             char ch = arr[i];
113
114             switch (state) {
115             case 0:
116                 // initial state
117
if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') {
118                     // do not write that char
119
continue;
120                 }
121                 // switch to regular state
122
state = 1;
123                 // fallthru
124
case 1:
125                 // regular state expecting any character
126
if (ch == '/') {
127                     state = 2;
128                     if (++numberofslashes == 3) {
129                         // last part of the ID, exit
130
break OUT;
131                     }
132                     arr[write++] = '/';
133                     continue;
134                 }
135                 break;
136             case 2:
137                 // previous character was /
138
if (ch == '/') {
139                     // ignore second / and write nothing
140
continue;
141                 }
142                 state = 1;
143                 break;
144             }
145
146             // write the char into the array
147
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
148                 arr[write++] = ch;
149             } else {
150                 arr[write++] = '_';
151             }
152         }
153
154         return new String JavaDoc (arr, 0, write);
155     }
156     
157 }
158
Popular Tags