001: /*
002: * $Id: SRAPUtil.java,v 1.2 2002/07/22 08:47:29 mm132998 Exp $
003: * $Source: /m/portal/ps/srap/src/migration/modules/srap/erproxy/Attic/SRAPUtil.java,v $
004: * $Log: SRAPUtil.java,v $
005: * Revision 1.2 2002/07/22 08:47:29 mm132998
006: * Bug ID - 4718198 , Desc - Initial code changes
007: *
008: *
009: */
010:
011: /**
012: * $Id: SRAPUtil.java,v 1.2 2002/07/22 08:47:29 mm132998 Exp $
013: * Copyright 2002 Sun Microsystems, Inc. All
014: * rights reserved. Use of this product is subject
015: * to license terms. Federal Acquisitions:
016: * Commercial Software -- Government Users
017: * Subject to Standard License Terms and
018: * Conditions.
019: *
020: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
021: * are trademarks or registered trademarks of Sun Microsystems,
022: * Inc. in the United States and other countries.
023: */package migration.modules.srap.erproxy;
024:
025: import javax.xml.parsers.*;
026: import org.xml.sax.*;
027: import org.xml.sax.helpers.*;
028: import org.w3c.dom.*;
029: import org.apache.xml.serialize.*;
030: import java.io.*;
031: import java.util.ArrayList;
032: import java.util.Date;
033: import java.text.SimpleDateFormat;
034:
035: public class SRAPUtil {
036: public static RandomAccessFile raf = null;
037: public static final String ENCODING = "ISO-8859-1";
038:
039: /**
040: * Creates the DOM object for the specified XML file.
041: */
042: public static Document create(String infile, boolean nsAware,
043: boolean validate) throws SRAPDOMException {
044: boolean ignoreWhitespace = false;
045: boolean ignoreComments = false;
046: try {
047: DocumentBuilderFactory dbf = DocumentBuilderFactory
048: .newInstance();
049: dbf.setNamespaceAware(nsAware);
050: dbf.setValidating(validate);
051: dbf.setIgnoringComments(true);
052: dbf.setIgnoringElementContentWhitespace(true);
053: DocumentBuilder db = dbf.newDocumentBuilder();
054: OutputStreamWriter errorWriter = new OutputStreamWriter(
055: System.out, "UTF-8");
056: db.setErrorHandler(new MyHandler(new PrintWriter(
057: errorWriter, true)));
058: return db.parse(createTmpWithComponent(infile));
059: } catch (Exception ex) {
060: throw new SRAPDOMException(ex.toString());
061: }
062: }
063:
064: /**
065: * Creates an empty DOM object.
066: */
067: static Document create(boolean nsAware, boolean validate)
068: throws SRAPDOMException {
069: boolean ignoreWhitespace = false;
070: boolean ignoreComments = false;
071: try {
072: DocumentBuilderFactory dbf = DocumentBuilderFactory
073: .newInstance();
074: dbf.setNamespaceAware(nsAware);
075: dbf.setValidating(validate);
076: dbf.setIgnoringComments(true);
077: dbf.setIgnoringElementContentWhitespace(true);
078: DocumentBuilder db = dbf.newDocumentBuilder();
079: OutputStreamWriter errorWriter = new OutputStreamWriter(
080: System.out, "UTF-8");
081: db.setErrorHandler(new MyHandler(new PrintWriter(
082: errorWriter, true)));
083: return db.newDocument();
084: } catch (Exception ex) {
085: throw new SRAPDOMException(ex.toString());
086: }
087: }
088:
089: /**
090: * This method serializes a DOM object to a specified OS.
091: */
092: public static void serializeDOMTree(Document doc, OutputStream os)
093: throws SRAPDOMException {
094: try {
095: OutputFormat format = new OutputFormat();
096: format.setIndenting(true);
097: format.setEncoding(ENCODING);
098: format.setVersion("1.0");
099: XMLSerializer serializer = new XMLSerializer(format);
100: serializer.setOutputByteStream(os);
101: serializer.serialize(doc);
102: } catch (Exception ex) {
103: throw new SRAPDOMException(ex.toString());
104: }
105: }
106:
107: /*
108: * Create a valid temporary XML file (namespace aware)
109: **/
110: private static FileInputStream createTmpWithComponent(String infile)
111: throws Exception {
112: BufferedReader br = new BufferedReader(new InputStreamReader(
113: new FileInputStream(infile), ENCODING));
114: File tmp = File.createTempFile("wt1234", "xml",
115: new File("/tmp"));
116: tmp.deleteOnExit();
117: FileOutputStream out = new FileOutputStream(tmp);
118: /*String compstuff = "<iwt:Component xmlns:iwt=\"\" name=\""+infile+"\" >\n";
119: System.out.println( compstuff);
120: out.write(compstuff.getBytes());*/
121: String read = br.readLine();
122: while (read != null) {
123: if (read.trim().startsWith("<iwt:Component")) {
124: int indx = read.indexOf("<iwt:Component");
125: StringBuffer sb = new StringBuffer();
126: sb.append(read.substring(0, indx + 14));
127: sb.append(" xmlns:iwt=\"\"");
128: sb.append(read.substring(indx + 14, read.length()));
129: out.write(sb.toString().getBytes());
130:
131: /*int indx = read.indexOf('>');
132: while(indx == -1){
133: read = br.readLine();
134: if(read == null)
135: throw new Exception("Error in the XML structure");
136: indx = read.indexOf('>');
137: }
138: if(indx != read.length())
139: out.write( read.substring(indx+1, read.length()).getBytes() );*/
140:
141: } else {
142: out.write(read.getBytes());
143: }
144: read = br.readLine();
145: }
146: br.close();
147: out.flush();
148: out.close();
149: return new FileInputStream(tmp);
150: }
151:
152: private static class MyHandler extends DefaultHandler {
153: private PrintWriter out;
154:
155: MyHandler(PrintWriter out) {
156: this .out = out;
157: }//constructor
158:
159: /**
160: * Returns a string describing parse exception details
161: */
162: private String getParseExceptionInfo(SAXParseException spe) {
163: String systemId = spe.getSystemId();
164: if (systemId == null) {
165: systemId = "null";
166: }
167: String info = "URI = " + systemId + " Line = "
168: + spe.getLineNumber() + ": " + spe.getMessage();
169: return info;
170: }//getParseExceptionInfo()
171:
172: /**
173: * The following methods are standard SAX ErrorHandler methods.
174: * See SAX documentation for more info.
175: */
176: public void warning(SAXParseException spe) throws SAXException {
177: String message = "Warning: " + getParseExceptionInfo(spe);
178: //Debug.message( message, spe );
179: out.println(message);
180: throw spe;
181: }//warning()
182:
183: public void error(SAXParseException spe) throws SAXException {
184: String message = "Error: " + getParseExceptionInfo(spe);
185: //Debug.message( message, spe );
186: out.println(message);
187: throw spe;
188: }//error()
189:
190: public void fatalError(SAXParseException spe)
191: throws SAXException {
192: String message = "Fatal Error: "
193: + getParseExceptionInfo(spe);
194: //Debug.message( message, spe );
195: out.println(message);
196: throw spe;
197: }//fatalError()
198:
199: }//class MyHandler
200:
201: public static Attribute getAttribute(String key, String value) {
202: Attribute attrib = new Attribute();
203: attrib.setKey(key);
204: attrib.setValue(value);
205: return attrib;
206: }
207:
208: public static InstAttribute getSRAPInstAttribute(String key,
209: ArrayList value) {
210: InstAttribute attrib = new InstAttribute();
211: attrib.setKey(key);
212: attrib.setValue(value);
213: return attrib;
214: }
215:
216: /**
217: * This method gets the current date and time with the customized pattern.
218: */
219: public static String getCurrentDateTime() {
220: //Get the curernt date
221: Date date = new Date();
222: //Get the DateFormat instance
223: SimpleDateFormat sdf = new SimpleDateFormat(
224: "yyyy/MM/dd kk:mm:ss");
225: //Get the datetime in the required format.
226: String sDate = sdf.format(date);
227: return sDate.trim();
228: }
229:
230: public static void getReportLogHandle(String reportfile)
231: throws Exception {
232: raf = new RandomAccessFile(reportfile, "rw");
233: raf.seek(raf.length());
234: }
235:
236: public static void writelog(String message) {
237: try {
238: raf.writeChars(message);
239: } catch (Exception ex) {
240: System.out.println(message);
241: }
242: }
243: }
|