1 19 20 package org.netbeans.spi.settings; 21 22 import java.io.IOException ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.Repository; 27 28 32 final class ConvertorResolver { 33 private static final String LOOKUP_PREFIX = "/xml/lookups"; private final static ConvertorResolver DEFAULT = new ConvertorResolver(); 35 36 37 private ConvertorResolver() { 38 } 39 40 protected static ConvertorResolver getDefault() { 41 return DEFAULT; 42 } 43 44 47 protected Convertor getConvertor(Class 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 .class); 52 } 53 return getConvertor(fo); 54 } catch (IOException ex) { 55 Logger.getLogger(ConvertorResolver.class.getName()).log(Level.WARNING, null, ex); 56 return null; 57 } 58 } 59 60 String getPublicID(Class 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 .class); 65 } 66 67 fo = org.netbeans.modules.settings.Env.findEntityRegistration(fo); 68 Object attrib = fo.getAttribute(org.netbeans.modules.settings.Env.EA_PUBLICID); 69 return (attrib == null || !(attrib instanceof String ))? null: (String ) attrib; 70 } catch (IOException ex) { 71 Logger.getLogger(ConvertorResolver.class.getName()).log(Level.WARNING, null, ex); 72 return null; 73 } 74 } 75 76 79 protected Convertor getConvertor(String publicID) { 80 StringBuffer sb = new StringBuffer (200); 81 sb.append(LOOKUP_PREFIX); 82 sb.append(convertPublicId(publicID)); 83 sb.append (".instance"); 86 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(sb.toString()); 87 return (fo == null)? null: getConvertor(fo); 88 } 89 90 91 private Convertor getConvertor(FileObject fo) { 92 Object attrb = fo.getAttribute(org.netbeans.modules.settings.Env.EA_CONVERTOR); 93 return (attrb == null || !(attrb instanceof Convertor))? null: (Convertor) attrb; 94 } 95 96 104 private static String convertPublicId (String 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 if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') { 118 continue; 120 } 121 state = 1; 123 case 1: 125 if (ch == '/') { 127 state = 2; 128 if (++numberofslashes == 3) { 129 break OUT; 131 } 132 arr[write++] = '/'; 133 continue; 134 } 135 break; 136 case 2: 137 if (ch == '/') { 139 continue; 141 } 142 state = 1; 143 break; 144 } 145 146 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 (arr, 0, write); 155 } 156 157 } 158 | Popular Tags |