View Javadoc

1   /* 
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: OntoText Lab. / SIRMA </p>
23   */
24  package org.semanticgov.ui.wsdl;
25  
26  import java.util.*;
27  import java.io.*;
28  
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  
32  import org.eclipse.core.resources.*;
33  import org.eclipse.core.runtime.*;
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  import org.wsmostudio.runtime.LogManager;
37  
38  public class WSDLCache {
39  
40  	private Map<String, String> uri2file = new HashMap<String, String>();
41  	private static WSDLCache instance;
42  
43  	private WSDLCache() {
44  	    reinit();
45  		ResourcesPlugin.getWorkspace().addResourceChangeListener(
46  				new WSDLResourceListener());
47  	}
48  
49  	public static WSDLCache getCache() {
50  		if (instance == null) {
51  			instance = new WSDLCache();
52  		}
53  		return instance;
54  	}
55  
56  	public String getWSDLFile(String uri) {
57  		return this.uri2file.get(uri);
58  	}
59  
60  	public Set<String> listWSDLURIs() {
61  		return this.uri2file.keySet();
62  	}
63  
64  	public void reinit() {
65  		this.uri2file.clear();
66  
67  		IProject[] projectList = ResourcesPlugin.getWorkspace().getRoot()
68  				.getProjects();
69  		for (int i = 0; i < projectList.length; i++) {
70  			scanPath(projectList[i]);
71  		}
72  	}
73  
74  	private void scanPath(IContainer directory) {
75  		IResource[] entries = null;
76  		try {
77  			entries = directory.members();
78  		} catch (CoreException coreEx) {
79  			return;
80  		}
81  		if (entries == null) {
82  			return;
83  		}
84  		for (int i = 0; i < entries.length; i++) {
85  			if (entries[i] instanceof IFile
86  					&& ("wsdl".equalsIgnoreCase(((IFile) entries[i])
87  							.getFileExtension()) || "wsdls"
88  							.equalsIgnoreCase(((IFile) entries[i])
89  									.getFileExtension()))) {
90  				processWsdlHeader((IFile) entries[i]);
91  			} else if (entries[i] instanceof IContainer) {
92  				scanPath((IContainer) entries[i]);
93  			}
94  		}
95  	}
96  
97      public void processWsdlHeader(IFile iFile) {
98          File file = iFile.getLocation().toFile();
99          Document doc = null;
100         try {
101             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
102             factory.setIgnoringElementContentWhitespace(true);
103 
104             DocumentBuilder builder = factory.newDocumentBuilder();
105             doc = builder.parse(file);
106         }
107         catch(Exception ex) {
108             LogManager.logError(file.getName() + ":" + ex.getMessage());
109         }
110         if (doc == null 
111                 || doc.getDocumentElement() == null) {
112             return;
113         }
114         Element root = doc.getDocumentElement();
115         if (root.getTagName().equals("wsdl:definitions")
116                 || root.getTagName().equals("wsdl:description")
117                 || root.getTagName().equals("definitions")
118                 || root.getTagName().equals("description")) {
119             String targetNS = root.getAttribute("targetNamespace");
120             if (targetNS != null 
121                     && targetNS.trim().length() > 0) {
122                 this.uri2file.put(targetNS, file.getAbsolutePath());
123             }
124         }
125     }
126 }