1 19 20 26 package org.netbeans.modules.xml.wsdl.ui.fastmodel.impl; 27 28 import java.io.File ; 29 import java.io.InputStream ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import javax.xml.parsers.SAXParser ; 34 import javax.xml.parsers.SAXParserFactory ; 35 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastSchema; 36 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastSchemaFactory; 37 38 import org.xml.sax.Attributes ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.SAXParseException ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 43 44 45 51 public class FastSchemaFactoryImpl extends FastSchemaFactory { 52 53 private boolean mParseImports = false; 54 55 56 private Logger logger = Logger.getLogger(this.getClass().getName()); 57 58 @Override  59 public FastSchema newFastSchema(InputStream in, boolean parseImports) { 60 this.mParseImports = parseImports; 61 FastSchema def = new FastSchemaImpl(); 62 try { 63 SAXParserFactory fac = SAXParserFactory.newInstance(); 64 SAXParser parser = fac.newSAXParser(); 65 FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def); 66 parser.parse(in, handler); 67 68 } catch(Exception ex) { 69 logger.log(Level.SEVERE, "Failed to parse schema", ex); 70 def.setParseErrorMessage(ex.getMessage()); 71 } 72 73 return def; 74 } 75 76 @Override  77 public FastSchema newFastSchema(String defFileUrl) { 78 return newFastSchema(defFileUrl, false); 79 } 80 81 @Override  82 public FastSchema newFastSchema(String defFileUrl, 83 boolean parseImports) { 84 File file = new File (defFileUrl); 85 return newFastSchema(file, parseImports); 86 } 87 88 @Override  89 public FastSchema newFastSchema(File file) { 90 return newFastSchema(file, false); 91 } 92 93 @Override  94 public FastSchema newFastSchema(File file, boolean parseImports) { 95 this.mParseImports = parseImports; 96 FastSchema def = new FastSchemaImpl(); 97 98 99 100 try { 101 SAXParserFactory fac = SAXParserFactory.newInstance(); 102 SAXParser parser = fac.newSAXParser(); 103 FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def); 104 parser.parse(file, handler); 105 106 } catch(Exception ex) { 107 logger.log(Level.SEVERE, "Failed to parse "+ file.getAbsolutePath(), ex); 108 def.setParseErrorMessage(ex.getMessage()); 109 } 110 111 return def; 112 } 113 114 public class FastWSDLDefinitionsHandler extends DefaultHandler { 115 116 private String targetNamespace; 117 118 private FastSchema mDef; 119 120 public FastWSDLDefinitionsHandler(FastSchema def) { 121 this.mDef = def; 122 } 123 124 public String getTargetNamespace() { 125 return targetNamespace; 126 } 127 128 @Override  129 public void startElement (String uri, 130 String localName, 131 String qName, 132 Attributes attributes) 133 throws SAXException  134 { 135 if(qName.endsWith("schema")) { 136 for(int i = 0 ; i < attributes.getLength(); i++) { 137 String attrQName = attributes.getQName(i); 138 if(attrQName.endsWith("targetNamespace")) { 139 targetNamespace = attributes.getValue(i); 140 mDef.setTargetNamespace(targetNamespace); 141 break; 142 } 143 } 144 } 145 146 147 } 148 149 @Override  150 public void fatalError (SAXParseException e) 151 throws SAXException  152 { 153 154 } 155 156 @Override  157 public void error (SAXParseException e) 158 throws SAXException  159 { 160 161 } 162 163 } 164 } 165 | Popular Tags |