⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappingprocessor.java

📁 METAmorphoses is a system for flexible and easy-to-use generation of RDF metadata directly from a re
💻 JAVA
字号:
package cz.cvut.felk.cs.metamorphoses.mapping;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.CDATASection;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * <p>The main class of the mapping layer.</p> * @author Martin Svihla */public class MappingProcessor {	private static final String MAPPING_NS = "http://www.svihla.net/metamorphoses/mapping.dtd";		Document mapping;	String jdbcUrl;	String jdbcDriver;	String username;	String password;	/**	 * Create mapping connection instance (reads the mapping document from file and read database connection data)	 * @param mapping document filename Filename	 * @throws MappingProcessorException when processor initialization fails	 */	public MappingProcessor(String mappingFilename)		throws MappingProcessorException {		this.loadMapping(mappingFilename);		this.readDBConnection();	}	/**	 * Loads mapping document and builds DOM out of it.	 * @param mapping document filename filename 	 * @throws MappingProcessorException	 */	protected void loadMapping(String filename)		throws MappingProcessorException {		try {			DocumentBuilderFactory factory =				DocumentBuilderFactory.newInstance();			factory.setNamespaceAware(true);			DocumentBuilder builder = factory.newDocumentBuilder();			mapping = builder.parse(filename);		} catch (IOException e) {			throw new MappingProcessorException(filename + " not found.", e);		} catch (Exception e) {			throw new MappingProcessorException("Mapping document parsing failed.", e);		}	}	/*	public void readNS() {			NodeList list = this.mapping.getElementsByTagName("Namespace");			for (int i = 0; i < list.getLength(); i++) {				Element elem = (Element) list.item(i);				this.namespaces.put(					elem.getAttributeNS(MappingProcessor.MAPPING_NS, "prefix"),					elem.getAttributeNS(MappingProcessor.MAPPING_NS, "namespace"));			}		}	*/	/**	 * Returns content of header tag from mapping document. 	 * @return String content of RDF header.	 */	public String getHeader() {		NodeList list =			mapping.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.DOCUMENT_HEADER);		Element elem = (Element) list.item(list.getLength() - 1);		return getCDataContent(elem);	}	/**	 * Returns content of foot tag from mapping document. 	 * @return String content of RDF footer.	 */	public String getFoot() {		NodeList list =			mapping.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.DOCUMENT_FOOT);		Element elem = (Element) list.item(list.getLength() - 1);		return getCDataContent(elem);	}	/**	 * Gets content of CDATA from XML element	 * @param Element XML element	 * @return CDATA content	 */	private String getCDataContent(Element elem) {		String data = "";		if (elem != null) {			if ((elem.getChildNodes().item(1) != null)				& (elem.getChildNodes().item(1).getNodeType()					== Node.CDATA_SECTION_NODE)) {				CDATASection content =					(CDATASection) elem.getChildNodes().item(1);				data = content.getData();			}		}		return data;	}		/**	 * Reads database connection configuration from a mapping document.	 * @throws MappingProcessorException	 */	private void readDBConnection() throws MappingProcessorException {		NodeList db =			mapping.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.DATABASE_CONNECTON);		if (db.getLength() == 0) {			throw new MappingProcessorException("No database connection tag in mapping.");		}		Element elem = (Element) db.item(db.getLength() - 1);		if (elem != null) {			if (elem.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.JDBC_URL))				this.jdbcUrl =					elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.JDBC_URL);			if (elem.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.JDBC_DRIVER))				this.jdbcDriver =					elem.getAttributeNS(						MappingProcessor.MAPPING_NS,						MappingTags.JDBC_DRIVER);			if (elem.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.DB_USER))				this.username =					elem.getAttributeNS(						MappingProcessor.MAPPING_NS,						MappingTags.DB_USER);			if (elem.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.DB_PASSWORD))				this.password =					elem.getAttributeNS(						MappingProcessor.MAPPING_NS,						MappingTags.DB_PASSWORD);		}	}	/**	 * Creates list of all class names in the mapping (class name = template name in this case)	 * @return list of strings	 */	public List getAllClassNames() {		List nameList = new ArrayList();		NodeList classes =			mapping.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.CLASS);		for (int i = 0, n = classes.getLength(); i < n; i++) {			Element elem = (Element) classes.item(i);			if (elem				.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.TEMPLATE_NAME)) {				nameList.add(					elem.getAttributeNS(						MappingProcessor.MAPPING_NS,						MappingTags.TEMPLATE_NAME));			}		}		return nameList;	}		/**	 * Processor returns MappingClass with a specified template name.	 * @param String template name	 * @return MappingClass	 */	public MappingClass getClassByTemplateName(String templateName) {		NodeList classes =			mapping.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.CLASS);		Element elem, processedClass = null;		for (int i = 0, n = classes.getLength(); i < n; i++) {			elem = (Element) classes.item(i);			if (elem != null) {				if (elem					.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.TEMPLATE_NAME)					& (elem						.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.TEMPLATE_NAME)						.equals(templateName))) {					processedClass = elem;					break;				}			}		}				if (processedClass == null)			return null;		String rdfLabel = null, sql = null;		if (processedClass			.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.RDF_LABEL)) {			rdfLabel =				processedClass.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.RDF_LABEL);		}		if (processedClass.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.SQL))			sql =				processedClass.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.SQL);		MappingClass mc = new MappingClass(templateName, rdfLabel, sql);		NodeList list;		//conditions		list =			processedClass.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.CONDITION);		for (int i = 0, n = list.getLength(); i < n; i++) {			elem = (Element) list.item(i);			//try {			mc.getConditions().put(				elem.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.TEMPLATE_NAME),				createCondition(elem));			//} catch (Exception e) {			//	throw new MappingProcessorException(			//		"Processing condition from class " + templateName,			//		e);			//}		}		// variables		list =			processedClass.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.VARIABLE);		String variableName;		for (int i = 0, n = list.getLength(); i < n; i++) {			elem = (Element) list.item(i);			//try {			variableName =				elem.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.TEMPLATE_NAME);			mc.getVariables().put(				variableName,				elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.SQL_NAME));			//} catch (Exception e) {			//	throw new MappingProcessorException(			//		"Processing variable from class " + templateName,			//		e);			//}		}		// properties		list =			processedClass.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.PROPERTY);		for (int i = 0, n = list.getLength(); i < n; i++) {			elem = (Element) list.item(i);			//try {			mc.getProperties().put(				elem.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.TEMPLATE_NAME),				createProperty(elem));			//} catch (Exception e) {			//	throw new MappingProcessorException(			//		"Processing property from class " + templateName,			//		e);			//}		}		// attributes		mc.setAttributes(createAttributeList(processedClass));		return mc;	}			/**	 * Creates a MappingProperty object from XML element mmm:Property.	 * @param Element property element	 * @return created MappingProperty	 */	private MappingProperty createProperty(Element propertyElement) {		MappingProperty property;		//try {		String containerType = "";		if (propertyElement.hasAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.CONTAINER_TYPE)) {			containerType = propertyElement.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.CONTAINER_TYPE);		}		property =			new MappingProperty(				propertyElement.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.RDF_LABEL),				propertyElement.getAttributeNS(					MappingProcessor.MAPPING_NS,					MappingTags.SQL_NAME),				propertyElement.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.PREFIX),				propertyElement.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.SUFFIX),							containerType,				createAttributeList(propertyElement));		//			} catch (Exception e) {		//				throw new MappingProcessorException(		//					"Processing property from class " + templateName,		//					e);		//			}		return property;	}	/**	 * Creates a MappingCondition object from XML element mmm:ClassCondition	 * @param Element property element	 * @return created MappingCondition	 */	private MappingCondition createCondition(Element elem) {		return new MappingCondition(			elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.TEMPLATE_NAME),			elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.SQL_WHERE_STRING),			elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.SQL_TABLE_STRING),			elem.getAttributeNS(MappingProcessor.MAPPING_NS, MappingTags.COMMENT));	}	/**	 * Returns list of attributes of a MappingClass or MappingProperty object from XML element mmm:Class or mmm:Property	 * @param Element class or property element	 * @return List list of MappingAtributes	 */	private ArrayList createAttributeList(Element element) {		ArrayList attributes = new ArrayList();		NodeList list =			element.getElementsByTagNameNS(				MappingProcessor.MAPPING_NS,				MappingTags.ATTRIBUTE);		for (int i = 0, n = list.getLength(); i < n; i++) {			Element elem = (Element) list.item(i);			//elem.			if (elem.getParentNode().equals(element)) {				MappingAttribute ma =					new MappingAttribute(						elem.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.RDF_LABEL),						elem.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.SQL_NAME),						elem.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.PREFIX),						elem.getAttributeNS(							MappingProcessor.MAPPING_NS,							MappingTags.SUFFIX));				attributes.add(ma);			}		}		return attributes;	}	/**	 * @return	 */	public String getJdbcDriver() {		return jdbcDriver;	}	/**	 * @return	 */	public String getJdbcUrl() {		return jdbcUrl;	}	/**	 * @return	 */	public String getPassword() {		return password;	}	/**	 * @return	 */	public String getUsername() {		return username;	}	/*	public static void main(String args[]) throws MappingProcessorException {		MappingProcessor proc = new MappingProcessor("resources/mapping1.xml");		proc.readDBConnection();		System.err.println(proc.jdbcDriver);		MappingClass mc = null;		//try {		mc = proc.getClassByTemplateName("person");		//} catch (MappingProcessorException e) {		//}		///System.out.println(mc.getSql());		///System.out.println(mc.getPropertyRdf().get("firstName"));	}	*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -