All pastes #482130 Raw Edit

Someone

public java v1 · immutable
·published 2007-05-11 04:20 UTC
rendered paste body
import org.apache.commons.codec.binary.Base64;import java.io.FileInputStream;import java.io.BufferedInputStream;import java.io.InputStream;import java.io.File;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.FilenameFilter;import java.io.Writer;import java.net.URISyntaxException;import java.net.URI;import java.lang.String;import org.xml.sax.SAXException;import org.xml.sax.Attributes;import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import java.text.NumberFormat;import java.text.DecimalFormat;public class ParseXML extends DefaultHandler{	private InspectionData[] objInspectionData;	private InspectorData[] objInspectorData;	private int intIndex;	private String strCurrentValue = "";	private String strCurrentTag = "";	private String strReadDirectory;	private String strWriteDirectory;	private String strPathToResourcesDir;		public ParseXML(String args, String strNewReadDirectory, String strNewWriteDirectory, String strNewPathToResourcesDir)	{		System.out.println("In ParseXML");		strReadDirectory = strNewReadDirectory;		strWriteDirectory = strNewWriteDirectory;		strPathToResourcesDir = strNewPathToResourcesDir;		try		{			File dir = new File(new URI("file:///" + strReadDirectory));			String[] children;			final String strTemp = args;			if ( args == null )			{				FilenameFilter filter = new FilenameFilter() 				{					public boolean accept(File dir, String name) 					{						return name.endsWith(".xml") && !name.endsWith("WEBVIEW.xml");					}				};				children = dir.list(filter);			}			else			{				FilenameFilter filter = new FilenameFilter() 				{					public boolean accept(File dir, String name) 					{						return name.startsWith(strTemp) && name.endsWith(".xml") && !name.endsWith("WEBVIEW.xml");					}				};				children = dir.list(filter);			}						if (children == null) 			{				System.out.println("No children");				// Either dir does not exist or is not a directory			} 			else 			{				System.out.println("Children not null");								//Once we know how many xml files we have we can set the length of the container objects for inspection/inspector data.				objInspectionData = new InspectionData[children.length];				objInspectorData = new InspectorData[children.length];				System.out.println(children.length);				for (int i=0; i<children.length; i++)				{					System.out.println(i);					objInspectionData[i] = new InspectionData();					objInspectorData[i] = new InspectorData();				}				String filename = null;				System.out.println("Children not null2");				NumberFormat objNumberFormat = new DecimalFormat("0000");				String[] strFileList = new String[children.length];								for (int i=0; i<children.length; i++) 				{					// Lets the rest of the program know what iteration the loop is on.					intIndex = i; 										filename = children[i];					System.out.println(filename);					parseDocuments(filename);										// Accessing the .jpg associated with the .xml.					filename = filename.replaceFirst(".xml", ".jpg"); 					strFileList[i] = filename;																		objInspectionData[i].setImage("<v:shape id=\"_x0000_i" + objNumberFormat.format(i) + "\" type=\"#_x0000_t75\" style=\"width:237pt;height:181.5pt\"><v:imagedata r:id=\"rId" + (i+8) + "\" o:title=\"" + filename.replaceFirst(".jpg", "") + "\" /></v:shape>"); 				}				// Sends the Inspector / Inspection objects to the report writer.								System.out.println("writing report");												WriteReport objWriteReport = new WriteReport( objInspectorData, objInspectionData, strWriteDirectory, strPathToResourcesDir, strFileList);				objWriteReport.writeRelsFile();				objWriteReport.write();												System.out.println("done writing report");			}		}		catch (java.net.URISyntaxException e)		{			e.printStackTrace();		}	}	// Main, init etc... methods	public static void main(String argv[])	{		//Empty	}	public void init ()	{		//Empty	}	public void start ()	{		//Empty	}	public void parseDocuments(String strFile)	{        try 		{			System.out.println("parseDocuments");			// Use an instance of ourselves as the SAX event handler			DefaultHandler handler = this;			// Use the default (non-validating) parser			SAXParserFactory factory = SAXParserFactory.newInstance();            // Set up output stream            out = new OutputStreamWriter(System.out, "UTF8");            // Parse the input            SAXParser saxParser = factory.newSAXParser();			saxParser.parse( new File(new URI("file:///" + strReadDirectory + strFile)), handler);			System.out.println("done parseDocuments");		}		catch (java.net.URISyntaxException e)		{			e.printStackTrace();		}		catch (java.io.IOException e) 		{ 			e.printStackTrace(); 		}		catch (javax.xml.parsers.ParserConfigurationException e)		{			e.printStackTrace();		}		catch (org.xml.sax.SAXException e)		{			e.printStackTrace();		}	}    static private Writer out;    //===========================================================    // SAX DocumentHandler methods    //===========================================================    public void startDocument()    throws SAXException    {		//Empty    }    public void endDocument() throws SAXException    {        try 		{            out.flush();        } 		catch (IOException e) 		{            throw new SAXException("I/O error", e);        }    }    public void startElement(String namespaceURI,                             String lName, // local name                             String qName, // qualified name                             Attributes attrs)    throws SAXException    {        String eName = lName; // element name        if ("".equals(eName)) 		{			eName = qName; // namespaceAware = false		}        if (attrs != null) 		{            for (int i = 0; i < attrs.getLength(); i++) 			{                String aName = attrs.getLocalName(i); // Attr name                 if ("".equals(aName))				{					aName = attrs.getQName(i);				}				if (aName.equals("TAG"))				{					System.out.println(strCurrentTag);					strCurrentTag = attrs.getValue(i);				}            }        }    }    public void endElement(String namespaceURI,                           String sName, // simple name                           String qName  // qualified name                          )    throws SAXException    {		//Empty    }    public void characters(char buf[], int offset, int len)    throws SAXException    {        String s = new String(buf, offset, len);        if (!s.trim().equals("")) 		{			strCurrentValue = s;		}		else		{			strCurrentValue = "";		}		objInspectionData[intIndex].assignByTag(strCurrentTag, strCurrentValue);			objInspectorData[intIndex].assignByTag(strCurrentTag, strCurrentValue);		System.out.println("Current tag: " + strCurrentTag + " Current Value: " + strCurrentValue);     }    //===========================================================    // Utility Methods ...    //===========================================================    // Wrap I/O exceptions in SAX exceptions, to    // suit handler signature requirements    private void emit(String s)    throws SAXException    {        try 		{           out.write(s);           out.flush();        } 		catch (IOException e) 		{            throw new SAXException("I/O error", e);        }    }		/* THIS IS NOT USED AS OF 05/08/07	//===========================================================	//Encodes a file(in this case an image) into Base64	//===========================================================	public String encodeFile(String filename)	{		byte[] result;		byte[] fileContents;		InputStream myFileContents = null;				try		{					File myFile = new File(new URI("file:///" + strReadDirectory + filename));						//Check if file exists			if (myFile.exists())			{				myFileContents = new BufferedInputStream (new FileInputStream (myFile));				fileContents = new byte[(int)myFile.length()];				myFileContents.read(fileContents, 0, (int)myFile.length());				// Convert binary image data into Base64 for embedding				result = Base64.encodeBase64Chunked(fileContents); 				myFileContents.close();				return new String(result);			}			else			{				System.out.println("Error: " + strReadDirectory + filename + " does not exist.");				return null;			}		}		catch (Exception e)		{			e.printStackTrace();			return null;		}		finally		{			try			{				myFileContents.close();			}			catch (java.io.IOException e) 			{ 				e.printStackTrace(); 			}		}	}	*/}