001: package com.avaje.util.codegen;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.Writer;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import com.avaje.lib.util.Dnode;
011:
012: /**
013: * This is very old code. Just leaving it around for kicks at the moment.
014: * <p>
015: * Should be generating EJB3 xml instead of this.
016: * </p>
017: */
018: public class DeployXmlWriter {
019:
020: String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE ebeandeploy SYSTEM \"ebeandeploy.dtd\">";
021: String prefix = "<ebean>";
022: String suffix = "</ebean>";
023:
024: String crnl = "\r\n";
025:
026: int[] minLength = { 30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
027:
028: String indent = " ";
029:
030: Writer writer;
031:
032: public DeployXmlWriter() {
033: }
034:
035: public void write(Dnode beanDeploy, File file) throws IOException {
036:
037: writer = new FileWriter(file, false);
038: writer.write(header);
039: writer.write(crnl);
040: writer.write(prefix);
041: writer.write(crnl);
042: Dnode table = beanDeploy.find("table");
043: writeNodeXml(table);
044: writer.write(crnl);
045:
046: List propList = beanDeploy.findAll("property", 1);
047: for (int i = 0; i < propList.size(); i++) {
048: Dnode node = (Dnode) propList.get(i);
049: writeNodeXml(node);
050: }
051: writer.write(crnl);
052:
053: List joinIt = beanDeploy.findAll("tablejoin", 1);
054: for (int i = 0; i < joinIt.size(); i++) {
055: Dnode node = (Dnode) joinIt.get(i);
056: writeNodeXml(node);
057: writer.write(crnl);
058: }
059:
060: List beanIt = beanDeploy.findAll("bean", 1);
061: for (int i = 0; i < beanIt.size(); i++) {
062: Dnode node = (Dnode) beanIt.get(i);
063: writeNodeXml(node);
064: writer.write(crnl);
065: }
066:
067: List listIt = beanDeploy.findAll("many", 1);
068: for (int i = 0; i < listIt.size(); i++) {
069: Dnode node = (Dnode) listIt.get(i);
070: writeNodeXml(node);
071: writer.write(crnl);
072: }
073:
074: writer.write(crnl);
075: writer.write(suffix);
076: writer.write(crnl);
077: writer.close();
078: }
079:
080: protected String minLength(String content, int minLength) {
081: int spaceToAdd = minLength - content.length();
082: if (spaceToAdd <= 0) {
083: return content;
084:
085: } else {
086: //append some space to the end...
087: StringBuffer sb = new StringBuffer(minLength);
088: sb.append(content);
089: for (int i = 0; i < spaceToAdd; i++) {
090: sb.append(" ");
091: }
092: return sb.toString();
093: }
094: }
095:
096: protected String genAttribute(Dnode node, String attrName,
097: int minLength) {
098: StringBuffer attrBuffer = new StringBuffer();
099: Object attrValue = node.getAttribute(attrName);
100: attrBuffer.append(" ").append(attrName).append("=\"");
101: if (attrValue != null) {
102: attrBuffer.append(attrValue);
103: }
104: attrBuffer.append("\"");
105: return minLength(attrBuffer.toString(), minLength);
106: }
107:
108: protected void writeIndent(Dnode node) throws IOException {
109: int level = node.getLevel();
110: for (int i = 0; i < level; i++) {
111: writer.write(indent);
112: }
113: }
114:
115: protected void writeNodeXml(Dnode node) throws IOException {
116:
117: writer.write(crnl);
118: writeIndent(node);
119: writer.write("<");
120: writer.write(node.getNodeName());
121: Iterator it = node.attributeNames();
122: int attributeIndex = 0;
123: int min;
124: while (it.hasNext()) {
125: String attrName = (String) it.next();
126: min = -1;
127: if (node.getNodeName().equals("property")
128: && attributeIndex < minLength.length) {
129: min = minLength[attributeIndex];
130: }
131: String attrContent = genAttribute(node, attrName, min);
132: writer.write(attrContent);
133: attributeIndex++;
134: }
135:
136: if (node.getNodeContent() == null && !node.hasChildren()) {
137: writer.write(" />");
138: } else {
139: writer.write(">");
140: if (node.hasChildren()) {
141:
142: // writer.write(crnl);
143: // writeIndent(node);
144:
145: Iterator childIt = node.children().iterator();
146: while (childIt.hasNext()) {
147: Dnode child = (Dnode) childIt.next();
148: writeNodeXml(child);
149: }
150: }
151: if (node.getNodeContent() != null) {
152: writer.write(crnl);
153: writeIndent(node);
154: writer.write(node.getNodeContent());
155: }
156: writer.write(crnl);
157: writeIndent(node);
158: writer.write("</");
159: writer.write(node.getNodeName());
160: writer.write(">");
161: }
162: }
163:
164: }
|