0001: /*
0002: * The contents of this file are subject to the terms of the Common Development
0003: * and Distribution License (the License). You may not use this file except in
0004: * compliance with the License.
0005: *
0006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
0007: * or http://www.netbeans.org/cddl.txt.
0008: *
0009: * When distributing Covered Code, include this CDDL Header Notice in each file
0010: * and include the License file at http://www.netbeans.org/cddl.txt.
0011: * If applicable, add the following below the CDDL Header, with the fields
0012: * enclosed by brackets [] replaced by your own identifying information:
0013: * "Portions Copyrighted [year] [name of copyright owner]"
0014: *
0015: * The Original Software is NetBeans. The Initial Developer of the Original
0016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0017: * Microsystems, Inc. All Rights Reserved.
0018: */
0019:
0020: package org.netbeans.modules.bpel.project;
0021:
0022: import java.awt.Dialog;
0023:
0024: import java.io.File;
0025: import java.io.FileOutputStream;
0026: import java.net.URI;
0027: import org.openide.util.NbBundle;
0028: import org.openide.DialogDescriptor;
0029: import org.openide.DialogDisplayer;
0030:
0031: import java.net.URISyntaxException;
0032: import java.awt.BorderLayout;
0033:
0034: import java.net.URL;
0035:
0036: import javax.swing.JComponent;
0037: import javax.swing.SwingUtilities;
0038: import javax.xml.parsers.DocumentBuilder;
0039: import javax.xml.parsers.DocumentBuilderFactory;
0040: import javax.xml.transform.Transformer;
0041: import javax.xml.transform.TransformerFactory;
0042: import javax.xml.transform.stream.StreamResult;
0043: import javax.xml.transform.dom.DOMSource;
0044: import java.util.*;
0045:
0046: import java.util.logging.ErrorManager;
0047: import java.util.logging.Level;
0048: import java.util.logging.Logger;
0049: import java.text.MessageFormat;
0050:
0051: import org.netbeans.modules.bpel.model.spi.BpelModelFactory;
0052: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
0053:
0054: import org.netbeans.api.project.Project;
0055:
0056: import org.netbeans.modules.bpel.model.api.BpelModel;
0057: import org.netbeans.modules.bpel.model.api.Import;
0058: import org.netbeans.modules.bpel.model.spi.BpelModelFactory;
0059: import org.netbeans.modules.bpel.project.ui.FoldersListSettings;
0060: import org.netbeans.modules.xml.retriever.Retriever;
0061:
0062: import org.openide.filesystems.FileObject;
0063: import org.openide.filesystems.FileUtil;
0064:
0065: import org.openide.util.Lookup;
0066:
0067: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
0068: import org.netbeans.modules.xml.xam.ModelSource;
0069: import org.netbeans.modules.xml.retriever.catalog.Utilities;
0070: import org.netbeans.modules.xml.schema.model.Schema;
0071: import org.netbeans.modules.xml.schema.model.SchemaModel;
0072: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
0073: import org.netbeans.modules.xml.wsdl.model.Types;
0074: import org.netbeans.modules.xml.xam.Model;
0075: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
0076: import org.netbeans.modules.xml.xam.locator.CatalogModelFactory;
0077: import org.netbeans.api.progress.ProgressHandleFactory;
0078: import org.netbeans.api.progress.ProgressHandle;
0079:
0080: import org.w3c.dom.*;
0081:
0082: /**
0083: * This class is used to populate catalog
0084: * @author Sreenivasan Genipudi
0085: */
0086: public class BpelProjectRetriever {
0087: // private File mProjectDirectory = null;
0088: // private String mProjectDirectoryPath = null;
0089: // private File mRetrieveToDirectory = null;
0090: // private HashSet mVisitedXMLResources = new HashSet();
0091: ProgressHandle pg = null;
0092: private RetrieverWrapper retrieveWrap = new RetrieverWrapper();
0093:
0094: /**
0095: * Logger instance
0096: */
0097: private Logger logger = Logger.getLogger(BpelProjectRetriever.class
0098: .getName());
0099:
0100: private volatile String mStatus = null;
0101:
0102: private RetrieverUpdater mRetUpd = null;
0103: Dialog mDialog = null;
0104:
0105: /**
0106: * Construtor - takes in Project Location (File Object) as paramter
0107: * and tries to populate the catalog
0108: * @param projectDirectory FileObject instance of project directory
0109: */
0110: public BpelProjectRetriever(FileObject projectDirectory) {
0111: retrieveWrap.mProjectDirectoryPath = projectDirectory.getPath();
0112: //mProjectDirectoryPath = projectDirectory.getPath();
0113: }
0114:
0115: private void init() {
0116: String initMsg = NbBundle.getMessage(
0117: BpelProjectRetriever.class, "LBL_Populate_Catalog");
0118: pg = ProgressHandleFactory.createHandle(initMsg,
0119: new org.openide.util.Cancellable() {
0120: public boolean cancel() {
0121: try {
0122: pg.finish();
0123: } catch (Exception ex) {
0124:
0125: }
0126: return true;
0127: }
0128: });
0129: // pg.setInitialDelay(2000);
0130: // pg.setDisplayName(initMsg);
0131:
0132: mRetUpd = new RetrieverUpdater(pg);
0133: DialogDescriptor dd = new DialogDescriptor(mRetUpd,
0134: initMsg,
0135: true, // modal
0136: new Object[0], null, DialogDescriptor.DEFAULT_ALIGN,
0137: null, null, true);
0138: mDialog = DialogDisplayer.getDefault().createDialog(dd);
0139: pg.start();
0140:
0141: }
0142:
0143: public void execute() {
0144: try {
0145: init();
0146: Thread t = new Thread(retrieveWrap);
0147: t.start();
0148: mDialog.setVisible(true);
0149: /* String projectDirPath = this.mProjectDirectoryPath;
0150: String sourceDirectoryPath = projectDirPath + File.separator + "src";
0151: CommandlineBpelProjectXmlCatalogProvider.getInstance().setSourceDirectory(sourceDirectoryPath);
0152:
0153: mProjectDirectory = new File(projectDirPath);
0154: File sourceDirectory =
0155: new File(sourceDirectoryPath);
0156: mRetrieveToDirectory =
0157: new File(CommandlineBpelProjectXmlCatalogProvider.getInstance().getRetrieverPath());
0158: if (!mRetrieveToDirectory.exists()) {
0159: mRetrieveToDirectory.mkdirs();
0160: }
0161: processSourceDir(sourceDirectory);
0162: mProgressDisplay.displayStatus(NbBundle.getMessage( BpelProjectRetriever.class, "LBL_Populate_Catalog_Complete" )); */
0163:
0164: } catch (Exception ex) {
0165: mRetUpd.setProgressMessage(NbBundle.getMessage(
0166: BpelProjectRetriever.class,
0167: "LBL_Populate_Catalog_Error"));
0168: } finally {
0169: dispose();
0170: }
0171: }
0172:
0173: void dispose() {
0174: try {
0175: mDialog.setVisible(false);
0176: mDialog.dispose();
0177: mDialog = null;
0178: } catch (Exception ex) {
0179:
0180: }
0181: try {
0182: pg.finish();
0183: } catch (Exception ex) {
0184:
0185: }
0186: }
0187:
0188: class RetrieverWrapper implements Runnable {
0189: private File mProjectDirectory = null;
0190: private String mProjectDirectoryPath = null;
0191: private String mProjectSourcePath = null;
0192: private File mRetrieveToDirectory = null;
0193: private HashSet mVisitedXMLResources = new HashSet();
0194:
0195: public void run() {
0196: try {
0197: String projectDirPath = mProjectDirectoryPath;
0198: String sourceDirectoryPath = projectDirPath + "/"
0199: + "src";
0200: mProjectSourcePath = sourceDirectoryPath.replace('\\',
0201: '/');
0202: CommandlineBpelProjectXmlCatalogProvider.getInstance()
0203: .setSourceDirectory(sourceDirectoryPath);
0204:
0205: mProjectDirectory = new File(projectDirPath);
0206: File sourceDirectory = new File(sourceDirectoryPath);
0207: mRetrieveToDirectory = new File(
0208: CommandlineBpelProjectXmlCatalogProvider
0209: .getInstance().getRetrieverPath());
0210: if (!mRetrieveToDirectory.exists()) {
0211: mRetrieveToDirectory.mkdirs();
0212: }
0213: processSourceDir(sourceDirectory);
0214: moveCachedDirs();
0215: displayStatus(NbBundle.getMessage(
0216: BpelProjectRetriever.class,
0217: "LBL_Populate_Catalog_Complete"));
0218:
0219: } catch (Exception ex) {
0220: displayStatus(NbBundle.getMessage(
0221: BpelProjectRetriever.class,
0222: "LBL_Populate_Catalog_Error"));
0223: } finally {
0224: File localCatalogFile = new File(
0225: CommandlineBpelProjectXmlCatalogProvider
0226: .getInstance().getProjectWideCatalog());
0227: if (localCatalogFile.exists()
0228: && localCatalogFile.length() == 0) {
0229: localCatalogFile.delete();
0230: }
0231: dispose();
0232: }
0233:
0234: }
0235:
0236: /**
0237: * Replace the retrieved dirs with long name to simpler ones...
0238: *
0239: * @throws Exception
0240: */
0241: private void moveCachedDirs() throws Exception {
0242: File catFile = new File(mRetrieveToDirectory.getParent(),
0243: "catalog.xml"); // NOI18N
0244: if (catFile.exists()) {
0245: DocumentBuilderFactory fact = DocumentBuilderFactory
0246: .newInstance();
0247: DocumentBuilder builder = fact.newDocumentBuilder();
0248: Document catlogDoc = builder.parse(catFile);
0249: ArrayList uris = new ArrayList();
0250: NodeList sas = catlogDoc.getElementsByTagName("system"); // NOI18N
0251: HashMap dirMap = new HashMap();
0252: if (sas != null) { // find them
0253: for (int i = 0; i < sas.getLength(); i++) {
0254: Element sys = (Element) sas.item(i);
0255: String furi = sys.getAttribute("uri"); // NOI18N
0256: if (!furi.startsWith("src/cached")) { // NOI18N
0257: uris.add(furi);
0258:
0259: // map retreived dir to cached dir..
0260: File cf = new File(mRetrieveToDirectory
0261: .getParentFile(), furi);
0262: File pcf = cf.getParentFile();
0263: File mpcf = (File) dirMap.get(pcf);
0264: if (mpcf == null) { // create a new cached dir for this
0265: String cacheDirName = null;
0266: int baseCount = FoldersListSettings
0267: .getDefault()
0268: .getNewProjectCount() + 1;
0269: String formater = NbBundle.getMessage(
0270: BpelProjectRetriever.class,
0271: "LBL_DefaultCacheDirName"); // NOI18N
0272: while ((cacheDirName = validFreeCacheDirName(
0273: mRetrieveToDirectory, formater,
0274: baseCount)) == null) {
0275: baseCount++;
0276: }
0277: mpcf = new File(mRetrieveToDirectory,
0278: cacheDirName);
0279: mpcf.mkdirs();
0280: dirMap.put(pcf, mpcf);
0281: }
0282:
0283: // move the file
0284: FileObject dst = FileUtil
0285: .toFileObject(FileUtil
0286: .normalizeFile(mpcf));
0287: FileObject src = FileUtil
0288: .toFileObject(FileUtil
0289: .normalizeFile(cf));
0290: FileUtil.moveFile(src, dst, src.getName());
0291:
0292: // update catalog.xml uirs... furi -> mpcf
0293: Attr atr = sys.getAttributeNode("uri"); // NOI18N
0294: atr.setNodeValue("src/" + mpcf.getName()
0295: + "/" + cf.getName());
0296: // System.out.println("newURI: "+sys.getAttribute("uri"));
0297: }
0298: }
0299:
0300: // remove retreived dirs..
0301: Iterator it = dirMap.keySet().iterator();
0302: while (it.hasNext()) {
0303: File key = (File) it.next();
0304: FileObject src = FileUtil.toFileObject(FileUtil
0305: .normalizeFile(key));
0306: FileObject root = FileUtil
0307: .toFileObject(FileUtil
0308: .normalizeFile(mRetrieveToDirectory));
0309: while (src.getParent() != root) {
0310: src = src.getParent();
0311: }
0312: src.delete();
0313: }
0314:
0315: // update catalog.xml
0316: DOMSource src = new DOMSource(catlogDoc);
0317: FileOutputStream fos = new FileOutputStream(catFile);
0318: StreamResult rest = new StreamResult(fos);
0319: TransformerFactory transFact = TransformerFactory
0320: .newInstance();
0321: Transformer transformer = transFact
0322: .newTransformer();
0323: transformer.transform(src, rest);
0324: }
0325: }
0326: }
0327:
0328: private String validFreeCacheDirName(final File parentFolder,
0329: final String formater, final int index) {
0330: String name = MessageFormat.format(formater,
0331: new Object[] { new Integer(index) });
0332: File file = new File(parentFolder, name);
0333: return file.exists() ? null : name;
0334: }
0335:
0336: /**
0337: * Process the list of source directories to generate JBI.xml
0338: * @param sourceDirs list of source directory
0339: */
0340: private void processSourceDirs(List sourceDirs) {
0341: Iterator it = sourceDirs.iterator();
0342: while (it.hasNext()) {
0343: File sourceDir = (File) it.next();
0344: processSourceDir(sourceDir);
0345: }
0346:
0347: }
0348:
0349: /**
0350: * Proces the source directory to generate JBI.xml
0351: * @param sourceDir
0352: */
0353: private void processSourceDir(File sourceDir) {
0354: processFileObject(sourceDir);
0355: }
0356:
0357: /**
0358: * Process the file object to generate JBI.xml
0359: * @param file BPEL file location
0360: */
0361: private void processFileObject(File file) {
0362: if (file.isDirectory()) {
0363: processFolder(file);
0364: } else {
0365: processFile(file);
0366: }
0367: }
0368:
0369: /**
0370: * Process the folder to generate JBI.xml
0371: * @param fileDir Folder location
0372: */
0373: private void processFolder(File fileDir) {
0374: File[] children = fileDir.listFiles();
0375:
0376: for (int i = 0; i < children.length; i++) {
0377: processFileObject(children[i]);
0378: }
0379: }
0380:
0381: /**
0382: * Process the file to generate JBI.xml
0383: * @param file input file
0384: */
0385: private void processFile(File file) {
0386: String fileName = file.getName();
0387: String fileExtension = null;
0388: int dotIndex = fileName.lastIndexOf('.');
0389: if (dotIndex != -1) {
0390: fileExtension = fileName.substring(dotIndex + 1);
0391: }
0392:
0393: if (fileExtension != null
0394: && fileExtension.equalsIgnoreCase("bpel")) {
0395:
0396: BpelModelFactory bpelFactory = (BpelModelFactory) Lookup
0397: .getDefault().lookup(BpelModelFactory.class);
0398: FileObject fobj = FileUtil.toFileObject(FileUtil
0399: .normalizeFile(file));
0400: ModelSource ms = null;
0401: try {
0402: ms = Utilities.createModelSource(fobj, true);
0403: } catch (Exception ex) {
0404: logger.log(Level.SEVERE,
0405: "Error encountered while creating module source - "
0406: + file.toURI());
0407: throw new RuntimeException(
0408: "Error encountered while creating module source - "
0409: + file.toURI());
0410: }
0411:
0412: BpelModel bm = bpelFactory.getModel(ms);
0413: org.netbeans.modules.bpel.model.api.Import[] imports = bm
0414: .getProcess().getImports();
0415: WSDLModel wsdlModel = null;
0416: SchemaModel schModel = null;
0417:
0418: for (org.netbeans.modules.bpel.model.api.Import imprt : imports) {
0419: wsdlModel = getWsdlModel(imprt);
0420: if (wsdlModel != null) {
0421: processWSDLImport(wsdlModel, imprt);
0422: /*
0423: Types types = wsdlModel.getDefinitions().getTypes();
0424: if (types != null) {
0425: Collection<org.netbeans.modules.xml.schema.model.Schema> schemas= types.getSchemas();
0426: for (Schema schema:schemas ){
0427:
0428: Collection<org.netbeans.modules.xml.schema.model.Import> colImports = schema.getImports();
0429:
0430: processSchemaImport(schema.getModel(), colImports);
0431: }
0432: }
0433:
0434:
0435: Collection<org.netbeans.modules.xml.wsdl.model.Import> colImports =
0436: wsdlModel.getDefinitions().getImports();
0437:
0438: if (colImports != null) {
0439: processWSDLImport(wsdlModel, colImports);
0440: }*/
0441: } else {
0442:
0443: SchemaModel scMdl = getSchemaModel(imprt);
0444: if (scMdl != null) {
0445: processSchemaImport(scMdl, imprt);
0446: /* String targetNameSpace = scMdl.getSchema().getTargetNamespace();
0447: String versionInfo =scMdl.getSchema().getVersion();
0448: if (versionInfo == null) {
0449: versionInfo = "";
0450: }
0451: if (!mVisitedXMLResources.contains(targetNameSpace+versionInfo)) {
0452: mVisitedXMLResources.add(targetNameSpace+versionInfo);
0453: Collection<org.netbeans.modules.xml.schema.model.Import> colImports = scMdl.getSchema().getImports();
0454: processSchemaImport(scMdl, colImports);
0455: } */
0456: }
0457: }
0458:
0459: }
0460:
0461: }
0462:
0463: }
0464:
0465: void processSchemaImport(SchemaModel scm,
0466: org.netbeans.modules.bpel.model.api.Import imports) {
0467: String importLocation = imports.getLocation();
0468: if (importLocation != null) {
0469: String importLocationLowerCase = importLocation;
0470: if (!mVisitedXMLResources
0471: .contains(importLocationLowerCase)) {
0472: mVisitedXMLResources.add(importLocationLowerCase);
0473: String resourceName = null;
0474: Document doc = scm.getDocument();
0475: if (doc != null) {
0476: resourceName = doc.getLocalName();
0477: } else {
0478: resourceName = "";
0479: }
0480: URI resourceURI = externalResource(resourceName,
0481: importLocationLowerCase);
0482: if (resourceURI != null) {
0483: if (!ApacheResolverHelper.isPresent(new File(
0484: CommandlineBpelProjectXmlCatalogProvider
0485: .getInstance()
0486: .getProjectWideCatalog())
0487: .getAbsolutePath(), resourceURI
0488: .toString())) {
0489: try {
0490: FileObject catalogFO = FileUtil
0491: .toFileObject(FileUtil
0492: .normalizeFile(new File(
0493: CommandlineBpelProjectXmlCatalogProvider
0494: .getInstance()
0495: .getRetrieverPath())));
0496: displayStatus(importLocationLowerCase);
0497:
0498: Retriever
0499: .getDefault()
0500: .retrieveResource(
0501: catalogFO,
0502: FileUtil
0503: .normalizeFile(
0504: new File(
0505: CommandlineBpelProjectXmlCatalogProvider
0506: .getInstance()
0507: .getProjectWideCatalog()))
0508: .toURI(),
0509: resourceURI);
0510:
0511: } catch (Exception ex) {
0512: logger.log(Level.SEVERE,
0513: "Error encountered while retreiving file - "
0514: + importLocation);
0515: }
0516: }
0517: } else {
0518: SchemaModel scMdl = getSchemaModel(imports);
0519: if (scMdl != null) {
0520: Collection<org.netbeans.modules.xml.schema.model.Import> subImports = scMdl
0521: .getSchema().getImports();
0522: processSchemaImport(scMdl, subImports);
0523: }
0524: }
0525: } else {
0526: // System.out.println(" ALREADY DOWNLOADED!!"+importLocationLowerCase);
0527: }
0528: }
0529:
0530: }
0531:
0532: void processSchemaImport(
0533: SchemaModel scm,
0534: Collection<org.netbeans.modules.xml.schema.model.Import> colImports) {
0535: for (org.netbeans.modules.xml.schema.model.Import imports : colImports) {
0536: String importLocation = imports.getSchemaLocation();
0537: if (importLocation != null) {
0538: String importLocationLowerCase = importLocation;
0539: if (!mVisitedXMLResources
0540: .contains(importLocationLowerCase)) {
0541: mVisitedXMLResources
0542: .add(importLocationLowerCase);
0543: String resourceName = null;
0544: Document doc = scm.getDocument();
0545: if (doc != null) {
0546: resourceName = doc.getLocalName();
0547: } else {
0548: resourceName = "";
0549: }
0550: URI resourceURI = externalResource(
0551: resourceName, importLocationLowerCase);
0552: if (resourceURI != null) {
0553: if (!ApacheResolverHelper
0554: .isPresent(
0555: new File(
0556: CommandlineBpelProjectXmlCatalogProvider
0557: .getInstance()
0558: .getProjectWideCatalog())
0559: .getAbsolutePath(),
0560: resourceURI.toString())) {
0561: try {
0562: /* Retriever.getDefault().retrieveResource(new File(CommandlineBpelProjectXmlCatalogProvider.getInstance().getRetrieverPath()),
0563: new URI(importLocation));
0564: */
0565: FileObject catalogFO = FileUtil
0566: .toFileObject(FileUtil
0567: .normalizeFile(new File(
0568: CommandlineBpelProjectXmlCatalogProvider
0569: .getInstance()
0570: .getRetrieverPath())));
0571: displayStatus(importLocationLowerCase);
0572:
0573: Retriever
0574: .getDefault()
0575: .retrieveResource(
0576: catalogFO,
0577: FileUtil
0578: .normalizeFile(
0579: new File(
0580: CommandlineBpelProjectXmlCatalogProvider
0581: .getInstance()
0582: .getProjectWideCatalog()))
0583: .toURI(),
0584: resourceURI);
0585:
0586: } catch (Exception ex) {
0587: logger.log(Level.SEVERE,
0588: "Error encountered while retreiving file - "
0589: + importLocation);
0590: //throw new RuntimeException(ex);
0591: }
0592: }
0593: } else {
0594: SchemaModel scMdl = getSchemaModel(imports);
0595: if (scMdl != null) {
0596: /* String targetNameSpace = scMdl.getSchema().getTargetNamespace();
0597: String versionInfo =scMdl.getSchema().getVersion();
0598: if (versionInfo == null) {
0599: versionInfo = "";
0600: }
0601: String xsdId = scMdl.getSchema().getId();
0602: if (xsdId == null) {
0603: xsdId = "";
0604: }
0605: String xsdKey = targetNameSpace+versionInfo+xsdId;
0606: if (!mVisitedXMLResources.contains(xsdKey)) {
0607: mVisitedXMLResources.add(xsdKey);*/
0608: Collection<org.netbeans.modules.xml.schema.model.Import> subImports = scMdl
0609: .getSchema().getImports();
0610: processSchemaImport(scMdl, subImports);
0611: //}
0612: }
0613: }
0614: } else {
0615: // System.out.println(" ALREADY DOWNLOADED!!"+importLocationLowerCase);
0616: }
0617: }
0618: }
0619:
0620: }
0621:
0622: void processWSDLImport(WSDLModel wsdlModel,
0623: org.netbeans.modules.bpel.model.api.Import imports) {
0624: String importLocation = imports.getLocation();
0625: String importLocationLowerCase = importLocation;
0626: String wsdlKey = importLocationLowerCase;
0627: if (!mVisitedXMLResources.contains(wsdlKey)) {
0628: mVisitedXMLResources.add(wsdlKey);
0629: String resourceName = null;
0630: Document doc = wsdlModel.getDocument();
0631: if (doc != null) {
0632: resourceName = doc.getLocalName();
0633: } else {
0634: resourceName = "";
0635: }
0636: URI resourceURI = externalResource(wsdlModel
0637: .getDocument().getLocalName(),
0638: importLocationLowerCase);
0639: if (resourceURI != null) {
0640: if (!ApacheResolverHelper.isPresent(new File(
0641: CommandlineBpelProjectXmlCatalogProvider
0642: .getInstance()
0643: .getProjectWideCatalog())
0644: .getAbsolutePath(), resourceURI.toString())) {
0645: try {
0646: /* Retriever.getDefault().retrieveResource(new File(CommandlineBpelProjectXmlCatalogProvider.getInstance().getRetrieverPath()),
0647: new URI(importLocation));
0648: */
0649: FileObject catalogFO = FileUtil
0650: .toFileObject(FileUtil
0651: .normalizeFile(new File(
0652: CommandlineBpelProjectXmlCatalogProvider
0653: .getInstance()
0654: .getRetrieverPath())));
0655: displayStatus(importLocationLowerCase);
0656:
0657: Retriever
0658: .getDefault()
0659: .retrieveResource(
0660: catalogFO,
0661: FileUtil
0662: .normalizeFile(
0663: new File(
0664: CommandlineBpelProjectXmlCatalogProvider
0665: .getInstance()
0666: .getProjectWideCatalog()))
0667: .toURI(),
0668: resourceURI);
0669:
0670: } catch (Exception ex) {
0671: logger.log(Level.SEVERE,
0672: "Error encountered while retreiving file - "
0673: + importLocation);
0674: //throw new RuntimeException(ex);
0675: }
0676: }
0677: } else {
0678: WSDLModel wm = wsdlModel;
0679:
0680: Collection<org.netbeans.modules.xml.wsdl.model.Import> subImports = wm
0681: .getDefinitions().getImports();
0682: if (subImports != null && subImports.size() > 0) {
0683: processWSDLImport(wm, subImports);
0684: }
0685: Types types = wm.getDefinitions().getTypes();
0686: if (types != null) {
0687: Collection<org.netbeans.modules.xml.schema.model.Schema> schemas = types
0688: .getSchemas();
0689:
0690: for (Schema schema : schemas) {
0691: Collection<org.netbeans.modules.xml.schema.model.Import> colImports1 = schema
0692: .getImports();
0693: processSchemaImport(schema.getModel(),
0694: colImports1);
0695: }
0696: }
0697: }
0698: } else {
0699: // System.out.println(" ALREADY DOWNLOADED!!"+importLocationLowerCase);
0700: }
0701: }
0702:
0703: void processWSDLImport(
0704: WSDLModel wsdlModel,
0705: Collection<org.netbeans.modules.xml.wsdl.model.Import> colImports) {
0706: for (org.netbeans.modules.xml.wsdl.model.Import imports : colImports) {
0707: String importLocation = imports.getLocation();
0708: String importLocationLowerCase = importLocation;
0709: String wsdlKey = importLocationLowerCase;
0710: if (!mVisitedXMLResources.contains(wsdlKey)) {
0711: mVisitedXMLResources.add(wsdlKey);
0712: String resourceName = null;
0713: Document doc = wsdlModel.getDocument();
0714: if (doc != null) {
0715: resourceName = doc.getLocalName();
0716: } else {
0717: resourceName = "";
0718: }
0719: URI resourceURI = externalResource(wsdlModel
0720: .getDocument().getLocalName(),
0721: importLocationLowerCase);
0722: if (resourceURI != null) {
0723: if (!ApacheResolverHelper.isPresent(new File(
0724: CommandlineBpelProjectXmlCatalogProvider
0725: .getInstance()
0726: .getProjectWideCatalog())
0727: .getAbsolutePath(), resourceURI
0728: .toString())) {
0729: try {
0730: /* Retriever.getDefault().retrieveResource(new File(CommandlineBpelProjectXmlCatalogProvider.getInstance().getRetrieverPath()),
0731: new URI(importLocation));
0732: */
0733: FileObject catalogFO = FileUtil
0734: .toFileObject(FileUtil
0735: .normalizeFile(new File(
0736: CommandlineBpelProjectXmlCatalogProvider
0737: .getInstance()
0738: .getRetrieverPath())));
0739: displayStatus(importLocationLowerCase);
0740:
0741: Retriever
0742: .getDefault()
0743: .retrieveResource(
0744: catalogFO,
0745: FileUtil
0746: .normalizeFile(
0747: new File(
0748: CommandlineBpelProjectXmlCatalogProvider
0749: .getInstance()
0750: .getProjectWideCatalog()))
0751: .toURI(),
0752: resourceURI);
0753:
0754: } catch (Exception ex) {
0755: logger.log(Level.SEVERE,
0756: "Error encountered while retreiving file - "
0757: + importLocation);
0758: //throw new RuntimeException(ex);
0759: }
0760: }
0761: } else {
0762: WSDLModel wm = getWsdlModel(imports);
0763: if (wm != null) {
0764: /* String targetNameSpace =
0765: wm.getDefinitions().getTargetNamespace();
0766: String wsdlName = wm.getDefinitions().getName();
0767: if (wsdlName == null) {
0768: wsdlName = "";
0769: }
0770: String wsdlKey = targetNameSpace + wsdlName;
0771: if (!mVisitedXMLResources.contains(wsdlKey)) {
0772: mVisitedXMLResources.add(wsdlKey);*/
0773:
0774: Collection<org.netbeans.modules.xml.wsdl.model.Import> subImports = wm
0775: .getDefinitions().getImports();
0776: if (subImports != null
0777: && subImports.size() > 0) {
0778: processWSDLImport(wm, subImports);
0779: }
0780: Types types = wm.getDefinitions()
0781: .getTypes();
0782: if (types != null) {
0783: Collection<org.netbeans.modules.xml.schema.model.Schema> schemas = types
0784: .getSchemas();
0785:
0786: for (Schema schema : schemas) {
0787: /* String targetNameSpace1 =
0788: schema.getTargetNamespace();
0789: String versionInfo1 = schema.getVersion();
0790: if (versionInfo1 == null) {
0791: versionInfo1 = "";
0792: }
0793: String xsdId1 = schema.getId();
0794: if (xsdId1 == null) {
0795: xsdId1 = "";
0796: }
0797: String xsdKey1 =
0798: targetNameSpace1 + versionInfo1 + xsdId1;
0799: if (!mVisitedXMLResources.contains(xsdKey1)) {
0800: mVisitedXMLResources.add(xsdKey1);
0801: */
0802: Collection<org.netbeans.modules.xml.schema.model.Import> colImports1 = schema
0803: .getImports();
0804: processSchemaImport(schema
0805: .getModel(), colImports1);
0806: // }
0807: }
0808: }
0809: }
0810: }
0811: } else {
0812: // System.out.println(" ALREADY DOWNLOADED!!"+importLocationLowerCase);
0813: }
0814:
0815: }
0816:
0817: }
0818:
0819: public void displayStatus(String stats) {
0820: //pg.progress(stats);
0821: mRetUpd.setProgressMessage(NbBundle.getMessage(
0822: BpelProjectRetriever.class, "LBL_Retrieving",
0823: new Object[] { stats }));
0824: }
0825:
0826: public WSDLModel getWsdlModel(Import imp) {
0827: if (!Import.WSDL_IMPORT_TYPE.equals(imp.getImportType())) {
0828: return null;
0829: }
0830: String location = imp.getLocation();
0831: WSDLModel wsdlModel;
0832: if (location == null) {
0833: return null;
0834: }
0835: try {
0836: URI uri = new URI(location);
0837: ModelSource source = CatalogModelFactory.getDefault()
0838: .getCatalogModel(
0839: imp.getModel().getModelSource())
0840: .getModelSource(uri,
0841: imp.getModel().getModelSource());
0842: wsdlModel = WSDLModelFactory.getDefault().getModel(
0843: source);
0844: } catch (URISyntaxException e) {
0845: wsdlModel = null;
0846: } catch (CatalogModelException e) {
0847: wsdlModel = null;
0848: }
0849: if (wsdlModel != null
0850: && wsdlModel.getState() == Model.State.NOT_WELL_FORMED) {
0851: return null;
0852: }
0853: return wsdlModel;
0854: }
0855:
0856: public SchemaModel getSchemaModel(Import imp) {
0857: if (!Import.SCHEMA_IMPORT_TYPE.equals(imp.getImportType())) {
0858: return null;
0859: }
0860: String location = imp.getLocation();
0861: SchemaModel schemaModel;
0862: if (location == null) {
0863: return null;
0864: }
0865: try {
0866: URI uri = new URI(location);
0867: ModelSource modelSource = CatalogModelFactory
0868: .getDefault().getCatalogModel(
0869: imp.getModel().getModelSource())
0870: .getModelSource(uri,
0871: imp.getModel().getModelSource());
0872:
0873: schemaModel = SchemaModelFactory.getDefault().getModel(
0874: modelSource);
0875: } catch (URISyntaxException e) {
0876: schemaModel = null;
0877: } catch (CatalogModelException e) {
0878: schemaModel = null;
0879: }
0880: if (schemaModel != null
0881: && schemaModel.getState() == Model.State.NOT_WELL_FORMED) {
0882: schemaModel = null;
0883: }
0884: return schemaModel;
0885: }
0886:
0887: public SchemaModel getSchemaModel(
0888: org.netbeans.modules.xml.schema.model.Import imp) {
0889: String location = imp.getSchemaLocation();
0890: SchemaModel schemaModel;
0891: if (location == null) {
0892: return null;
0893: }
0894: try {
0895: URI uri = new URI(location);
0896: ModelSource modelSource = CatalogModelFactory
0897: .getDefault().getCatalogModel(
0898: imp.getModel().getModelSource())
0899: .getModelSource(uri,
0900: imp.getModel().getModelSource());
0901:
0902: schemaModel = SchemaModelFactory.getDefault().getModel(
0903: modelSource);
0904: } catch (URISyntaxException e) {
0905: schemaModel = null;
0906: } catch (CatalogModelException e) {
0907: schemaModel = null;
0908: }
0909: if (schemaModel != null
0910: && schemaModel.getState() == Model.State.NOT_WELL_FORMED) {
0911: schemaModel = null;
0912: }
0913: return schemaModel;
0914: }
0915:
0916: public WSDLModel getWsdlModel(
0917: org.netbeans.modules.xml.wsdl.model.Import imp) {
0918: String location = imp.getLocation();
0919: WSDLModel wsdlModel;
0920: if (location == null) {
0921: return null;
0922: }
0923: try {
0924: URI uri = new URI(location);
0925: ModelSource source = CatalogModelFactory.getDefault()
0926: .getCatalogModel(
0927: imp.getModel().getModelSource())
0928: .getModelSource(uri,
0929: imp.getModel().getModelSource());
0930: wsdlModel = WSDLModelFactory.getDefault().getModel(
0931: source);
0932: } catch (URISyntaxException e) {
0933: wsdlModel = null;
0934: } catch (CatalogModelException e) {
0935: wsdlModel = null;
0936: }
0937: if (wsdlModel != null
0938: && wsdlModel.getState() == Model.State.NOT_WELL_FORMED) {
0939: return null;
0940: }
0941: return wsdlModel;
0942: }
0943:
0944: URI externalResource(String resourceName, String location) {
0945: try {
0946: if (location.startsWith("http:")
0947: || location.startsWith("https:")) {
0948: return new URI(location);
0949: }
0950: File resourceFile = null;
0951: File normalizedLocation = null;
0952: if (location.startsWith("file:")) {
0953: URI fileURI = new URI(location);
0954: resourceFile = new File(fileURI);
0955: normalizedLocation = FileUtil
0956: .normalizeFile(resourceFile);
0957: if (normalizedLocation.getAbsolutePath().replace(
0958: '\\', '/').indexOf(mProjectSourcePath) != -1) {
0959: return null;
0960: }
0961: if (resourceFile.exists()) {
0962: return normalizedLocation.toURI();
0963: }
0964: }
0965: if (resourceFile == null) {
0966: resourceFile = new File(location);
0967: }
0968: if (!resourceFile.exists()) {
0969: resourceFile = new File(this .mProjectSourcePath,
0970: location);
0971: }
0972: normalizedLocation = FileUtil
0973: .normalizeFile(resourceFile);
0974: if (normalizedLocation.exists()) {
0975: if (normalizedLocation.getAbsolutePath().replace(
0976: '\\', '/').indexOf(mProjectSourcePath) != -1) {
0977: return null;
0978: } else {
0979: return normalizedLocation.toURI();
0980: }
0981: }
0982:
0983: } catch (Exception ex) {
0984: logger.severe("Problem in the imported location "
0985: + location + " of " + resourceName);
0986: }
0987: return null;
0988: }
0989:
0990: }
0991:
0992: /*
0993: class ProgressDisplay implements Runnable {
0994: String compltedFlag = NbBundle.getMessage( BpelProjectRetriever.class, "LBL_Populate_Catalog_Complete" );
0995: ProgressHandle pg = null;
0996: boolean bContinueLooping = true;
0997: Thread t = null;
0998: public ProgressDisplay() {
0999: String initMsg = NbBundle.getMessage( BpelProjectRetriever.class, "LBL_Populate_Catalog" );
1000: pg = ProgressHandleFactory.createHandle(initMsg, new org.openide.util.Cancellable() {
1001: public boolean cancel() {
1002: try {
1003: pg.finish();
1004: }catch (Exception ex) {
1005:
1006: }
1007: return true;
1008: }});
1009: // ProgressHandleFactory.createProgressComponent(pg).setVisible(true);
1010: // sd.setStatusText(initMsg);
1011: pg.setDisplayName(initMsg);
1012: pg.start();
1013: }
1014:
1015: public void run() {
1016: while(true){
1017: synchronized(this) {
1018: while (bContinueLooping)
1019: try {
1020: wait();
1021: }catch (InterruptedException ie) {
1022: break;
1023: }
1024: }
1025: break;
1026: }
1027:
1028:
1029: }
1030:
1031: public void displayStatus(String stats ){
1032: pg.progress(stats);
1033: }
1034:
1035: public synchronized void endProgress() {
1036: bContinueLooping = false;
1037: pg.finish();
1038: notify();
1039: }
1040: }*/
1041:
1042: /**
1043: *
1044: */
1045: class RetrieverUpdater extends javax.swing.JPanel {
1046:
1047: private JComponent progress;
1048:
1049: /**
1050: * Creates new form ModuleUpdaterProgress
1051: */
1052: public RetrieverUpdater(ProgressHandle handle) {
1053: progress = ProgressHandleFactory
1054: .createProgressComponent(handle);
1055: initComponents();
1056: //#67914: On macosx, the background of JTextField is white even if non-editable:
1057: message.setBackground(javax.swing.UIManager
1058: .getColor("Panel.background")); // NOI18N
1059: }
1060:
1061: /** This method is called from within the constructor to
1062: * initialize the form.
1063: * WARNING: Do NOT modify this code. The content of this method is
1064: * always regenerated by the Form Editor.
1065: */
1066: private void initComponents() {//GEN-BEGIN:initComponents
1067: java.awt.GridBagConstraints gridBagConstraints;
1068:
1069: innerPanel = new javax.swing.JPanel();
1070: message = new javax.swing.JTextField();
1071:
1072: setLayout(new java.awt.GridBagLayout());
1073:
1074: setBorder(javax.swing.BorderFactory.createEmptyBorder(11,
1075: 11, 11, 11));
1076: innerPanel.setLayout(new java.awt.BorderLayout());
1077:
1078: innerPanel.add(progress, BorderLayout.CENTER);
1079:
1080: gridBagConstraints = new java.awt.GridBagConstraints();
1081: gridBagConstraints.gridx = 0;
1082: gridBagConstraints.gridy = 1;
1083: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
1084: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
1085: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
1086: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
1087: gridBagConstraints.weightx = 1.0;
1088: add(innerPanel, gridBagConstraints);
1089:
1090: message.setColumns(40);
1091: message.setEditable(false);
1092: message
1093: .setFont(javax.swing.UIManager
1094: .getFont("Label.font"));
1095: message.setBorder(null);
1096: message.setDisabledTextColor(javax.swing.UIManager
1097: .getColor("Label.foreground"));
1098: gridBagConstraints = new java.awt.GridBagConstraints();
1099: gridBagConstraints.gridx = 0;
1100: gridBagConstraints.gridy = 0;
1101: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
1102: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
1103: gridBagConstraints.weightx = 1.0;
1104: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
1105: add(message, gridBagConstraints);
1106:
1107: }//GEN-END:initComponents
1108:
1109: // Variables declaration - do not modify//GEN-BEGIN:variables
1110: public javax.swing.JPanel innerPanel;
1111: public javax.swing.JTextField message;
1112:
1113: // End of variables declaration//GEN-END:variables
1114:
1115: public void setProgressMessage(String name) {
1116: message.setText(name);
1117: }
1118: }
1119:
1120: }
|