1 19 20 package org.netbeans.modules.xml.wsdl.ui.wizard; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URISyntaxException ; 26 import javax.swing.JTextField ; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.modules.xml.schema.model.Schema; 29 import org.netbeans.modules.xml.schema.model.SchemaModel; 30 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 31 import org.netbeans.modules.xml.wsdl.model.Definitions; 32 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 33 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema; 34 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.UIUtilities; 35 import org.netbeans.modules.xml.xam.ModelSource; 36 import org.netbeans.modules.xml.xam.Model.State; 37 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 38 import org.openide.DialogDescriptor; 39 import org.openide.DialogDisplayer; 40 import org.openide.ErrorManager; 41 import org.openide.WizardValidationException; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.loaders.DataObject; 45 import org.openide.util.NbBundle; 46 import org.xml.sax.Attributes ; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.XMLReader ; 50 51 55 public class WsdlUIPanel extends javax.swing.JPanel { 56 57 private static final String TARGET_URL_PREFIX = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultTNS"); 59 private WsdlPanel wizardPanel; 60 private javax.swing.JTextField fileNameTF; 61 62 private boolean hasUserModifiedNamespace = false; 63 64 private NamespaceDocListener mListener = new NamespaceDocListener(); 65 66 67 public WsdlUIPanel(WsdlPanel wizardPanel) { 68 initComponents(); 69 this.wizardPanel=wizardPanel; 70 nsTF.setText(TARGET_URL_PREFIX); 71 } 72 73 void attachFileNameListener(javax.swing.JTextField fileNameTF) { 74 this.fileNameTF=fileNameTF; 75 if (fileNameTF!=null) { 76 nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText()); 77 DocListener list = new DocListener(); 78 javax.swing.text.Document doc = fileNameTF.getDocument(); 79 doc.addDocumentListener(list); 80 } else { 81 nsTF.setText(TARGET_URL_PREFIX); 82 } 83 84 nsTF.getDocument().addDocumentListener(mListener); 85 } 86 87 92 private void initComponents() { 94 java.awt.GridBagConstraints gridBagConstraints; 95 96 cbImport = new javax.swing.JCheckBox (); 97 schemaTF = new javax.swing.JTextField (); 98 browseButton = new javax.swing.JButton (); 99 schemaLB = new javax.swing.JLabel (); 100 namespaceLB = new javax.swing.JLabel (); 101 nsTF = new javax.swing.JTextField (); 102 jPanel1 = new javax.swing.JPanel (); 103 104 setLayout(new java.awt.GridBagLayout ()); 105 106 org.openide.awt.Mnemonics.setLocalizedText(cbImport, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema")); cbImport.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 108 cbImport.setMargin(new java.awt.Insets (0, 0, 0, 0)); 109 cbImport.addItemListener(new java.awt.event.ItemListener () { 110 public void itemStateChanged(java.awt.event.ItemEvent evt) { 111 cbImportItemStateChanged(evt); 112 } 113 }); 114 115 gridBagConstraints = new java.awt.GridBagConstraints (); 116 gridBagConstraints.gridx = 0; 117 gridBagConstraints.gridy = 2; 118 gridBagConstraints.gridwidth = 3; 119 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 120 gridBagConstraints.insets = new java.awt.Insets (5, 0, 6, 5); 121 add(cbImport, gridBagConstraints); 122 cbImport.getAccessibleContext().setAccessibleDescription(null); 124 schemaTF.setEditable(false); 125 schemaTF.setToolTipText(null); gridBagConstraints = new java.awt.GridBagConstraints (); 127 gridBagConstraints.gridx = 1; 128 gridBagConstraints.gridy = 3; 129 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 130 gridBagConstraints.weightx = 1.0; 131 gridBagConstraints.insets = new java.awt.Insets (0, 5, 2, 0); 132 add(schemaTF, gridBagConstraints); 133 schemaTF.getAccessibleContext().setAccessibleDescription(null); 135 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_browse")); browseButton.setEnabled(false); 137 browseButton.addActionListener(new java.awt.event.ActionListener () { 138 public void actionPerformed(java.awt.event.ActionEvent evt) { 139 browseButtonActionPerformed(evt); 140 } 141 }); 142 143 gridBagConstraints = new java.awt.GridBagConstraints (); 144 gridBagConstraints.gridx = 2; 145 gridBagConstraints.gridy = 3; 146 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 147 gridBagConstraints.insets = new java.awt.Insets (0, 5, 2, 0); 148 add(browseButton, gridBagConstraints); 149 browseButton.getAccessibleContext().setAccessibleDescription(null); 151 schemaLB.setLabelFor(schemaTF); 152 org.openide.awt.Mnemonics.setLocalizedText(schemaLB, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_schemaFiles")); gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 3; 156 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 157 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 158 add(schemaLB, gridBagConstraints); 159 160 namespaceLB.setLabelFor(nsTF); 161 org.openide.awt.Mnemonics.setLocalizedText(namespaceLB, org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_targetNamespace")); gridBagConstraints = new java.awt.GridBagConstraints (); 163 gridBagConstraints.gridx = 0; 164 gridBagConstraints.gridy = 0; 165 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 166 gridBagConstraints.insets = new java.awt.Insets (5, 0, 2, 0); 167 add(namespaceLB, gridBagConstraints); 168 169 nsTF.setToolTipText(null); gridBagConstraints = new java.awt.GridBagConstraints (); 171 gridBagConstraints.gridx = 1; 172 gridBagConstraints.gridy = 0; 173 gridBagConstraints.gridwidth = 2; 174 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 175 gridBagConstraints.weightx = 1.0; 176 gridBagConstraints.insets = new java.awt.Insets (5, 5, 2, 0); 177 add(nsTF, gridBagConstraints); 178 nsTF.getAccessibleContext().setAccessibleDescription(null); 180 gridBagConstraints = new java.awt.GridBagConstraints (); 181 gridBagConstraints.gridx = 0; 182 gridBagConstraints.gridy = 5; 183 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 184 gridBagConstraints.weighty = 1.0; 185 add(jPanel1, gridBagConstraints); 186 187 } 189 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { Project project = wizardPanel.getProject(); 192 FileObject prjdir = project.getProjectDirectory(); 193 FileObject privdir = prjdir.getFileObject("nbproject/private"); 195 File directory = FileUtil.toFile(privdir != null ? privdir : prjdir); 198 String fname = fileNameTF.getText(); 199 if (fname == null || fname.length() == 0) { 200 fname = "wizard"; 201 } 202 File file = null; 203 try { 204 file = File.createTempFile(fname, ".wsdl", directory); 205 wizardPanel.populateFileFromTemplate(file); 206 } catch (Exception e) { 207 ErrorManager.getDefault().notify(e); 209 if (file != null) { 210 file.delete(); 211 } 212 return; 213 } 214 WSDLModel model = wizardPanel.prepareModelFromFile(file); 215 model.startTransaction(); 216 WSDLSchema wsdlSchema = model.getFactory().createWSDLSchema(); 217 Definitions defs = model.getDefinitions(); 218 defs.getTypes().addExtensibilityElement(wsdlSchema); 219 SchemaModel schemaModel = wsdlSchema.getSchemaModel(); 220 Schema schema = schemaModel.getSchema(); 221 schema.setTargetNamespace(defs.getTargetNamespace()); 223 model.endTransaction(); 224 225 String original = schemaTF.getText().trim(); 227 ImportSchemaCreator creator = new ImportSchemaCreator(schema, model, original); 228 DialogDescriptor descriptor = UIUtilities.getCreatorDialog( 229 creator, NbBundle.getMessage(WsdlUIPanel.class, 230 "TITLE_selectSchema"), true); 231 descriptor.setValid(false); 232 Object result = DialogDisplayer.getDefault().notify(descriptor); 233 if (result == DialogDescriptor.OK_OPTION) { 234 String selections = creator.getSelectedFiles(); 235 schemaTF.setText(selections); 236 schemaTF.firePropertyChange("VALUE_SET", false, true); 237 } 238 239 file = FileUtil.normalizeFile(file); 241 FileObject fobj = FileUtil.toFileObject(file); 242 try { 243 DataObject.find(fobj).delete(); 244 } catch (IOException ex) { 245 } 247 } 249 private void cbImportItemStateChanged(java.awt.event.ItemEvent evt) { if (cbImport.isSelected()) { 251 schemaTF.setEditable(true); 252 browseButton.setEnabled(true); 253 } else { 254 schemaTF.setEditable(false); 255 browseButton.setEnabled(false); 256 } 257 } 259 260 private javax.swing.JButton browseButton; 262 private javax.swing.JCheckBox cbImport; 263 private javax.swing.JPanel jPanel1; 264 private javax.swing.JLabel namespaceLB; 265 private javax.swing.JTextField nsTF; 266 private javax.swing.JLabel schemaLB; 267 private javax.swing.JTextField schemaTF; 268 270 private class NamespaceDocListener implements javax.swing.event.DocumentListener { 271 272 public void removeUpdate(javax.swing.event.DocumentEvent e) { 273 documentChanged(e); 274 } 275 276 public void insertUpdate(javax.swing.event.DocumentEvent e) { 277 documentChanged(e); 278 } 279 280 public void changedUpdate(javax.swing.event.DocumentEvent e) { 281 documentChanged(e); 282 } 283 284 private void documentChanged(javax.swing.event.DocumentEvent e) { 285 hasUserModifiedNamespace = true; 286 } 287 } 288 289 private class DocListener implements javax.swing.event.DocumentListener { 290 291 public void removeUpdate(javax.swing.event.DocumentEvent e) { 292 documentChanged(e); 293 } 294 295 public void insertUpdate(javax.swing.event.DocumentEvent e) { 296 documentChanged(e); 297 } 298 299 public void changedUpdate(javax.swing.event.DocumentEvent e) { 300 documentChanged(e); 301 } 302 303 private void documentChanged(javax.swing.event.DocumentEvent e) { 304 if(!hasUserModifiedNamespace) { 305 nsTF.getDocument().removeDocumentListener(mListener); 306 nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText()); 307 nsTF.getDocument().addDocumentListener(mListener); 308 } 309 310 } 311 } 312 313 315 static class SchemaInfo { 316 private java.net.URL url; 317 SchemaInfo(java.net.URL url) { 318 this.url=url; 319 } 320 321 java.net.URL getURL() { 322 return url; 323 } 324 325 String getNamespace() { 326 InputSource is = new InputSource (url.toExternalForm()); 327 try { 328 return parse(is); 329 } catch (java.io.IOException ex){ 330 } catch (SAXException ex){} 331 return ""; 332 } 333 334 String getSchemaName() { 335 return url.toExternalForm(); 336 } 337 338 340 private String parse(InputSource is) throws java.io.IOException , SAXException { 341 XMLReader xmlReader = org.openide.xml.XMLUtil.createXMLReader(); 342 NsHandler handler = new NsHandler(); 343 xmlReader.setContentHandler(handler); 344 try { 346 xmlReader.parse(is); 347 } catch (SAXException ex) { 348 if (!"EXIT".equals(ex.getMessage())) throw ex; } 350 String ns = handler.getNs(); 351 if (ns==null) return ""; 352 else return ns; 353 } 354 355 private static class NsHandler extends org.xml.sax.helpers.DefaultHandler { 356 private String ns; 357 358 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 359 if (qName.endsWith("schema")) { ns=attributes.getValue("targetNamespace"); throw new SAXException ("EXIT"); } 363 } 364 365 String getNs() { 366 return ns; 367 } 368 } 369 370 } 371 372 String getNS() { 373 return nsTF.getText(); 374 } 375 376 boolean isImport() { 377 return cbImport.isSelected(); 378 } 379 380 SchemaInfo[] getSchemas() { 381 if (cbImport.isSelected()) { 382 String schemas = schemaTF.getText(); 383 String [] urls = schemas.split(","); 384 java.util.List infos = new java.util.ArrayList (); 385 for (int i=0;i<urls.length;i++) { 386 String urlString=urls[i].trim(); 387 if (urlString.length()==0) continue; 388 try { 389 java.net.URL url = new java.net.URL (urlString); 390 infos.add(new SchemaInfo(url)); 391 } catch (java.net.MalformedURLException ex) { 392 try { 394 org.openide.loaders.DataFolder folder = wizardPanel.getTemplateWizard().getTargetFolder(); 395 org.openide.filesystems.FileObject fo = folder.getPrimaryFile(); 396 if ((fo.getFileObject(urlString))!=null) { 397 String parentURL = fo.getURL().toExternalForm(); 398 infos.add(new SchemaInfo(new java.net.URL (parentURL+urlString))); 399 } 400 } catch (java.io.IOException ex1) {} 401 } 402 } 403 SchemaInfo[] result = new SchemaInfo[infos.size()]; 404 infos.toArray(result); 405 return result; 406 } 407 return new SchemaInfo[]{}; 408 } 409 410 public void validateSchemas() throws WizardValidationException { 411 if (cbImport.isSelected()) { 412 String schemas = schemaTF.getText(); 413 String [] urls = schemas.split(","); 414 for (int i=0;i<urls.length;i++) { 415 String urlString=urls[i].trim(); 416 if (urlString.length()==0) continue; 417 createSchemaModel(urlString); 418 } 419 } 420 421 } 422 423 private void createSchemaModel(String urlString) throws WizardValidationException { 424 java.net.URL url = null; 425 try { 426 url = new java.net.URL (urlString); 427 } catch (MalformedURLException e) { 428 org.openide.loaders.DataFolder folder; 429 try { 430 folder = wizardPanel.getTemplateWizard().getTargetFolder(); 431 org.openide.filesystems.FileObject fo = folder.getPrimaryFile(); 432 if ((fo.getFileObject(urlString))!=null) { 433 String parentURL = fo.getURL().toExternalForm(); 434 try { 435 url = new java.net.URL (parentURL+urlString); 436 } catch (MalformedURLException e1) { 437 throw new WizardValidationException(schemaTF, e1.getMessage(), e1.getLocalizedMessage()); 438 } 439 } 440 } catch (IOException e1) { 441 throw new WizardValidationException(schemaTF, e1.getMessage(), e1.getLocalizedMessage()); 442 } 443 if (url == null) { 444 String errorString = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString); 445 throw new WizardValidationException(schemaTF, errorString, errorString); 446 } 447 } 448 ModelSource source; 449 try { 450 FileObject fo = FileUtil.toFileObject(new File (url.toURI())); 451 if (fo == null) { 452 String errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString); 453 throw new WizardValidationException(schemaTF, errorMessage, errorMessage); 454 } 455 source = org.netbeans.modules.xml.retriever.catalog.Utilities. 456 createModelSource(fo, false); 457 } catch (WizardValidationException e) { 458 throw e; 459 } catch (CatalogModelException e) { 460 throw new WizardValidationException(schemaTF, e.getMessage(), e.getLocalizedMessage()); 461 } catch (URISyntaxException e) { 462 throw new WizardValidationException(schemaTF, e.getMessage(), e.getLocalizedMessage()); 463 } catch (Throwable e) { 464 String errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString); 465 throw new WizardValidationException(schemaTF, errorMessage, errorMessage); 466 } 467 SchemaModel model = null; 468 try { 469 model = SchemaModelFactory.getDefault().getModel(source); 470 } catch (Throwable e) { 471 String errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString); 472 throw new WizardValidationException(schemaTF, errorMessage, errorMessage); 473 } 474 475 if (model == null || model.getState().equals(State.NOT_WELL_FORMED)) { 476 String errorMessage = NbBundle.getMessage(WsdlUIPanel.class, "INVALID_SCHEMA_FILE", urlString); 477 throw new WizardValidationException(schemaTF, errorMessage, errorMessage); 478 } 479 } 480 481 public JTextField getSchemaFileTextField() { 482 return schemaTF; 483 } 484 } 485 | Popular Tags |