Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/om/parser/Parser.java
6005 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.uma.om.parser;2223import java.io.File;24import java.io.IOException;25import java.io.InputStream;26import java.net.URL;27import java.util.Hashtable;28import java.util.Vector;2930import javax.xml.XMLConstants;31import javax.xml.parsers.DocumentBuilder;32import javax.xml.parsers.DocumentBuilderFactory;33import javax.xml.parsers.ParserConfigurationException;34import javax.xml.validation.Schema;35import javax.xml.validation.SchemaFactory;3637import org.w3c.dom.Document;38import org.w3c.dom.NamedNodeMap;39import org.w3c.dom.Node;40import org.w3c.dom.NodeList;41import org.xml.sax.EntityResolver;42import org.xml.sax.InputSource;43import org.xml.sax.SAXException;4445import com.ibm.uma.IConfiguration;46import com.ibm.uma.UMA;47import com.ibm.uma.UMAException;48import com.ibm.uma.om.Artifact;49import com.ibm.uma.om.Module;50import com.ibm.uma.om.Predicate;51import com.ibm.uma.om.PredicateList;52import com.ibm.uma.om.SubdirArtifact;53import com.ibm.uma.util.Logger;5455public class Parser implements EntityResolver {5657static String fileCurrentlyBeingParsed;58static public String getFileCurrentlyBeingParsed() {59return fileCurrentlyBeingParsed;60}6162UMA generator;63Vector<Module> modules = new Vector<Module>();64Hashtable<String,Module> modulesByFullName = new Hashtable<String,Module>();65DocumentBuilder domParser;6667public Parser(UMA generator) {68this.generator = generator;6970try {71URL schemaURL = getClass().getResource("module.xsd");72SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);73Schema schema = schemaFactory.newSchema(schemaURL);747576DocumentBuilderFactory domParserFactory = DocumentBuilderFactory.newInstance();77domParserFactory.setNamespaceAware(true);78domParserFactory.setSchema(schema);79domParserFactory.setXIncludeAware(true);8081domParser = domParserFactory.newDocumentBuilder();82//domParser.setEntityResolver(this);83domParser.setErrorHandler(new ParserErrorHandler());84} catch (ParserConfigurationException e) {85// TODO Auto-generated catch block86e.printStackTrace();87} catch (SAXException e) {88// TODO Auto-generated catch block89e.printStackTrace();90}919293}9495IConfiguration getConfiguration() {96return generator.getConfiguration();97}9899public Vector<Module> getModules() {100return modules;101}102103Document parseFile(String xmlFile) throws SAXException, IOException {104fileCurrentlyBeingParsed = xmlFile;105Document retval = domParser.parse(xmlFile);106fileCurrentlyBeingParsed = null;107return retval;108}109110void resolveModuleHierarchy() throws UMAException {111112generator.validateArtifactLocations();113114// Make sure that all modules have parents all the way to the root.115// If a hole is found and a module with artifacts of type subdir.116//System.out.println("resolveModuleHierarchy");117boolean modulesToBeChecked = true;118while (modulesToBeChecked) {119//System.out.println("resolveModuleHierarchy - modulesToBeChecked");120// Store modules in a hashtable by full name.121// e.g., root/gc/module.xml is 'gc'122// root/gc/modron/base/module.xml is 'gc/modron/base'123for ( Module module : modules ) {124if ( !module.evaluate() ) continue;125addModuleByLogicalFullname(module);126}127Vector<Module> newModules = new Vector<Module>();128modulesToBeChecked = false;129130for ( Module module : modules) {131if ( !module.evaluate() ) continue;132// ensure each parent of a module exists.133if ( module.isTopLevel() ) continue;134String[] parents = module.getParents();135for ( int depth=0; depth<module.getModuleDepth(); depth++ ) {136Module parentMod = getModuleByLogicalFullname(parents[depth]);137if ( parentMod == null ) {138// need to create the parent139String moduleXmlFilename = UMA.getUma().getRootDirectory();140for ( int d=1; d<=depth; d++ ) {141moduleXmlFilename = moduleXmlFilename + module.moduleNameAtDepth(d) + "/";142}143moduleXmlFilename = moduleXmlFilename + getConfiguration().getMetadataFilename();144parentMod = new Module(moduleXmlFilename);145addModuleByLogicalFullname(parentMod);146newModules.add(parentMod);147modulesToBeChecked = true;148}149}150}151modules.addAll(newModules);152for ( Module module : modules) {153if ( !module.evaluate() ) continue;154// ensure each parent of a module exists.155if ( module.isTopLevel()) continue;156String[] parents = module.getParents();157for ( int depth=0; depth<module.getModuleDepth(); depth++ ) {158Module parentMod = getModuleByLogicalFullname(parents[depth]);159boolean found = false;160String child = module.moduleNameAtDepth(depth+1);161for ( Artifact artifact : parentMod.getArtifacts() ){162if ( !artifact.evaluate()) continue;163if ( artifact.getType() == Artifact.TYPE_SUBDIR ) {164if ( artifact.getTargetName().equalsIgnoreCase(child) ) {165found = true;166}167}168}169if ( !found && !child.equalsIgnoreCase(UMA.getUma().getConfiguration().getMetadataFilename())) {170Module subdirModule = getModuleByLogicalFullname(parentMod.getLogicalFullName()+"/"+child);171Artifact artifact = new SubdirArtifact(child, subdirModule, parentMod);172parentMod.addArtifact(artifact);173}174}175}176}177178}179180void addAllModulesFoundInDiretory( File dir, Vector<String> moduleFilenames ) {181String rootDir = generator.getRootDirectory();182File [] dirListing = dir.listFiles();183Vector<File> directories = new Vector<File>();184for ( File file : dirListing ) {185if ( file.isDirectory() ) {186directories.add(file);187} else if ( file.getName().equalsIgnoreCase(generator.getConfiguration().getMetadataFilename()) ) {188String modulePath = file.getParent();189if ( modulePath.equalsIgnoreCase(rootDir) || rootDir.equalsIgnoreCase(modulePath+File.separator)) {190modulePath = "";191} else {192modulePath = file.getParent().substring(rootDir.length());193}194modulePath = modulePath.replace(File.separator, "/");195moduleFilenames.add(modulePath);196Logger.getLogger().println(Logger.InformationL2Log, "Adding " + modulePath);197}198}199for ( File file : directories ) {200addAllModulesFoundInDiretory(file, moduleFilenames);201}202}203204public Vector<String> getModuleFilenames(Vector<String> moduleFilenames) {205String rootDir = generator.getRootDirectory();206String metaDataFileName = generator.getConfiguration().getMetadataFilename();207Vector<String> filenames = new Vector<String>();208for ( String name : moduleFilenames ) {209filenames.add( rootDir + name + "/" + metaDataFileName);210}211return filenames;212}213214215public boolean parse() throws UMAException {216Vector<String> moduleFilenames = new Vector<String>();217File rootDirectory = new File(generator.getRootDirectory());218addAllModulesFoundInDiretory(rootDirectory, moduleFilenames);219220// parse it to determine directories to search for module.xml files.221moduleFilenames = getModuleFilenames(moduleFilenames);222223// parse all found module.xml files.224for (String moduleXmlFilename : moduleFilenames) {225try {226Logger.getLogger().println(Logger.InformationL2Log, "Parsing " + moduleXmlFilename);227Document doc = parseFile(moduleXmlFilename);228Module module = ModuleParser.parse(doc, moduleXmlFilename);229modules.add( module );230} catch (SAXException e) {231throw new UMAException("Error: Module " + moduleXmlFilename + " failed to parse.", e);232} catch (IOException e) {233throw new UMAException("Error: " + e.getMessage(), e);234}235}236237resolveModuleHierarchy();238239return true;240}241242static public void populatePredicateList(NodeList nodeList, PredicateList predicates) {243for ( int j=0; j<nodeList.getLength(); j++ ) {244Node item = nodeList.item(j);245NamedNodeMap attributes = item.getAttributes();246if ( attributes == null ) continue;247Node conditionNode = attributes.getNamedItem("condition");248int predicateType = Predicate.EXCLUDE_IF;249if ( item.getNodeName().equalsIgnoreCase("include-if") ) {250predicateType = Predicate.INCLUDE_IF;251} else if ( item.getNodeName().equalsIgnoreCase("exclude-if") ) {252predicateType = Predicate.EXCLUDE_IF;253} else continue;254predicates.add(new Predicate(predicateType,conditionNode.getNodeValue()));255}256}257258259260public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {261if (!systemId.endsWith("module.xsd")) {262throw new SAXException("Unable to resolve schema named [" + systemId + "]");263}264265InputStream stream = getClass().getResourceAsStream("module.xsd");266return new InputSource(stream);267}268269public Module getModuleByLogicalFullname(String name) {270return modulesByFullName.get(name);271}272273public void addModuleByLogicalFullname(Module module ) {274//System.out.println("adding logical name: " + module.getLogicalFullName());275modulesByFullName.put(module.getLogicalFullName(), module);276}277278279280}281282283