KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > datafile > DataFile2EntityXml


1 /*
2  * DataFile2EntityXml.java
3  *
4  * Created on 15 febbraio 2005, 9.45
5  */

6
7 package org.ofbiz.datafile;
8
9 import java.util.*;
10 import java.net.*;
11 import java.io.*;
12 import org.ofbiz.base.util.*;
13 import org.ofbiz.datafile.*;
14
15 /**
16  *
17  * @author jacopo
18  */

19 public class DataFile2EntityXml {
20     
21     /** Creates a new instance of DataFile2EntityXml */
22     public DataFile2EntityXml() {
23     }
24     
25     /**
26      * @param args the command line arguments
27      */

28     public static void writeToEntityXml(String JavaDoc fileName, DataFile dataFile) throws DataFileException {
29         File file = new File(fileName);
30         BufferedWriter outFile = null;
31
32         try {
33             
34             //outFile = new BufferedWriter(new FileWriter(file));
35
outFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
36         } catch (Exception JavaDoc e) {
37             throw new DataFileException("Could not open file " + fileName, e);
38         }
39         //----------------------------------------------------
40
try {
41             outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
42             outFile.newLine();
43             outFile.write("<entity-engine-xml>");
44             outFile.newLine();
45             List records = dataFile.getRecords();
46             for(int r=0; r<records.size(); r++) {
47                 Record record = (Record)records.get(r);
48                 ModelRecord modelRecord = record.getModelRecord();
49                 outFile.write("<" + modelRecord.name + " ");
50                 for (int f=0;f<modelRecord.fields.size(); f++) {
51                     ModelField modelField = (ModelField)modelRecord.fields.get(f);
52                     if (modelField.ignored) continue;
53                     Object JavaDoc value = record.get(modelField.name);
54                     if (value == null) {
55                         value = modelField.defaultValue;
56                     }
57                     if (value instanceof String JavaDoc) {
58                         value = ((String JavaDoc)value).trim();
59                         if (((String JavaDoc)value).length() == 0) {
60                             value = modelField.defaultValue;
61                         }
62                     }
63                     if (value != null) {
64                         outFile.write(modelField.name + "=\"" + value + "\" ");
65                     }
66                 }
67                 outFile.write("/>");
68                 outFile.newLine();
69             }
70             outFile.write("</entity-engine-xml>");
71             outFile.close();
72         } catch (IOException e) {
73             throw new DataFileException("Error writing to file " + fileName, e);
74         }
75         
76     }
77     
78     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
79         // TODO code application logic here
80
String JavaDoc dataFileLoc = args[0];
81         String JavaDoc definitionLoc = args[1];
82         String JavaDoc definitionName = args[2];
83         
84         BufferedWriter outFile = new BufferedWriter(new FileWriter(dataFileLoc + ".xml"));
85         
86         URL dataFileUrl = null;
87         //try {
88
dataFileUrl = UtilURL.fromFilename(dataFileLoc);
89         //} catch (java.net.MalformedURLException e) {
90
//messages.add(e.getMessage());
91
//}
92
URL definitionUrl = null;
93         //try {
94
definitionUrl = UtilURL.fromFilename(definitionLoc);
95         //} catch (java.net.MalformedURLException e) {
96
//messages.add(e.getMessage());
97
//}
98

99         DataFile dataFile = null;
100         if (dataFileUrl != null && definitionUrl != null && definitionName != null && definitionName.length() > 0) {
101             try {
102                 dataFile = DataFile.readFile(dataFileUrl, definitionUrl, definitionName);
103             } catch (Exception JavaDoc e) {
104                 //messages.add(e.toString());
105
//Debug.log(e);
106
}
107         }
108
109         // -----------------------------------------
110
List records = dataFile.getRecords();
111         for(int r=0; r<records.size(); r++) {
112             Record record = (Record)records.get(r);
113             ModelRecord modelRecord = record.getModelRecord();
114             outFile.write("<" + modelRecord.name + " ");
115             for (int f=0;f<modelRecord.fields.size(); f++) {
116                 ModelField modelField = (ModelField)modelRecord.fields.get(f);
117                 Object JavaDoc value = record.get(modelField.name);
118                 if (value instanceof String JavaDoc) {
119                     value = ((String JavaDoc)value).trim();
120                 }
121                 outFile.write(modelField.name + "=\"" + value + "\" ");
122             }
123             outFile.write("/>");
124             outFile.newLine();
125         }
126         outFile.close();
127
128     }
129     
130 }
131
Popular Tags