View Javadoc

1   package net.sf.jlayercheck.util;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.TreeSet;
8   import java.util.logging.Logger;
9   
10  import javax.xml.parsers.DocumentBuilderFactory;
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import net.sf.jlayercheck.util.exceptions.ConfigurationException;
14  import net.sf.jlayercheck.util.model.FilesystemClassSource;
15  
16  import org.w3c.dom.Document;
17  import org.w3c.dom.Element;
18  import org.w3c.dom.Node;
19  import org.w3c.dom.NodeList;
20  import org.xml.sax.SAXException;
21  
22  /**
23   * Used to create XMLConfiguration objects from an xml configuration file.
24   * 
25   * @author webmaster@earth3d.org
26   */
27  public class XMLConfigurationParser {
28  	protected static Logger logger = Logger.getLogger("JLayerCheck"); 
29  
30  	protected String basedir = ".";
31  	
32  	public String getBasedir() {
33  		return basedir;
34  	}
35  
36  	public void setBasedir(String basedir) {
37  		this.basedir = basedir;
38  	}
39  
40  	/**
41  	 * Creates a new parser object that can be used to create XMLConfiguration objects.
42  	 */
43  	public XMLConfigurationParser() {
44  	}
45  
46      /**
47       * Parses the given configuration file and creates an XMLConfiguration object.
48       * 
49       * @param is InputStream that points to an XML configuration file
50       * @throws ConfigurationException 
51       * @throws SAXException
52       * @throws IOException
53       * @throws ParserConfigurationException 
54       */
55  	public XMLConfiguration parse(File f) throws ConfigurationException, SAXException, IOException, ParserConfigurationException {
56  		setBasedir(f.getParentFile().getAbsolutePath());
57  		return parse(new FileInputStream(f));
58  	}
59  	
60      /**
61       * Parses the given configuration file and creates an XMLConfiguration object.
62       * 
63       * @param is InputStream that points to an XML configuration file
64       * @throws ConfigurationException 
65       * @throws SAXException
66       * @throws IOException
67       * @throws ParserConfigurationException 
68       */
69  	public XMLConfiguration parse(InputStream is) throws ConfigurationException, SAXException, IOException, ParserConfigurationException {
70  		XMLConfiguration xmlConf = new XMLConfiguration();
71  		
72  		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
73  		Element docElem = doc.getDocumentElement();
74  
75  		// get the architecture tag
76  		if (!docElem.getNodeName().equalsIgnoreCase("jlayercheck")) {
77  			throw new ConfigurationException("Tag jlayercheck not found.");
78  		}
79  
80  		// get the tag sources and architecture
81  		NodeList configNodeList = docElem.getChildNodes();
82  		for(int i=0; i<configNodeList.getLength(); i++) {
83  			Node configNode = configNodeList.item(i);
84  			if (configNode.getNodeType() == Node.ELEMENT_NODE) {
85  				// Element elemConfig = (Element) configNode;
86  
87  				if (configNode.getNodeName().equals("sources")) {
88  					parseSourcesTag(configNode, xmlConf);
89  				}
90  				if (configNode.getNodeName().equals("architecture")) {
91  					parseArchitectureTag(configNode, xmlConf);
92  				}
93  			}
94  		}
95  		
96  		return xmlConf;
97  	}
98  
99  	protected void parseArchitectureTag(Node configNode, XMLConfiguration xmlConf) {
100 		// get the tag module
101 		NodeList modulesNodeList = configNode.getChildNodes();
102 		for(int i2=0; i2<modulesNodeList.getLength(); i2++) {
103 			Node moduleNode = modulesNodeList.item(i2);
104 			if (moduleNode.getNodeType() == Node.ELEMENT_NODE) {
105 				if (moduleNode.getNodeName().equals("module")) {
106 					parseModule(moduleNode, xmlConf);
107 				}
108 				if (moduleNode.getNodeName().equals("exclude")) {
109 					parseExclude(moduleNode, xmlConf);
110 				}
111 	            if (moduleNode.getNodeName().equals("entry")) {
112 	                parseEntry(moduleNode, xmlConf);
113 	            }
114 			}
115 		}
116 	}
117 
118 	/**
119 	 * Parses the entry tag.
120 	 * 
121 	 * @param entryNode
122 	 * @param xmlConf 
123 	 */
124 	protected void parseEntry(Node entryNode, XMLConfiguration xmlConf) {
125 	    Element elemEntry = (Element) entryNode;
126 	    xmlConf.addEntryClass(elemEntry.getAttribute("name").replaceAll("\\.", "/"));
127 	}
128 
129 	protected void parseExclude(Node excludeNode, XMLConfiguration xmlConf) {
130 		// get the tag package and dependency
131 		NodeList nodeList = excludeNode.getChildNodes();
132 		for(int i3=0; i3<nodeList.getLength(); i3++) {
133 			Node packageDepNode = nodeList.item(i3);
134 			if (packageDepNode.getNodeType() == Node.ELEMENT_NODE) {
135 				Element elemPackageDep = (Element) packageDepNode;
136 	
137 				// add a new package to the current module
138 				if (elemPackageDep.getNodeName().equalsIgnoreCase("package")) {
139 					String packageName = elemPackageDep.getAttribute("name");
140 	
141 					xmlConf.addPackageToExcludeList(packageName);
142 				}
143 			}
144 		}
145 	}
146 
147 	protected void parseModule(Node moduleNode, XMLConfiguration xmlConf) {
148 		Element elemModule = (Element) moduleNode;
149 		String moduleName = elemModule.getAttribute("name");
150 		xmlConf.addModuleDependency(moduleName, new TreeSet<String>());
151 	
152 		// get the tag package and dependency
153 		NodeList nodeList = moduleNode.getChildNodes();
154 		for(int i3=0; i3<nodeList.getLength(); i3++) {
155 			Node packageDepNode = nodeList.item(i3);
156 			if (packageDepNode.getNodeType() == Node.ELEMENT_NODE) {
157 				Element elemPackageDep = (Element) packageDepNode;
158 	
159 				// add a new package to the current module
160 				if (elemPackageDep.getNodeName().equalsIgnoreCase("package")) {
161 					String packageName = elemPackageDep.getAttribute("name");
162 	
163 					xmlConf.addPackageToModule(moduleName, packageName);
164 				}
165 	
166 				// add a new dependency to the current module
167 				if (elemPackageDep.getNodeName().equalsIgnoreCase("dependency")) {
168 					String dependencyName = elemPackageDep.getAttribute("name");
169 	
170 					xmlConf.addDependencyToModule(moduleName, dependencyName);
171 				}
172 			}
173 		}
174 	}
175 
176 	protected void parseSourcesTag(Node configNode, XMLConfiguration xmlConf) {
177 		// get the tag module
178 		NodeList sourcesNodeList = configNode.getChildNodes();
179 		for(int i2=0; i2<sourcesNodeList.getLength(); i2++) {
180 			Node sourceNode = sourcesNodeList.item(i2);
181 			if (sourceNode.getNodeType() == Node.ELEMENT_NODE) {
182 				Element elemSource = (Element) sourceNode;
183 				
184 				if (sourceNode.getNodeName().equals("filesystem")) {
185 					logger.finer("Filesystem: "+elemSource.getAttribute("bin"));
186 					
187 					xmlConf.addClassSource(new FilesystemClassSource(getBasedir(), elemSource.getAttribute("bin"), elemSource.getAttribute("src")));
188 				}
189 			}
190 		}
191 	}
192 }