Subversion Repositories SmartDukaan

Rev

Rev 6030 | Rev 6034 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6030 Rev 6033
Line 7... Line 7...
7
import java.io.ByteArrayInputStream;
7
import java.io.ByteArrayInputStream;
8
import java.io.File;
8
import java.io.File;
9
import java.io.FileOutputStream;
9
import java.io.FileOutputStream;
10
import java.io.FileReader;
10
import java.io.FileReader;
11
import java.io.IOException;
11
import java.io.IOException;
-
 
12
import java.io.StringReader;
12
import java.text.SimpleDateFormat;
13
import java.text.SimpleDateFormat;
13
import java.util.Date;
14
import java.util.Date;
14
 
15
 
15
import javax.xml.parsers.DocumentBuilder;
16
import javax.xml.parsers.DocumentBuilder;
16
import javax.xml.parsers.DocumentBuilderFactory;
17
import javax.xml.parsers.DocumentBuilderFactory;
Line 30... Line 31...
30
import org.apache.poi.ss.usermodel.Workbook;
31
import org.apache.poi.ss.usermodel.Workbook;
31
import org.apache.struts2.dispatcher.StreamResult;
32
import org.apache.struts2.dispatcher.StreamResult;
32
import org.slf4j.Logger;
33
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
import org.slf4j.LoggerFactory;
34
import org.w3c.dom.NodeList;
35
import org.w3c.dom.NodeList;
-
 
36
import org.xml.sax.InputSource;
35
 
37
 
36
import com.opensymphony.xwork2.ActionSupport;
38
import com.opensymphony.xwork2.ActionSupport;
37
 
39
 
38
/**
40
/**
39
 * @author mandeep
41
 * @author mandeep
Line 157... Line 159...
157
    /**
159
    /**
158
    * @param xmlFile
160
    * @param xmlFile
159
    */
161
    */
160
   private String convertXmlToXls(File xmlFile) {
162
   private String convertXmlToXls(File xmlFile) {
161
       try {
163
       try {
-
 
164
    	   String xmlData = correctMalformedTags(xmlFile); 
-
 
165
    	   
162
           Workbook w = new HSSFWorkbook();
166
           Workbook w = new HSSFWorkbook();
163
           Sheet sheet=w.createSheet("spreadSheet");
167
           Sheet sheet=w.createSheet("spreadSheet");
164
           String convertedFileName = ARCHIVE_DIR + "Converted-"
168
           String convertedFileName = ARCHIVE_DIR + "Converted-"
165
                   + xmlFile.getName();
169
                   + xmlFile.getName();
166
           FileOutputStream fileOut = new FileOutputStream(convertedFileName);
170
           FileOutputStream fileOut = new FileOutputStream(convertedFileName);
167
           DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
171
           DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
168
           DocumentBuilder builder = factory.newDocumentBuilder();
172
           DocumentBuilder builder = factory.newDocumentBuilder();
-
 
173
           InputSource is = new InputSource(new StringReader(xmlData));
169
           org.w3c.dom.Document document = builder.parse(xmlFile);
174
           org.w3c.dom.Document document = builder.parse(is);
170
           
175
           
171
           NodeList itemNameList = document.getElementsByTagName("DSPDISPNAME");
176
           NodeList itemNameList = document.getElementsByTagName("DSPDISPNAME");
172
           NodeList itemQtyList = document.getElementsByTagName("DSPCLAMTA");
177
           NodeList itemQtyList = document.getElementsByTagName("DSPCLQTY");
173
           
178
           
174
           for(int i =1; i<=itemNameList.getLength(); i++) {
179
           for(int i =1; i<=itemNameList.getLength(); i++) {
175
        	   Row row = sheet.createRow(i);
180
        	   Row row = sheet.createRow(i);
176
        	   int j = 0;
181
        	   int j = 0;
177
        	   Cell cell1 = row.createCell(j++);
182
        	   Cell cell1 = row.createCell(j++);
Line 188... Line 193...
188
           logger.error("Error converting file", e);
193
           logger.error("Error converting file", e);
189
           return xmlFile.getName();
194
           return xmlFile.getName();
190
       }
195
       }
191
   }
196
   }
192
    
197
    
-
 
198
    private String correctMalformedTags(File xmlFile) throws IOException {
-
 
199
    	String xmlContent = readFileAsString(xmlFile);
-
 
200
    	xmlContent = xmlContent.replace("<DSPACCNA\\nE>", "<DSPACCNAME>").
-
 
201
    		replace("<DSPACCNA\nE>", "<DSPACCNAME>").replace("DSPACCNAME\\n<", "DSPACCNAME><").
-
 
202
    		replace("DSPACCNAME\n<", "DSPACCNAME><").replace("DSPACCNAME\\n <", "DSPACCNAME><").
-
 
203
    		replace("DSPACCNAME\n <", "DSPACCNAME><");
-
 
204
    	System.out.println(xmlContent);
-
 
205
    	return xmlContent;
-
 
206
	}
-
 
207
 
-
 
208
    private static String readFileAsString(File file)
-
 
209
    	throws java.io.IOException{
-
 
210
    	StringBuffer fileData = new StringBuffer(1000);
-
 
211
    	BufferedReader reader = new BufferedReader(
-
 
212
    			new FileReader(file));
-
 
213
    	char[] buf = new char[1024];
-
 
214
    	int numRead=0;
-
 
215
    	while((numRead=reader.read(buf)) != -1){
-
 
216
    		String readData = String.valueOf(buf, 0, numRead);
-
 
217
    		fileData.append(readData);
-
 
218
    		buf = new char[1024];
-
 
219
    	}
-
 
220
    	reader.close();
-
 
221
    	return fileData.toString();
-
 
222
    }
-
 
223
    
193
    public File getFile() {
224
	public File getFile() {
194
        return file;
225
        return file;
195
    }
226
    }
196
 
227
 
197
    public void setFile(File file) {
228
    public void setFile(File file) {
198
        this.file = file;
229
        this.file = file;