001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020: /*
021: * Schema.java
022: *
023: * Created on December 31, 2003, 11:29 AM
024: */
025:
026: package net.sf.borg.model.db.file.mdb;
027:
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.Map;
031: import java.util.Vector;
032:
033: import net.sf.borg.common.VMap;
034: import net.sf.borg.common.XTree;
035:
036: /**
037: *
038: * @author mbb
039: */
040:
041: // Class Schema keeps a mapping of field names to types
042: // It is used by class Row to determine the Class for conversion
043: // of field data to and from String
044: // It is used by genMDBObject to generate KeyedBean and FileBeanAdapter classes
045: public class Schema {
046: // ---------------------------------------------------------------------------
047: // SCHEMA
048: // ---------------------------------------------------------------------------
049:
050: // the mapping of type names to java classes
051: // these are the types supported by SMDB
052: static private HashMap typeMap_;
053: static {
054: typeMap_ = new HashMap();
055: typeMap_.put("String", java.lang.String.class);
056: typeMap_.put("Integer", java.lang.Integer.class);
057: typeMap_.put("Date", Date.class);
058: typeMap_.put("StringVector", Vector.class);
059: };
060:
061: // VMap is pretty much a Java Map. It holds the schema's
062: // fieldname to type mapping
063: private VMap map_;
064:
065: private boolean init_ = false;
066:
067: // gets the type of a given field
068: public String getType(String field) throws Exception {
069: String t = (String) map_.get(field);
070: if (t == null)
071: throw new Exception("Field: " + field
072: + " not found in schema");
073: return (t);
074: }
075:
076: // gets the class used to represent a given field
077: public Class getClass(String field) throws Exception {
078: return ((Class) typeMap_.get(getType(field)));
079: }
080:
081: // get the nth field name in the schema
082: public String getField(int n) {
083: if (n > map_.size())
084: return null;
085:
086: Object o[] = map_.entrySet().toArray();
087: Map.Entry me = (Map.Entry) o[n - 1];
088:
089: return ((String) me.getKey());
090: }
091:
092: // get the number of fields in the schema
093: public int numCols() {
094: return (map_.size());
095: }
096:
097: public Schema() {
098: map_ = new VMap();
099: }
100:
101: // add a field to the schema of a given type
102: // error if the schema is already used by a DB - cannot change
103: // the schema read from an active DB. you can add to schemas
104: // that have yet to be written out to a DB
105: // this is because all of the active Row objects from a DB will
106: // be using the same schema object
107: public void add(String field, String type) throws Exception {
108: if (init_ == true)
109: throw new Exception("In-use Schema cannot be changed");
110: map_.put(field, type);
111: }
112:
113: // output schema as text (to be stored in DB in a SCHEMA system Row)
114: public String toString() {
115:
116: return (map_.passivate());
117: }
118:
119: // init a schema from its text form
120: public void set(String txt) throws Exception {
121: init_ = true;
122: map_.activate(txt);
123: }
124:
125: // this reads a schema from a URL in **XML**
126: public void setFromXML(XTree xt) throws Exception {
127:
128: for (int i = 1;; i++) {
129: XTree field = xt.child("field", i);
130: if (!field.exists())
131: break;
132:
133: XTree name = field.child("name");
134: if (!name.exists())
135: throw new Exception(
136: "Cannot parse Schema XML - field found with no name");
137: XTree type = field.child("type");
138: if (!type.exists())
139: throw new Exception("Cannot parse Schema XML - field "
140: + name.value() + " found with no type");
141: add(name.value(), type.value());
142: //System.out.println(name.value() + "--" + type.value() );
143: }
144: }
145:
146: }
|