1 19 20 package org.netbeans.modules.xml.xam.ui; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.OutputStream ; 31 import java.io.PrintWriter ; 32 import java.lang.management.ManagementFactory ; 33 import java.net.URI ; 34 import java.util.Collection ; 35 import javax.swing.text.Document ; 36 import org.netbeans.modules.xml.schema.model.Annotation; 37 import org.netbeans.modules.xml.schema.model.ComplexType; 38 import org.netbeans.modules.xml.schema.model.Documentation; 39 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 40 import org.netbeans.modules.xml.schema.model.GlobalElement; 41 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 42 import org.netbeans.modules.xml.schema.model.LocalComplexType; 43 import org.netbeans.modules.xml.schema.model.LocalElement; 44 import org.netbeans.modules.xml.schema.model.LocalSimpleType; 45 import org.netbeans.modules.xml.schema.model.Schema; 46 import org.netbeans.modules.xml.schema.model.SchemaComponent; 47 import org.netbeans.modules.xml.schema.model.SchemaModel; 48 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 49 import org.netbeans.modules.xml.schema.model.Sequence; 50 import org.netbeans.modules.xml.schema.model.SimpleType; 51 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction; 52 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl; 53 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM; 54 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 55 import org.netbeans.modules.xml.xam.dom.DocumentModel; 56 import org.openide.filesystems.FileLock; 57 import org.openide.filesystems.FileObject; 58 import org.openide.filesystems.FileUtil; 59 import org.w3c.dom.Element ; 60 61 65 public class Util { 66 public static final String PO_XSD = "resources/PurchaseOrder.xsd"; 67 public static final String EMPTY_XSD = "resources/Empty.xsd"; 68 69 public static Document getResourceAsDocument(String path) throws Exception { 70 InputStream in = Util.class.getResourceAsStream(path); 71 return loadDocument(in); 72 } 73 74 public static Document loadDocument(InputStream in) throws Exception { 75 Document sd = new org.netbeans.editor.BaseDocument( 77 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 78 return setDocumentContentTo(sd, in); 79 } 80 81 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 82 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 83 StringBuffer sbuf = new StringBuffer (); 84 try { 85 String line = null; 86 while ((line = br.readLine()) != null) { 87 sbuf.append(line); 88 sbuf.append(System.getProperty("line.separator")); 89 } 90 } finally { 91 br.close(); 92 } 93 doc.remove(0, doc.getLength()); 94 doc.insertString(0,sbuf.toString(),null); 95 return doc; 96 } 97 98 public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception { 99 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 100 } 101 102 public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception { 103 setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath); 104 } 105 106 public static int count = 0; 107 public static SchemaModel loadSchemaModel(String resourcePath) throws Exception { 108 NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath); 109 if (nl != null) { 110 return TestCatalogModel.getDefault().getSchemaModel(nl); 111 } 112 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 113 URI locationURI = new URI (location); 114 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 115 return TestCatalogModel.getDefault().getSchemaModel(locationURI); 116 } 117 118 public static SchemaModel loadSchemaModel(File schemaFile) throws Exception { 119 URI locationURI = new URI (schemaFile.getName()); 120 TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI()); 121 return TestCatalogModel.getDefault().getSchemaModel(locationURI); 122 } 123 124 public static SchemaModel createEmptySchemaModel() throws Exception { 125 return loadSchemaModel(EMPTY_XSD); 126 } 127 128 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 129 PrintWriter w = new PrintWriter (out); 130 w.print(doc.getText(0, doc.getLength())); 131 w.close(); 132 out.close(); 133 } 134 135 public static void dumpToFile(Document doc, File f) throws Exception { 136 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 137 PrintWriter w = new PrintWriter (out); 138 w.print(doc.getText(0, doc.getLength())); 139 w.close(); 140 out.close(); 141 } 142 143 public static File dumpToTempFile(Document doc) throws Exception { 144 File f = File.createTempFile("xsm", "xsd"); 145 dumpToFile(doc, f); 146 return f; 147 } 148 149 public static Document loadDocument(File f) throws Exception { 150 InputStream in = new BufferedInputStream (new FileInputStream (f)); 151 return loadDocument(in); 152 } 153 154 public static SchemaComponent findComponent(Schema schema, String xpath) { 155 return (new FindSchemaComponentFromDOM().findComponent(schema, xpath)); 156 } 157 158 public static Sequence toSequence(SchemaComponent sc) { 159 return (sc instanceof Sequence) ? (Sequence) sc : null; 160 } 161 public static LocalElement toLocalElement(SchemaComponent sc) { 162 return (sc instanceof LocalElement) ? (LocalElement) sc : null; 163 } 164 public static SchemaModelImpl toSchemaModelImpl(SchemaModel sc) { 165 return sc instanceof SchemaModelImpl ? (SchemaModelImpl) sc : null; 166 } 167 168 public static GlobalElement createGlobalElement(SchemaModel model, String name) { 169 GlobalElement ge = model.getFactory().createGlobalElement(); 170 ge.setName(name); model.getSchema().addElement(ge); 171 return ge; 172 } 173 public static GlobalSimpleType createGlobalSimpleType(SchemaModel model, String name) { 174 GlobalSimpleType t = model.getFactory().createGlobalSimpleType(); 175 t.setName(name); model.getSchema().addSimpleType(t); 176 return t; 177 } 178 179 public static GlobalComplexType createGlobalComplexType(SchemaModel model, String name) { 180 GlobalComplexType t = model.getFactory().createGlobalComplexType(); 181 t.setName(name); model.getSchema().addComplexType(t); 182 return t; 183 } 184 185 public static Sequence createSequence(SchemaModel m, ComplexType gct) { 186 Sequence s = m.getFactory().createSequence(); gct.setDefinition(s); 187 return s; 188 } 189 190 public static LocalElement createLocalElement(SchemaModel m, Sequence seq, String name, int i) { 191 LocalElement le = m.getFactory().createLocalElement(); 192 seq.addContent(le, i); le.setName(name); 193 return le; 194 } 195 196 public static LocalSimpleType createLocalSimpleType(SchemaModel m, LocalElement e) { 197 LocalSimpleType t = m.getFactory().createLocalSimpleType(); 198 e.setInlineType(t); 199 return t; 200 } 201 202 public static LocalComplexType createLocalComplexType(SchemaModel m, LocalElement e) { 203 LocalComplexType t = m.getFactory().createLocalComplexType(); 204 e.setInlineType(t); 205 return t; 206 } 207 208 public static LocalComplexType createLocalComplexType(SchemaModel m, GlobalElement e) { 209 LocalComplexType t = m.getFactory().createLocalComplexType(); 210 e.setInlineType(t); 211 return t; 212 } 213 214 public static GlobalSimpleType getPrimitiveType(String typeName){ 215 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 216 Collection <GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes(); 217 for(GlobalSimpleType ptype: primitives){ 218 if(ptype.getName().equals(typeName)){ 219 return ptype; 220 } 221 } 222 return null; 223 } 224 225 public static Annotation createAnnotation(SchemaModel m, SchemaComponent p, String s) { 226 Annotation a = m.getFactory().createAnnotation(); 227 Documentation d = m.getFactory().createDocumentation(); 228 a.addDocumentation(d); p.setAnnotation(a); 229 Element e = d.getDocumentationElement(); m.getDocument().createTextNode(s); 230 d.setDocumentationElement(e); 231 return a; 232 } 233 234 public static SimpleTypeRestriction createSimpleRestriction(SchemaModel m, SimpleType st) { 235 SimpleTypeRestriction csr = m.getFactory().createSimpleTypeRestriction(); 236 st.setDefinition(csr); 237 return csr; 238 } 239 240 public static URI getResourceURI(String path) throws RuntimeException { 241 try { 242 return Util.class.getResource(path).toURI(); 243 } catch (Exception ex) { 244 throw new RuntimeException (ex); 245 } 246 } 247 248 public static File getTempDir(String path) throws Exception { 249 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 250 tempdir.mkdirs(); 251 return tempdir; 252 } 253 254 public static void printMemoryUsage(String str){ 255 long init = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getInit(); 256 long cur = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); 257 long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax(); 258 System.out.printf("%s:\n@@@@@@MEMORY: %d/%d/%d\n",str, (init/(1024*1024)), (cur/(1024*1024)), (max/(1024*1024))); 259 } 260 261 public static SchemaModel dumpAndReloadModel(SchemaModel sm) throws Exception { 262 return dumpAndReloadModel((Document ) sm.getModelSource().getLookup().lookup(Document .class)); 263 } 264 265 public static SchemaModel dumpAndReloadModel(Document doc) throws Exception { 266 File f = dumpToTempFile(doc); 267 URI dumpURI = new URI ("dummyDump" + count++); 268 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 269 return TestCatalogModel.getDefault().getSchemaModel(dumpURI); 270 } 271 272 public static GlobalSimpleType findGlobalSimpleType(Schema schema, String name) { 273 for (GlobalSimpleType t : schema.getSimpleTypes()) { 274 if (t.getName().equals(name)) { 275 return t; 276 } 277 } 278 return null; 279 } 280 281 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 282 String filename = getFileName(path); 283 284 FileObject dest = destFolder.getFileObject(filename); 285 if (dest == null) { 286 dest = destFolder.createData(filename); 287 } 288 FileLock lock = dest.lock(); 289 OutputStream out = dest.getOutputStream(lock); 290 InputStream in = Util.class.getResourceAsStream(path); 291 try { 292 FileUtil.copy(in, out); 293 } finally { 294 out.close(); 295 in.close(); 296 if (lock != null) lock.releaseLock(); 297 } 298 return dest; 299 } 300 301 public static String getFileName(String path) { 302 int i = path.lastIndexOf('/'); 303 if (i > -1) { 304 return path.substring(i+1); 305 } else { 306 return path; 307 } 308 } 309 310 } 311 | Popular Tags |