001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.settings;
043:
044: import java.io.IOException;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047: import org.openide.filesystems.FileObject;
048: import org.openide.filesystems.Repository;
049:
050: /** Look up proper settings convertor registrations.
051: *
052: * @author Jan Pokorsky
053: */
054: final class ConvertorResolver {
055: private static final String LOOKUP_PREFIX = "/xml/lookups"; // NOI18N
056: private final static ConvertorResolver DEFAULT = new ConvertorResolver();
057:
058: /** Creates a new instance of ConvertorResolver */
059: private ConvertorResolver() {
060: }
061:
062: protected static ConvertorResolver getDefault() {
063: return DEFAULT;
064: }
065:
066: /** look up a convertor registered under xml/memory; used by the storing operation;
067: * can return <code>null</code>
068: */
069: protected Convertor getConvertor(Class clazz) {
070: try {
071: FileObject fo = org.netbeans.modules.settings.Env
072: .findProvider(clazz);
073: if (fo == null) {
074: fo = org.netbeans.modules.settings.Env
075: .findProvider(Object.class);
076: }
077: return getConvertor(fo);
078: } catch (IOException ex) {
079: Logger.getLogger(ConvertorResolver.class.getName()).log(
080: Level.WARNING, null, ex);
081: return null;
082: }
083: }
084:
085: String getPublicID(Class clazz) {
086: try {
087: FileObject fo = org.netbeans.modules.settings.Env
088: .findProvider(clazz);
089: if (fo == null) {
090: fo = org.netbeans.modules.settings.Env
091: .findProvider(Object.class);
092: }
093:
094: fo = org.netbeans.modules.settings.Env
095: .findEntityRegistration(fo);
096: Object attrib = fo
097: .getAttribute(org.netbeans.modules.settings.Env.EA_PUBLICID);
098: return (attrib == null || !(attrib instanceof String)) ? null
099: : (String) attrib;
100: } catch (IOException ex) {
101: Logger.getLogger(ConvertorResolver.class.getName()).log(
102: Level.WARNING, null, ex);
103: return null;
104: }
105: }
106:
107: /** look up a convertor registered under xml/lookups; used by reading operation;
108: * can return <code>null</code>
109: */
110: protected Convertor getConvertor(String publicID) {
111: StringBuffer sb = new StringBuffer(200);
112: sb.append(LOOKUP_PREFIX);
113: sb.append(convertPublicId(publicID));
114: // at least for now
115: sb.append(".instance"); // NOI18N
116:
117: FileObject fo = Repository.getDefault().getDefaultFileSystem()
118: .findResource(sb.toString());
119: return (fo == null) ? null : getConvertor(fo);
120: }
121:
122: /** extract convertor from file attributes */
123: private Convertor getConvertor(FileObject fo) {
124: Object attrb = fo
125: .getAttribute(org.netbeans.modules.settings.Env.EA_CONVERTOR);
126: return (attrb == null || !(attrb instanceof Convertor)) ? null
127: : (Convertor) attrb;
128: }
129:
130: /** Converts the publicID into filesystem friendly name.
131: * <p>
132: * It expects that PUBLIC has at maximum three "//" parts
133: * (standard // vendor // entity name // language). It is basically
134: * converted to "vendor/entity_name" resource name.
135: *
136: * @see EntityCatalog
137: */
138: private static String convertPublicId(String publicID) {
139: char[] arr = publicID.toCharArray();
140:
141: int numberofslashes = 0;
142: int state = 0;
143: int write = 0;
144: OUT: for (int i = 0; i < arr.length; i++) {
145: char ch = arr[i];
146:
147: switch (state) {
148: case 0:
149: // initial state
150: if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S'
151: || ch == 'O') {
152: // do not write that char
153: continue;
154: }
155: // switch to regular state
156: state = 1;
157: // fallthru
158: case 1:
159: // regular state expecting any character
160: if (ch == '/') {
161: state = 2;
162: if (++numberofslashes == 3) {
163: // last part of the ID, exit
164: break OUT;
165: }
166: arr[write++] = '/';
167: continue;
168: }
169: break;
170: case 2:
171: // previous character was /
172: if (ch == '/') {
173: // ignore second / and write nothing
174: continue;
175: }
176: state = 1;
177: break;
178: }
179:
180: // write the char into the array
181: if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z'
182: || ch >= '0' && ch <= '9') {
183: arr[write++] = ch;
184: } else {
185: arr[write++] = '_';
186: }
187: }
188:
189: return new String(arr, 0, write);
190: }
191:
192: }
|