dataset.java

来自「Town是一个100% 纯Java API」· Java 代码 · 共 709 行 · 第 1/2 页

JAVA
709
字号
                }
                catch (SQLException sqle)
                {
                    throw new ConnectionException (sqle);
                }
            }
            //throw new SQLException (e.getMessage());
            throw new ConnectionException (e);
        }

        return this;
    }
    /**
      * This method was created in VisualAge.
      * @return java.lang.String[]
      */
    public String [] getColumnNames ()
    {
        return getColumnNames (PRESERVE_CASE);
    }
    /**
      * This method was created in VisualAge.
      * @return java.lang.String[]
      * @param caseMode int
      */
    public String[] getColumnNames (int caseMode)
    {
        Column headers[] = schema.getColumns ();

        int columns = headers.length;
        String names[] = new String[columns];
        if (caseMode == PRESERVE_CASE)
            for (int i = 1; i < columns; i++)
                names[i] = headers[i].name ();
        else if (caseMode == FORCE_LOWER_CASE)
            for (int i = 1; i < columns; i++)
                names[i] = headers[i].name ().toLowerCase();
        else if (caseMode == FORCE_LOWER_CASE)
            for (int i = 1; i < columns; i++)
                names[i] = headers[i].name ().toUpperCase();
        return names;
    }
    /**
       * Returns the KeyDef for the DataSet
       *
       * @return     a keydef
       */
    public abstract KeyDef getKeyDef() throws DataSetException;
    /**
      * Get Record at 0 based index position
      *
      * @param   pos
      * @return     an instance of the found Record
      * @exception   DataSetException
      * @exception   ConnectionException
      */
    public Record getRecord (int pos) throws DataSetException,
    ConnectionException
    {
        if (containsRecord (pos))
        {
            Record rec = (Record) this.records.elementAt(pos);
            if (this instanceof TableDataSet)
                rec.markForUpdate();
            recordRetrievedCount++;
            return rec;
        }
        throw new DataSetException ("Record not found at index: " + pos);
    }

    /**
       * Find Record at 0 based index position. This is an internal alternative
       * to getRecord which tries to be smart about the type of record it is.
       *
       * @param   pos
       * @return     an instance of the found Record
       * @exception   DataSetException
       */
    Record findRecord (int pos) throws DataSetException, ConnectionException
    {
        if (containsRecord (pos))
        {
            return (Record) this.records.elementAt(pos);
        }
        throw new DataSetException ("Record not found at index: " + pos);
    }

    /**
      * Returns this schema for this Data Set
      * @return com.workingdogs.town.Schema
      */
    public Schema getSchema ()
    {
        return schema;
    }
    /**
       * Classes extending this class must implement this method.
       *
       * @return     the select string
       */
    protected abstract String getSelectString()
            throws ConnectionException, DataSetException;
    /**
       Gets the tableName upon table data set creation
       @return string
     */
    public abstract String getTableName() throws DataSetException,
    ConnectionException;
    /**
       The number of records that were fetched with the last fetchRecords.

       @return int
     */
    public int lastFetchSize()
    {
        return lastFetchSize;
    }
    /**
       * Removes the records from the DataSet, but does not null the records out
       *
       * @return     an instance of myself
       */
    public void releaseRecords()
    {
        this.records = null;
        this.recordRetrievedCount = 0;
        this.lastFetchSize = 0;
        setAllRecordsRetrieved (false);
    }
    /**
       * Remove a record from the DataSet's internal storage
       *
       * @param   rec
       * @return     the record removed
       * @exception   DataSetException
       */
    public Record removeRecord (Record rec) throws DataSetException
    {
        Record removeRec = null;
        try
        {
            int loc = this.records.indexOf(rec);
            removeRec = (Record) this.records.elementAt (loc);
            this.records.removeElementAt (loc);
        }
        catch (Exception e)
        {
            throw new DataSetException ("Record could not be removed!");
        }
        return removeRec;
    }
    /**
       * Gets the ResultSet for this DataSet
       *
       * @return     the result set for this DataSet
       * @exception   ConnectionException
       * @exception   DataSetException
       */
    protected ResultSet resultSet() throws ConnectionException,
    DataSetException
    {
        if (this.resultSet == null)
            throw new DataSetException ("ResultSet is null.");

        return this.resultSet;
    }
    /**
       * Set all records retrieved
       *
       */
    protected void setAllRecordsRetrieved(boolean set)
    {
        this.allRecordsRetrieved = set;
    }
    /**
        Gets the number of Records in this DataSet. It is 0 based.

        @return number of Records in this DataSet
      */
    public int size() throws ConnectionException, DataSetException
    {
        if (records == null)
            fetchRecords ();
        return this.records.size();
    }
    /**
      * This method was created in VisualAge.
      * @return java.lang.String
      */
    public String toDTD ()
    {
        return schema.toDTD ();
    }
    /**
        This returns a represention of this DataSet
      */
    public String toString()
    {
        try
        {
            if (records == null)
                fetchRecords ();

            ByteArrayOutputStream bout = new ByteArrayOutputStream ();
            PrintWriter out = new PrintWriter (bout);
            if (schema != null)
                out.println (schema.toString ());
            for (int i = 0; i < size (); i++)
                out.println (findRecord (i));

            out.flush ();
            return bout.toString ();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace ();
        }
        return "{}";
    }
    /**
      * Produces a valid XML document based on this data
      * @param String listName for the entire XML document
      * @param String itemName for each XML item (per record)
      * @author Donald Ball <a href="mailto:balld@webslingerZ.com">balld@webslingerZ.com</a>
      * @author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
      * @return java.lang.String
      */
    public String toXML (String listName,
            String itemName) throws ConnectionException, DataSetException
    {
        return toXML (listName, itemName, PRESERVE_CASE, CHARACTER_ENTITIES, OMIT_NULLS);
    }
    /**
      * Produces a valid XML document based on this data
      * @param String listName for the entire XML document
      * @param String itemName for each XML item (per record)
      * @param int caseMode The case mode to be work in
      * @author Donald Ball <a href="mailto:balld@webslingerZ.com">balld@webslingerZ.com</a>
      * @author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
      * @return java.lang.String
      */
    public String toXML (String listName, String itemName,
            int caseMode) throws ConnectionException, DataSetException
    {
		return toXML (listName, itemName, caseMode, CHARACTER_ENTITIES, OMIT_NULLS);
	}
    /**
      * Produces a valid XML document based on this data
      * @param String listName for the entire XML document
      * @param String itemName for each XML item (per record)
      * @param int caseMode The case mode to be work in
	  * @param int characterMode How to encode special characters
      * @author Donald Ball <a href="mailto:balld@webslingerZ.com">balld@webslingerZ.com</a>
      * @author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
      * @return java.lang.String
      */
    public String toXML (String listName, String itemName,
            int caseMode, int characterMode) throws ConnectionException, DataSetException
    {
		return toXML (listName, itemName, caseMode, characterMode, OMIT_NULLS);
	}
    /**
      * Produces a valid XML document based on this data
      * @param String listName for the entire XML document
      * @param String itemName for each XML item (per record)
      * @param int caseMode The case mode to be work in
	  * @param int characterMode How to encode special characters
	  * @param int nullMode How should we handle null values
      * @author Donald Ball <a href="mailto:balld@webslingerZ.com">balld@webslingerZ.com</a>
      * @author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
      * @return java.lang.String
      */
    public String toXML (String listName, String itemName,
            int caseMode, int characterMode, int nullMode) throws ConnectionException, DataSetException
    {
        if (records == null)
            fetchRecords ();

        String names[] = getColumnNames(caseMode);
		Column columns[] = schema.getColumns ();
		
        StringBuffer sb = new StringBuffer();
        sb.append('<'+listName + ">\n");
        for (int i = 0; i < size (); i++)
        {
            Record rec = findRecord (i);
            sb.append("\t<"+itemName + ">\n");
            for (int j = 1; j < names.length; j++)
			{
				String value = rec.getAsString (j);
				if (value == null)
				{
					if (nullMode == OMIT_NULLS)
						continue;
					else if (nullMode == EMPTY_NULLS)
						value = "";
				}
				sb.append("\t\t<"+names[j] + '>');
				if (rec.getValue (i).isString () || rec.getValue (i).isNull ())
					sb.append (getEscapedValue (characterMode, value));
				else
					sb.append (value);
				sb.append ("</"+ names[j] + ">\n");
			}
            sb.append("\t</"+itemName + ">\n");
        }
        sb.append("</"+listName + ">");
        return sb.toString();
    }

	protected static String getEscapedValue(int characterMode, String value)
	{
		if (value.length() == 0)
			return value;
		if (characterMode == CHARACTER_ENTITIES)
		{
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < value.length (); i++)
			{
				char c = value.charAt (i);
				switch(c)
				{
					case '&':
						sb.append("&amp;");
						break;
					case '<':
						sb.append("&lt;");
						break;
					case '>':
						sb.append("&gt;");
						break;
					case '"':
						sb.append("&quot;");
						break;
					case '\'':
						sb.append("&apos;");
						break;
					default:
						sb.append(c);
				}
			}
			return sb.toString();
		} else if (characterMode == CHARACTER_CDATA)
		{
			int index;
			while ((index = value.indexOf("]]>")) >= 0)
				value = value.substring(0,index+2)+"&lt;"+value.substring(index+3);
			return "<![CDATA["+value+"]]>";
		}
		//else if (characterMode == CHARACTER_PRESERVE)
		return value;
	}

}

⌨️ 快捷键说明

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