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

📄 contextmodelreader.java

📁 用JAVA实现排序等简单算法的演示
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		
		if (index < number) {
			rawData = event.getMetaContextData(index);
			result.append(rawData.getType() + " " + rawData.getName());
		}
		index = index + 1;
		while (index < number) {
			rawData = event.getMetaContextData(index);
			result.append(", " + rawData.getType() + " " + rawData.getName());
			index = index + 1;
		}
		result.append(")");
		return result.toString();
	}
	
	/** 
	 * 生成 AspectJ 文件的头部信息,主要是生成 import 一些必需类的语句
	 * @param out
	 */
	private void generateSensorHead(PrintWriter out) {
		// 必须引入 contextPoolManager 包中的类,主要是 CPM, Context 等类
		out.println("import contextPoolManager.*;");
		
		// 由于在 pointcut 中连接模型中总是使用方法和属性的全名(包括包、类名),因此我们发现无需引入有关的类
/*		
		ArrayList<String> classNameList = new ArrayList<String>(10);
		for (int i = 0; i < algorithmGroup.getAlgorithmNum(); i++) {
			AlgorithmData algorithm = algorithmGroup.getAlgorithmData(i);
			String className = algorithm.getImpClass();
			int classIndex;
			for (classIndex = 0; classIndex < classNameList.size(); classIndex++) {
				String name = classNameList.get(classIndex);
				if (name.equals(className)) break;
			}
			if (classIndex == classNameList.size()) {
				classNameList.add(className);
				out.println("import " + className + ";");
			}
		}
*/		
		// 生成一些简单的注释
		out.println();
		out.println("/** This Aspect is automatic generated by ContextModel");
		out.println(" *  Date: " + new Date());
		out.println(" */");
		out.println();
		
	}
	
	/**
	 * 查找 node 是否存在某个标签为 tagName 的节点为其祖先节点,如果存在返回该节点,否则返回 null。
	 * 注意,我们总认为 tagName 的节点是 ELEMENT_NODE 类型
	 */
	private Element findSuperNodeByTagName(Node node, String tagName) {
		if (node == null) return null;
		
		Node parent = node.getParentNode();
		while (parent != null) {
			if (parent.getNodeType() == Node.ELEMENT_NODE) {
				Element result = (Element)parent;
				if (result.getTagName().equalsIgnoreCase(tagName)) return result;
			}
			parent = parent.getParentNode();
		}
		
		return null;
	}
	
	/**
	 * 从以 subTreeRoot 为根的子树(包括subTreeRoot这个节点)中,查找标签为 tagName 的第一个节点,
	 * 注意,我们总认为 tagName 的节点是 ELEMENT_NODE 类型
	 */
	private Element findSubTreeNodeByTagName(Node subTreeRoot, String tagName) {
		if (subTreeRoot == null) return null;
		
		Node node = subTreeRoot;

		Element result = null;
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			result = (Element)node;
			if (result.getTagName().equalsIgnoreCase(tagName)) return result;
		}
		node = node.getFirstChild();
		while (node != null) {
			result = findSubTreeNodeByTagName(node, tagName);
			if (result != null) return result;
			node = node.getNextSibling();
		}
		return null;
	}

	/**
	 * 从 node 节点的所有儿子节点查找标签为 tagName 的节点,注意,我们总认为 tagName 的节点
	 * 是 ELEMENT_NODE 类型,而且这里只搜索node 的所有儿子,而没有递归地搜儿子节点的子节点。 
	 */
	private Element findChildNodeByTagName(Node node, String tagName) {
		if (node == null) return null;
		
		Node child = node.getFirstChild();
		while (child != null) {
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				Element result = (Element)child;
				if (result.getTagName().equalsIgnoreCase(tagName)) return result;
			}
			child = child.getNextSibling();
		}
		return null;
	}
	
	/**
	 * 从 node 开始(但不包括 node 节点),在它的兄弟中查找标签为 tagName 的下一个节点,注意,我们总认为
	 * tagName 的节点是 ELEMENT_NODE 类型,而且这里只搜索其兄弟节点(即同一层次的节点),而没有搜索兄弟 
	 * 节点的子节点。
	 */
	private Element findBrotherNodeByTagName(Node node, String tagName) {
		if (node == null) return null;
		
		Node brother = node.getNextSibling();

		while (brother != null) {
			if (brother.getNodeType() == Node.ELEMENT_NODE) {
				Element result = (Element)brother;
				if (result.getTagName().equalsIgnoreCase(tagName)) return result;
			}
			brother = brother.getNextSibling();
		}
		return null;
	}
	
	/**
	 * 获取一个 Element 节点 node 下的内容,例如获取 <class>algorithmImp/SortAlgorithmImp</class> 中的
	 * algorithmImp/SortAlgorithmImp,注意 像这种内容会是 node 的子节点,而且其节点类型是 TEXT_NODE
	 */
	private String getTextOfElement(Element node) {
		Text textNode = (Text)node.getFirstChild();
		String text = textNode.getData().trim();
		if ((text != null) && text.length() > 0) return fixup(text);
		else return "";
	}
	
	/**
	 * 获取某个 Element 节点 node 中的名为 attrName 的属性中的内容
	 */
	private String getAttributeOfElement(Element node, String attrName) {
		NamedNodeMap attributes = node.getAttributes();
		for (int i = 0; i < attributes.getLength(); i++) {
			Node attribute = attributes.item(i);
			if (attribute.getNodeName().equalsIgnoreCase(attrName)) {
				return new String(attribute.getNodeValue());
			}
		}
		return null;
	}
	
	/**
	 * 将读到 XML 文件重新打印出来,主要是为了调试时验证读写的正确性。下面的方法递归调用以遍历 DOM 树的所有节点
	 */
	private void write(PrintWriter out, Node node, String indent) {
		switch (node.getNodeType()) {
		case Node.DOCUMENT_NODE: {
			Document doc = (Document)node;
			out.println(indent+"<?xml version = '1.0' encoding = 'gb2312' ?>");
			Node child = doc.getFirstChild(); 
			while (child != null) {
				write(out, child, indent);
				child = child.getNextSibling();
			}
			break;
		}
		case Node.DOCUMENT_TYPE_NODE: {
			DocumentType doctype = (DocumentType)node;
			out.println("<!DOCTYPE " + doctype.getName() + ">");
			break;
		}
		case Node.ELEMENT_NODE: {
			Element elt = (Element)node;
			out.print(indent + "<" + elt.getTagName());
			
			NamedNodeMap attrs = elt.getAttributes();
			for (int i = 0; i < attrs.getLength(); i++) {
				Node a = attrs.item(i);
				out.print(" " + a.getNodeName() + "= '" + fixup(a.getNodeValue()) + "'" );
			}
			out.println(">");
			
			String newindent = indent + "    ";
			Node child = elt.getFirstChild();
			while (child != null) {
				write(out, child, newindent);
				child = child.getNextSibling();
			}
			
			out.println(indent + "</" + elt.getTagName() + ">");
			break;
		}
		case Node.TEXT_NODE: {
			Text textNode = (Text)node;
			String text = textNode.getData().trim();
			if ((text != null) && text.length() > 0) out.println(indent + fixup(text));
//			String text = textNode.getData();
//			out.println(indent + "$" + text + "$");
			break;
		}
		case Node.PROCESSING_INSTRUCTION_NODE: {
			ProcessingInstruction pi = (ProcessingInstruction)node;
			out.println(indent + "<?" + pi.getTarget() + " " + pi.getData() + "?>");
			break;
		}
		case Node.ENTITY_REFERENCE_NODE: {
			out.println(indent + "&" + node.getNodeName() + ";");
		}
		case Node.CDATA_SECTION_NODE: {
			CDATASection cdata = (CDATASection)node;
			out.println(indent + "<" + "![CDATA[" + cdata.getData() + "]]" + ">");
			break;
		}
		case Node.COMMENT_NODE: {
			Comment c = (Comment)node; 
			out.println(indent + "<!--" + c.getData() + "-->");
			break;
		}
		default:
			System.err.println("Ignoring node: " + node.getClass().getName());
			break;
		}
	}
	
	/**
	 * 修正其中的一些特别字符,包括 <, >, &, ", ' 这些字符在 XML 文件中有特别用处
	 */
	private String fixup(String s) {
		StringBuffer sb = new StringBuffer();
		int len = s.length();
		
		for (int i = 0; i < len; i++) {
			char c = s.charAt(i);
			switch (c) {
			default: sb.append(c); break;
			case '<': sb.append("&lt;"); break;
			case '>': sb.append("&gt;"); break;
			case '&': sb.append("&amp;"); break;
			case '"': sb.append("&quot;"); break;
			case '\'': sb.append("&apos;"); break;
			}
		}
		return sb.toString();
	}
}

/**
 * 用于处理解析中出现的错误的一个简单类,这个类只是负责打印出各种错误信息
 * @author carlven
 *
 */
class parserErrorHandler implements ErrorHandler {
	public void warning(SAXParseException e) {
		System.err.println("WARNING: " + e.getMessage());
	}
	public void error(SAXParseException e) {
		System.err.println("ERROR: " + e.getMessage());
	}
	public void fatalError(SAXParseException e) throws SAXException {
		System.err.println("FATAL: " + e.getMessage());
		throw e;
	}
}

⌨️ 快捷键说明

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