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

📄 nodefactory.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package eti.bi.alphaminer.vo;


import java.io.IOException;
import java.io.StringReader;
import java.util.Hashtable;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import eti.bi.alphaminer.operation.operator.DefaultOperatorDefinitionConstant;
import eti.bi.alphaminer.operation.operator.NodeInfo;
import eti.bi.alphaminer.vo.BICase;
import eti.bi.alphaminer.vo.DataNode;
import eti.bi.alphaminer.vo.ModelNode;
import eti.bi.alphaminer.vo.Node;
import eti.bi.alphaminer.vo.OperatorNode;
import eti.bi.alphaminer.vo.TaskNode;

/**
 * NodeFactory is responsible for creating different kind of node instances
 * for a specific case.
 */
public class NodeFactory {


	
	/**
	 * Different type of node.
	 */
	public static final int TASK = 0;
	public static final int MODEL = 1;
	public static final int OPERATOR = 2;
	public static final int DATA = 3;

	/**
	 * Creates a specific kind of node.
	 * @param a_CaseID ID of the case the node to be added.
	 * @param a_NodeType type of the node to be created.
	 * @return a Node object.
	 */
	public static Node createNode(String a_CaseID, int a_NodeType) {
		Node nodeObject = null;

		if (a_NodeType == TASK)
			nodeObject = new TaskNode(a_CaseID);
		else if (a_NodeType == MODEL)
			nodeObject = new ModelNode(a_CaseID);
		else if (a_NodeType == OPERATOR)
			nodeObject = new OperatorNode(a_CaseID);
		else if (a_NodeType == DATA)
			nodeObject = new DataNode(a_CaseID);
		else
			System.err.println("In NodeFactory:createNode: invalid node type");

		return nodeObject;
	}

	/**
	 * Creates a specific kind of node by copying the given node.
	 * @param a_CaseID ID of the case the node to be added.
	 * @param a_Node the node to be copied.
	 * @return a Node object.
	 */
	public static Node createNode(String a_CaseID, Node a_Node) {
		Node newNode = null;
		if (a_Node instanceof TaskNode)
			newNode = new TaskNode(a_CaseID, (TaskNode) a_Node);
		else if (a_Node instanceof OperatorNode)
			newNode = new OperatorNode(a_CaseID, (OperatorNode) a_Node);
		else if (a_Node instanceof ModelNode)
			newNode = new ModelNode(a_CaseID, (ModelNode) a_Node);
		else if (a_Node instanceof DataNode)
			newNode = new DataNode(a_CaseID, (DataNode) a_Node);
		else
			System.err.println("In NodeFactory:createNode: invalid node type");
		return newNode;
	}

	/**
	 * Creates an Operator Node of a particular type.
	 * @param a_CaseID ID of the case the node to be added.
	 * @param a_OperatorNodeType type of operator node to be created.
	 * @return a Node object.
	 */
	public static Node createOperatorNode(
		String a_CaseID,
		NodeInfo a_NodeInfo) {
		OperatorNode nodeObject = new OperatorNode(a_CaseID,a_NodeInfo);
		//nodeObject.setOperatorNodeType(a_OperatorNodeType);		
		return (Node) nodeObject;
	}
	
	/**
	 * Inserts header information and TaskNode into the Case base on 
	 * the node content provided.
	 * @param a_Case Case instance to be set.
	 * @param a_CaseString Content of the Case to be created.
	 * @return the Case instance after adding the information.
	 */
	public static BICase insertHeaderAndTaskNode(
		BICase a_Case,
		String a_CaseString) {
		DefaultHandler aHandler = new CMLSAXHandler(a_Case);
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			StringReader aStringReader = new StringReader(a_CaseString);
			saxParser.parse(new InputSource(aStringReader), aHandler);
		} catch (SAXException e) {
			// TODO
		} catch (ParserConfigurationException e) {
		} catch (IOException e) {
		}
		return ((BISAXHandler) aHandler).getCaseObject();
	}

	/**
	 * Inserts operator nodes and process into the Case base on 
	 * the case content provided.
	 * @param a_Case Case instance to be set.
	 * @param a_CaseString Content of the Case to be created.
	 * @return the Case instance after adding the information.
	 */
	public static BICase insertProcessNodes(BICase a_Case, String a_CaseString) {
		DefaultHandler aHandler = new PMLSAXHandler(a_Case);

		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			StringReader aStringReader = new StringReader(a_CaseString);
			saxParser.parse(new InputSource(aStringReader), aHandler);
		} catch (SAXException e) {
			// TODO
		} catch (ParserConfigurationException e) {
		} catch (IOException e) {
		}
		return ((BISAXHandler) aHandler).getCaseObject();
	}
}

/**
 * BISAXHandler is responsible for parsing XML documents to create instance of
 * different classes.
 */
class BISAXHandler extends DefaultHandler {
	
	/**
	 * Instances with properties based on the XML contents
	 */
	eti.bi.alphaminer.vo.Node m_Node = null;
	BICase m_Case = null;

	/**
	 * Gets the Case after setting the case properties.
	 * @return Case being set.
	 */
	public BICase getCaseObject() {
		return m_Case;
	}
}

/**
 * CMLSAXHandler is responsible for pasing CML which is a well-structured document
 * in XML format.
 */
class CMLSAXHandler extends BISAXHandler {

	final static String a_Type = "CML";
	StringBuffer m_TextBuffer;
	String m_StartElement;
	Hashtable<String, String> m_AttrMap;
	StringBuffer m_ElementTree = new StringBuffer("");
	
	/**
	 * Constructs a CMLSAXHandler.
	 * @param a_Case the Case object to be set.
	 */
	public CMLSAXHandler(BICase a_Case) {
		super();
		m_Node = new TaskNode();
		m_Case = a_Case;
	}

	/**
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] buf, int offset, int len)
		throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#endElement(String, String, String)
	 */
	public void endElement(
		String aNamespaceURI,
		String aSimpleName,
		String aQualifiedName)
		throws SAXException {
		String aElementName = aSimpleName; // element name
		if ("".equals(aElementName))
			aElementName = aQualifiedName; // not namespaceAware
		if (aElementName.equals("CML"))
			m_Case.setNode((TaskNode) m_Node);
		m_AttrMap = null;
	}

	/**
	 * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
	 */
	public void startElement(
		String aNamespaceURI,
		String aSimpleName,
		String aQualifiedName,
		Attributes aAttrs)
		throws SAXException {
		String aElementName = aSimpleName; // element name
		m_AttrMap = new Hashtable<String, String>();
		if ("".equals(aElementName)) {
			aElementName = aQualifiedName; // not namespaceAware
		}

		if (aAttrs != null) {
			for (int i = 0; i < aAttrs.getLength(); i++) {
				String aAttrName = aAttrs.getLocalName(i); // Attr name
				if ("".equals(aAttrName))
					aAttrName = aAttrs.getQName(i);
				m_AttrMap.put(aAttrName, aAttrs.getValue(i));
			}
		}

		if (aElementName.equals("CreateDate")) {
			m_Case.setCreateDate(m_AttrMap.get("value"));
		} else if (aElementName.equals("CaseDescription")) {
			m_Case.setCaseDescription(m_AttrMap.get("value"));
		} else if (aElementName.equals("Status")) {
			m_Case.setStatus(m_AttrMap.get("value"));
		//<<21/02/2005 Mark Li: Add Case Name
		} else if (aElementName.equals("CaseName")) {
			((TaskNode) m_Node).setCaseName(m_AttrMap.get("value"));	
        //21/02/2005 Mark Li: Add Case Name>>	

⌨️ 快捷键说明

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