baseobject.java

来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 325 行

JAVA
325
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/base/data/BaseObject.java,v 1.1.1.1 2004/07/01 09:07:39 wang_j Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/07/01 09:07:39 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
package com.webpump.ui.base.data;


import java.io.*;
import java.util.*;

import org.eclipse.core.runtime.*;
import org.eclipse.pde.core.*;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.plugin.*;
import org.w3c.dom.*;


public abstract class BaseObject
	extends PlatformObject
	implements IBaseObject, ISourceObject, Serializable {
	
	public final static String INDENT = "   ";
			
	transient BaseModel model;
	transient BaseObject parent;
	private Vector comments;	
	boolean inTheModel;
	protected int[] range;
	
	public void setInTheModel(boolean value) {
		inTheModel = value;
	}

	public boolean isInTheModel() {
		return inTheModel;
	}

	protected void ensureModelEditable() throws CoreException {
		if (!model.isEditable()) {
			throwCoreException("Illegal attempt to change read-only site manifest model");
		}
	}
	protected void firePropertyChanged(
		String property,
		Object oldValue,
		Object newValue) {
		firePropertyChanged(this, property, oldValue, newValue);
	}
	protected void firePropertyChanged(
		BaseObject object,
		String property,
		Object oldValue,
		Object newValue) {
        if (model!=null)
		if (model.isEditable() && model instanceof IModelChangeProvider) {
			IModelChangeProvider provider = (IModelChangeProvider) model;
			provider.fireModelObjectChanged(object, property, oldValue, newValue);
		}
	}
	protected void fireStructureChanged(BaseObject child, int changeType) {
		fireStructureChanged(new BaseObject[] { child }, changeType);
	}
	protected void fireStructureChanged(
		BaseObject[] children,
		int changeType) {
		BaseModel model = getModel();
        if (model!=null)
		if (model.isEditable() && model instanceof IModelChangeProvider) {
			IModelChangeProvider provider = (IModelChangeProvider) model;
			provider.fireModelChanged(new ModelChangedEvent(changeType, children, null));
		}
	}
	
	public BaseDataObject getDataObject() {
		return model.getDataObject();
	}

	public BaseModel getModel() {
		return model;
	}
	
	protected String getChildNodeValue(Node node,String strName)
	{
		String strNodeValue = "";
		NodeList children = node.getChildNodes();

		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			String strNodeName = child.getNodeName().toLowerCase();
			
			if (strNodeName.equals(strName.toLowerCase()))
			{
			 	strNodeValue = getNodeValue(node);
			 	break;
			}				
		}	
		
		return strNodeValue;	
	}
	
	protected String getNodeValue(Node node)
	{	
		String strNodeValue = "";
		
		NodeList children = node.getChildNodes();
		for (int i=0; i<children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType()==Node.TEXT_NODE) {
				Node firstChild = node.getFirstChild();
				if (firstChild!=null)
				strNodeValue = firstChild.getNodeValue();
				break;
			}
		}
		
		return strNodeValue;
	}
		
	protected String getNodeAttribute(Node node, String name) {
		NamedNodeMap atts = node.getAttributes();
		Node attribute = null;
		if (atts != null)
		   attribute = atts.getNamedItem(name);
		if (attribute != null)
			return attribute.getNodeValue();
		return null;
	}

	protected int getIntegerAttribute(Node node, String name) {
		String value = getNodeAttribute(node, name);
		if (value != null) {
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException e) {
			}
		}
		return 0;
	}
	
	protected boolean getBooleanAttribute(Node node, String name) {
		String value = getNodeAttribute(node, name);
		if (value != null) {
			return value.equalsIgnoreCase("true");
		}
		return false;
	}
	
	public BaseObject getParent() {
		return parent;
	}

	protected void reset() {
	}

	protected void throwCoreException(String message) throws CoreException {
		Status status =
			new Status(IStatus.ERROR, PDECore.getPluginId(), IStatus.OK, message, null);
		throw new CoreException(status);
	}
	
	/**
	 * Write the children nodes repeatingly
	 * @param indent
	 * @param children
	 * @param writer
	 */
	protected void writeChildren(
		String indent,
		Vector children,
		PrintWriter writer) {
		for (int i = 0; i < children.size(); i++) {
			IWritable writable = (IWritable) children.get(i);
			writable.write(indent, writer);
		}
	}	

	protected void writeIfDefined(
		String indent,
		PrintWriter writer,
		String attName,
		String attValue) {
		if (attValue == null)
			return;
		writer.print(indent + attName + "=\"" + attValue + "\"");
	}	

	public static String getWritableString(String source) {
		if (source == null)
			return "";
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < source.length(); i++) {
			char c = source.charAt(i);
			switch (c) {
				case '&' :
					buf.append("&amp;");
					break;
				case '<' :
					buf.append("&lt;");
					break;
				case '>' :
					buf.append("&gt;");
					break;
				case '\'' :
					buf.append("&apos;");
					break;
				case '\"' :
					buf.append("&quot;");
					break;
				default :
					buf.append(c);
					break;
			}
		}
		return buf.toString();
	}

	public void restoreProperty(String name, Object oldValue, Object newValue)
		throws CoreException {
	}

	public void write(String indent, PrintWriter writer) {
	}

	public void setModel(BaseModel model) {
		this.model = model;
	}
	
	public void setParent(BaseObject parent) {
		this.parent = parent;
	}
	
	public Object[] getChild()
	{
		return new Object[0];
	}
	

	protected void bindSourceLocation(Node node, Map lineTable) {
		//if (XMLCore.NEW_CODE_PATHS) {
		//	bindSourceLocationNew(node, lineTable);
		//} else {
			bindSourceLocationOrig(node, lineTable);
		//}
	}

	public void bindSourceLocationNew(Node node, Map lineTable) {
		ISourceRange lines = (ISourceRange) lineTable.get(node);
		if (lines != null) {
			range = new int[2];
			range[0] = lines.getStartLine();
			range[1] = lines.getEndLine();
		}
	}

	public void bindSourceLocationOrig(Node node, Map lineTable) {
		Integer[] lines = (Integer[]) lineTable.get(node);
		if (lines != null) {
			range = new int[2];
			range[0] = lines[0].intValue();
			range[1] = lines[1].intValue();
		}
	}

	public void addComments(Node node) {
		comments = addComments(node, comments);
	}

	public Vector addComments(Node node, Vector result) {
		for (Node prev = node.getPreviousSibling();
			prev != null;
			prev = prev.getPreviousSibling()) {
			if (prev.getNodeType() == Node.TEXT_NODE)
				continue;
			if (prev instanceof Comment) {
				String comment = prev.getNodeValue();
				if (result == null)
					result = new Vector();
				result.add(comment);
			} else
				break;
		}
		return result;
	}
	
	public void writeComments(PrintWriter writer) {
		writeComments(writer, comments);
	}
	
	public void writeComments(PrintWriter writer, Vector source) {
		if (source == null)
			return;
		for (int i = 0; i < source.size(); i++) {
			String comment = (String) source.elementAt(i);
			writer.println("<!--" + comment + "-->"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}	
	
	public int getStartLine() {
		if (range == null)
			return -1;
		return range[0];
	}
	public int getStopLine() {
		if (range == null)
			return -1;
		return range[1];
	}	
}

⌨️ 快捷键说明

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