Java tutorial
/** * PureInfo Dolphin * @(#)ScriptBlock.java 1.0 2005-8-29 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.dolphin.script.lang; import java.util.ArrayList; import java.util.List; import org.dom4j.Element; import com.pureinfo.force.container.ContainerUtil; import com.pureinfo.force.container.IClearable; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.xml.IXMLSupporter; /** * <P> * Created on 2005-8-29 22:44:50 <BR> * Last modified on 2005-8-29 * </P> * ScriptBlock: Dolphin View script block. * * @author Why * @version 1.0, 2005-8-29 * @since Dolphin 1.0 */ public class ScriptBlock implements IClearable, IXMLSupporter { //contents list: String or Expression private List m_contents; private int m_nStatementCount; /** * Constructor */ public ScriptBlock() { super(); m_contents = new ArrayList(); m_nStatementCount = 0; } /** * @see com.pureinfo.force.container.IClearable#clear() */ public void clear() { if (m_contents.isEmpty()) return; //else ContainerUtil.clear(m_contents); m_nStatementCount = 0; } /** * Returns <code>true</code> if the this block has no content. * * @return <code>true</code> if the this block has no content; * <code>false</code> otherwise. */ public boolean isEmpty() { return m_contents.isEmpty(); } /** * Returns the contents size. * * @return the contents size */ public int size() { return m_contents.size(); } /** * Returns the contents list in this block. * * @return the contents list in this block. */ public List getContents() { return m_contents; } /** * Adds a string content. * * @param _sContent * a string content. */ public void addContent(String _sContent) { m_contents.add(_sContent); } /** * Adds a script statement. * * @param _statement * a script statement. */ public void addContent(Statement _statement) { m_contents.add(_statement); m_nStatementCount++; } /** * Returns the total number of the script statements in this block. * * @return the total number of the script statements in this block. */ public int getStatementCount() { return m_nStatementCount; } /** * Returns <code>true</code> if there is one or more statements in this * block. * * @return <code>true</code> if there is one or more statements in this * block; <code>false</code> otherwise. */ public boolean hasStatement() { return m_nStatementCount > 0; } /** * @see java.lang.Object#toString() */ public String toString() { StringBuffer buff = new StringBuffer(); Object content; try { for (int i = 0; i < m_contents.size(); i++) { content = m_contents.get(i); if (content instanceof String) { buff.append((String) content); } else { buff.append(content.toString()); } } return buff.toString(); } finally { buff.setLength(0); } } //========================================================================= //implementation for IXMLSupporter /** * @see com.pureinfo.force.xml.IXMLSupporter#toXMLElement(org.dom4j.Element) */ public void toXMLElement(Element _element) throws PureException { Object content; Element element; for (int i = 0; i < m_contents.size(); i++) { content = m_contents.get(i); if (content instanceof String) { element = _element.addElement("text"); element.addCDATA((String) content); } else { element = _element.addElement("stmt"); ((IXMLSupporter) content).toXMLElement(element); } } } /** * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element) */ public void fromXML(Element _element) throws PureException { List children = _element.elements(); if (children == null || children.isEmpty()) return; Element element; String sName; Statement statement; for (int i = 0; i < children.size(); i++) { element = (Element) children.get(i); sName = element.getName(); if (sName.equals("statement")) { this.addContent(element.getText()); } else { statement = new Statement(); statement.fromXML(element); this.addContent(statement); } } } }