001: package net.xoetrope.data;
002:
003: import java.io.BufferedWriter;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.io.OutputStreamWriter;
007: import java.io.Reader;
008: import java.io.Writer;
009: import java.util.Enumeration;
010: import java.util.Vector;
011:
012: import net.xoetrope.debug.DebugLogger;
013: import net.xoetrope.xml.XmlElement;
014: import net.xoetrope.xml.XmlSource;
015: import net.xoetrope.xui.XProjectManager;
016: import net.xoetrope.xui.XResourceManager;
017: import net.xoetrope.xui.build.BuildProperties;
018: import net.xoetrope.xui.data.XModel;
019:
020: /**
021: * <p>Loads a model from a reader</p>
022: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
023: * License: see license.txt
024: * $Revision: 1.22 $
025: */
026: public class XDataSource {
027: private static int nextId = 0;
028:
029: public XDataSource() {
030: }
031:
032: /**
033: * Read a model from the Reader
034: * @param r the Reader
035: */
036: public void read(Reader r) {
037: XmlElement ele = XmlSource.read(r);
038: read(ele);
039: }
040:
041: /**
042: * Read an file pointed to by an element in the XML description of the data sources
043: * @param ele the individual data source description
044: */
045: public void read(XmlElement ele) {
046: Vector v = ele.getChildren();
047: if (v != null) {
048: int numFiles = v.size();
049: for (int i = 0; i < numFiles; i++) {
050: try {
051: XmlElement source = (XmlElement) v.elementAt(i);
052: String fileName = source.getAttribute("filename");
053: if (fileName != null) {
054: readDataSource(fileName, source);
055: }
056: } catch (Exception ex) {
057: ex.printStackTrace();
058: }
059: }
060: }
061: }
062:
063: /**
064: * Read the data source file.
065: * @param fileName the nam eof the file to open
066: * @param source the XML element describing the source
067: */
068: protected void readDataSource(String fileName, XmlElement source) {
069: try {
070: Reader sr = XResourceManager.getBufferedReader(fileName,
071: null);
072:
073: XmlElement src = XmlSource.read(sr);
074: String type = source.getAttribute("type");
075: if ((type == null) || (type.length() == 0))
076: loadTable(src, XProjectManager.getModel());
077: } catch (Exception ex) {
078: }
079: }
080:
081: /**
082: * Recursively load the model data
083: * @param source the source element
084: * @param model the model for the source element
085: */
086: public void loadTable(XmlElement source, XModel model) {
087: // Store the node tag name so that the XML can be regenerated/output
088: model.setTagName(source.getName().toString());
089:
090: Vector children = source.getChildren();
091: Enumeration attributes = source.enumerateAttributeNames();
092: boolean hasAutoId = false;
093:
094: int numChildren = children.size();
095:
096: // Count the number of attributes
097: int numAttributes = 0;
098: while (attributes.hasMoreElements()) {
099: attributes.nextElement();
100: numAttributes++;
101: }
102: model.setNumAttributes(numAttributes);
103:
104: // Add the attributes of the source element
105: attributes = source.enumerateAttributeNames();
106: while (attributes.hasMoreElements()) {
107: String attribName = ((String) attributes.nextElement())
108: .toLowerCase();
109: model.setAttribValue(model.getAttribute(attribName), source
110: .getAttribute(attribName));
111: }
112:
113: // Add the children of the source element
114: for (int i = 0; i < numChildren; i++) {
115: XmlElement ele = (XmlElement) children.elementAt(i);
116: Object elementName = ele.getAttribute("id");
117: // Give a unique id for unnamed elements
118: if (elementName == null) {
119: elementName = String.valueOf(getNextId());
120: hasAutoId = true;
121: }
122:
123: // Create a node/model for the child
124: XModel childModel = (XModel) model
125: .get((String) elementName);
126: // XModel childModel = ( XModel )model.append( ( String )elementName );
127:
128: // Allocate store for the child's children
129: childModel.hasAutoId(hasAutoId);
130: childModel.setNumChildren(Math.max(childModel
131: .getNumChildren(), ele.getChildren().size()));
132:
133: // Recurse to add the childs details
134: loadTable(ele, childModel);
135: }
136: }
137:
138: /**
139: * Get the next ID for anonymous elements
140: * @return
141: */
142: private int getNextId() {
143: return nextId++;
144: }
145:
146: /**
147: * Outputs the datasource as XML
148: * @param w The Writer of the file.
149: */
150: public void write(Writer w) {
151: write(w, XProjectManager.getModel());
152: }
153:
154: /**
155: * Outputs the datasource as XML
156: * @param w The Writer of the file.
157: */
158: public void write(Writer w, XModel model) {
159: try {
160: outputModel(w, model);
161: w.flush();
162: w.close();
163: } catch (IOException ex) {
164: ex.printStackTrace();
165: }
166: }
167:
168: /**
169: * Iterate the XModels and outputs the Elements and their attributes.
170: * @param w the output writer
171: * @param model the model to write
172: */
173: public static void outputModel(Writer w, XModel model) {
174: try {
175: w.write("<" + model.getTagName());
176: for (int j = 0; j < model.getNumAttributes(); j++) {
177: if ((model.getAttribValue(j) != null)
178: && (model.getAttribValue(j).toString().length() > 0))
179: if (!(model.getAttribName(j).compareTo("name") == 0 && model
180: .hasAutoId())) {
181: String valueStr = (String) model
182: .getAttribValue(j);
183: int pos = valueStr.indexOf("\"");
184: if (pos >= 0)
185: w.write(" " + model.getAttribName(j) + "="
186: + "\'" + valueStr + "\'");
187: else
188: w.write(" " + model.getAttribName(j) + "="
189: + "\"" + valueStr + "\"");
190: }
191: }
192: w.write(">\n");
193:
194: for (int i = 0; i < model.getNumChildren(); i++) {
195: XModel childModel = model.get(i);
196: outputModel(w, childModel);
197: }
198: w.write("</" + model.getTagName() + ">\n");
199: } catch (IOException ex) {
200: ex.printStackTrace();
201: }
202: }
203:
204: /**
205: * Iterate the XModels and outputs the Elements and their attributes.
206: * @param filename the file to write
207: * @param model the model to write
208: */
209: public void outputModel(String filename, XModel model) {
210: if (BuildProperties.DEBUG)
211: DebugLogger.trace("savePath:" + filename);
212:
213: if (filename != null) {
214: try {
215: if (filename.length() > 1) {
216: FileOutputStream fos = new FileOutputStream(
217: filename);
218: OutputStreamWriter osw = new OutputStreamWriter(
219: fos, "UTF8");
220: BufferedWriter bw = new BufferedWriter(osw);
221: outputModel(bw, XProjectManager.getModel());
222: }
223: } catch (Exception ex) {
224: if (BuildProperties.DEBUG)
225: DebugLogger.logError("Could not write content!");
226: }
227: }
228: }
229: }
|