Commit 35c62fa3 authored by Fabien Triolet's avatar Fabien Triolet
Browse files

Changed TReader code . Implementing a reader is easier now

parent 16ccdd24
/*
* $Id: ReadCSV.java,v 1.3 2002-11-06 14:08:21 serge Exp $
* $Id: ReadCSV.java,v 1.4 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -25,19 +25,12 @@ package fr.fluxmedia.tmcontrib.generator;
// Imported TRANSMORPHER Classes
import fr.fluxmedia.transmorpher.engine.TReader;
import fr.fluxmedia.transmorpher.engine.XML_Port;
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
//Imported JAVA classes
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
// Imported SAX Classes
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Read a CSV (comma separated value) file which coordinates (local path or URL) are given in the
......@@ -73,27 +66,8 @@ public class ReadCSV extends TReader {
*/
public void read() throws IOException, SAXException {
try {
InputSource inSource;
if (iFilename != null) {
if (iFilename.startsWith("http://")) {
URL url = new URL(iFilename);
URLConnection urlConnect = url.openConnection();
InputStream inStream = urlConnect.getInputStream();
inSource = new InputSource(inStream);
inSource.setSystemId(iFilename);
} else {
File file = new File(iFilename);
inSource = new InputSource(new FileInputStream(file));
inSource.setSystemId(file.toURL().toExternalForm());
}
iReader.parse(inSource);
} else {
iReader.parse(standardInput);
}//end if
} catch (IOException e) {
System.err.println("File : " + iFilename + " can't be found.");
iReader.getContentHandler().startDocument();
iReader.getContentHandler().endDocument();
iReader.parse(getInputSource());
} catch (Exception e) {
}
}
}
......
......@@ -25,19 +25,13 @@ package fr.fluxmedia.tmcontrib.generator;
// Imported TRANSMORPHER Classes
import fr.fluxmedia.transmorpher.engine.TReader;
import fr.fluxmedia.transmorpher.engine.XML_Port;
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
//Imported JAVA classes
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
// Imported SAX Classes
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Read a vcal or vcard (specify by the type parameter) file which coordinates (local path or URL) are given in the
......@@ -85,29 +79,9 @@ public class readvcs extends TReader {
*/
public void read() throws IOException, SAXException {
try {
InputSource inSource;
if (iFilename != null) {
if (iFilename.startsWith("http://")) {
URL url = new URL(iFilename);
URLConnection urlConnect = url.openConnection();
InputStream inStream = urlConnect.getInputStream();
inSource = new InputSource(inStream);
inSource.setSystemId(iFilename);
iReader.parse(inSource);
} else {
File file = new File(iFilename);
inSource = new InputSource(new FileInputStream(file));
inSource.setSystemId(file.toURL().toExternalForm());
iReader.parse(inSource);
}
} else {
iReader.parse(standardInput);
}
} catch (IOException e) {
System.err.println("File : " + iFilename + " can't be found.");
iReader.getContentHandler().startDocument();
iReader.getContentHandler().endDocument();
}//end if
iReader.parse(getInputSource());
} catch (Exception e) {
}
}
}
/**
* $Id: tidy.java,v 1.7 2002-11-06 14:08:21 serge Exp $
* $Id: tidy.java,v 1.8 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -26,31 +26,22 @@ package fr.fluxmedia.tmcontrib.generator;
// Imported Transmorpher classes
import fr.fluxmedia.transmorpher.engine.TReader;
import fr.fluxmedia.transmorpher.engine.TProcessFactory;
import fr.fluxmedia.transmorpher.engine.TReader;
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
import fr.fluxmedia.transmorpher.utils.SystemResources;
import fr.fluxmedia.transmorpher.utils.TMRuntimeException;
// Imported Java classes
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.FileInputStream;
import java.lang.Runnable;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import javax.xml.parsers.SAXParserFactory;
// Imported JAXP classes
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
......@@ -76,79 +67,64 @@ import org.xml.sax.helpers.XMLReaderFactory;
* file parameter of the generate call, parse it, process it through TIDY
* and generate a SAX event flow to the next handler
*
*@author Jrme.Euzenat@inrialpes.fr
*@author Fabien.Triolet@inrialpes.fr
*@author Jrme.Euzenat@inrialpes.fr
*@author Fabien.Triolet@inrialpes.fr
*/
public final class tidy extends TReader {
/**
*Constructor for the tidy object
*
*@param pOut Description of the Parameter
*@param pParam Description of the Parameter
*@param pStaticAttributes Description of the Parameter
*@exception SAXException Description of the Exception
*@exception IOException Description of the Exception
*/
public tidy(String[] pOut, Parameters pParam, StringParameters pStaticAttributes) throws SAXException, IOException {
super(pOut, pParam, pStaticAttributes);
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setFeature(NAMESPACES_FEATURE_ID, DEFAULT_NAMESPACES);
parserFactory.setFeature(NAMESPACES_PREFIXES_FEATURE_ID, DEFAULT_NAMESPACES_PREFIXES);
parserFactory.setFeature(VALIDATION_FEATURE_ID, DEFAULT_VALIDATION);
parserFactory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, DEFAULT_SCHEMA_VALIDATION);
iReader = (parserFactory.newSAXParser()).getXMLReader();
//System.out.println(iReader);
} catch (Exception e) {
System.out.println("[tidy]Exception :" + e);
System.out.println("[tidy] " + SystemResources.getProperty("XML_READER"));
}//end try
}
/**
* Description of the Method
*
*@exception IOException I/O error in either
*@exception SAXException Parsing error
*@exception TMRuntimeException Tidy processing error
*/
public void read() throws IOException, SAXException, TMRuntimeException {
Tidy cleaner = new Tidy();
cleaner.setErrout(new PrintWriter(System.err));
InputStream inStream;
if ( iFilename != null) {
if (iFilename.startsWith("http://")) {
URL url = new URL(iFilename);
URLConnection urlConnect = url.openConnection();
inStream = urlConnect.getInputStream();
} else {
File file = new File(iFilename);
inStream = new FileInputStream(file);
}
Node document = cleaner.parseDOM(inStream, null);
Transformer transformer = null;
try {
transformer =TProcessFactory.getSAXTFactory().newTransformer();
} catch (TransformerConfigurationException e) {
throw new TMRuntimeException( e, "Cannot create SAX transformer" );
}
DOMSource source = new DOMSource(document);
source.setSystemId(iFilename);
ContentHandler handler = iReader.getContentHandler();
SAXResult result = new SAXResult(handler);
try { transformer.transform(source, result); }
catch (TransformerException e) {
throw new TMRuntimeException( e, "Cannot process Tidy on "+iFilename );
}
} else {
// JE: it seems that here nothing is done!
iReader.parse(standardInput);
}//end if
}
/**
*Constructor for the tidy object
*
*@param pOut Description of the Parameter
*@param pParam Description of the Parameter
*@param pStaticAttributes Description of the Parameter
*@exception SAXException Description of the Exception
*@exception IOException Description of the Exception
*/
public tidy(String[] pOut, Parameters pParam, StringParameters pStaticAttributes) throws SAXException, IOException {
super(pOut, pParam, pStaticAttributes);
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setFeature(NAMESPACES_FEATURE_ID, DEFAULT_NAMESPACES);
parserFactory.setFeature(NAMESPACES_PREFIXES_FEATURE_ID, DEFAULT_NAMESPACES_PREFIXES);
parserFactory.setFeature(VALIDATION_FEATURE_ID, DEFAULT_VALIDATION);
parserFactory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, DEFAULT_SCHEMA_VALIDATION);
iReader = (parserFactory.newSAXParser()).getXMLReader();
} catch (Exception e) {
System.out.println("[tidy]Exception :" + e);
}//end try
}
/**
* Description of the Method
*
*@exception IOException I/O error in either
*@exception SAXException Parsing error
*@exception TMRuntimeException Tidy processing error
*/
public void read() throws IOException, SAXException, TMRuntimeException {
Tidy cleaner = new Tidy();
cleaner.setErrout(new PrintWriter(System.err));
Node document = cleaner.parseDOM(getInputStream(), null);
Transformer transformer = null;
try {
transformer = TProcessFactory.getSAXTFactory().newTransformer();
} catch (TransformerConfigurationException e) {
throw new TMRuntimeException(e, "Cannot create SAX transformer");
}
DOMSource source = new DOMSource(document);
source.setSystemId(iFilename);
ContentHandler handler = iReader.getContentHandler();
SAXResult result = new SAXResult(handler);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
throw new TMRuntimeException(e, "Cannot process Tidy on " + iFilename);
}
}
}
/**
* $Id: TLoop.java,v 1.3 2002-11-13 15:43:41 triolet Exp $
* $Id: TLoop.java,v 1.4 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -459,12 +459,6 @@ public final class TLoop extends TProcessComposite {
if (process instanceof TProcessBasic) {
((TProcessBasic)process).initParameters();
}
/* if (process instanceof TTransformation) {
((TTransformation)process).initParameters();
} */
//}
}
for (int i = 0; i < managers.length; i++) {
//System.out.println("NEW LOOP " + loopCounter);
......
/*
* $Id: TReader.java,v 1.2 2002-11-13 15:43:41 triolet Exp $
* $Id: TReader.java,v 1.3 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -22,29 +22,26 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package fr.fluxmedia.transmorpher.engine;
// Imported Transmorpher classes
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
import fr.fluxmedia.transmorpher.utils.SystemResources;
// Imported Transmorpher classes
import fr.fluxmedia.transmorpher.utils.TMException;
import fr.fluxmedia.transmorpher.utils.TMRuntimeException;
// Imported Java classes
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Exception;
import java.lang.Runnable;
import java.net.*;
import java.util.Hashtable;
import org.xml.sax.InputSource;
// Imported SAX classes
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* This class provide a basic process which is
* able to read an XML file
......@@ -111,7 +108,7 @@ public abstract class TReader extends TProcessBasic {
/**
* The XMLReader
*/
public XMLReader iReader = null;
protected XMLReader iReader = null;
/**
* Description of the Field
......@@ -125,14 +122,18 @@ public abstract class TReader extends TProcessBasic {
/**
* Name of the file to read
*/
public String iFilename = null;
protected String iFilename = null;
/**
* Description of the Field
*/
public InputSource standardInput = null;
protected InputSource standardInput = null;
/**
* Description of the Field
*/
protected InputSource inSource = null;
/**
*Constructor for the TReader object
......@@ -171,16 +172,40 @@ public abstract class TReader extends TProcessBasic {
super.bindParameters(getParameters());
iFilename = (String)getParameter("file");
getParameters().setParameters(param);
if (isThread) {
procThread.start();
if (raisedException != null) {
throw new TMRuntimeException(raisedException, "Thread reading error");
if (iFilename != null) {
if (iFilename.startsWith("http://")) {
URL url = new URL(iFilename);
URLConnection urlConnect = url.openConnection();
InputStream inStream = urlConnect.getInputStream();
inSource = new InputSource(inStream);
inSource.setSystemId(iFilename);
} else {
File file = new File(iFilename);
inSource = new InputSource(new FileInputStream(file));
inSource.setSystemId(file.toURL().toString());
}
if (isThread) {
procThread.start();
if (raisedException != null) {
throw new TMRuntimeException(raisedException, "Thread reading error");
}
} else {
read();
}
} else {
read();
inSource = new InputSource(System.in);
}
} catch (Exception e) {}
} catch (IOException e) {
// JE: There is a problem here either we report the error,
// either we send the empty document! And if we report it,
// this must be by raising an exception
System.err.println("File : " + iFilename + " can't be found.");
// Send the empty document on error!?
iReader.getContentHandler().startDocument();
iReader.getContentHandler().endDocument();
} catch (TMException e) {}
}
/**
......@@ -232,5 +257,22 @@ public abstract class TReader extends TProcessBasic {
*/
public abstract void read() throws IOException, SAXException, TMRuntimeException;
/**
* Gets the inputSource attribute of the TReader object
*
*@return The inputSource value
*/
public InputSource getInputSource() {
return inSource;
}
/**
* Gets the inputStream attribute of the TReader object
*
*@return The inputStream value
*/
public InputStream getInputStream() {
return inSource.getByteStream();
}
}
/**
* $Id: TSerializer.java,v 1.3 2002-11-13 15:43:41 triolet Exp $
* $Id: TSerializer.java,v 1.4 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -95,6 +95,11 @@ public abstract class TSerializer extends TProcessBasic {
if (pStaticAttributes.getParameter("file") != null) {
if (pStaticAttributes.getParameter("file").equals("_null_")) {
printNull = true;
printOut = false;
}
if (pStaticAttributes.getParameter("file").equals("_stdout_")) {
printNull = false;
printOut = true;
}
}
String method = (String)pParam.getParameter("method");
......
/*
* $Id: ReadFile.java,v 1.2 2002-11-13 15:43:41 triolet Exp $
* $Id: ReadFile.java,v 1.3 2002-11-14 15:46:45 triolet Exp $
*
* Transmorpher
*
......@@ -25,29 +25,19 @@ package fr.fluxmedia.transmorpher.stdlib;
// Imported Transmorpher classes
import fr.fluxmedia.transmorpher.engine.TProcessFactory;
import fr.fluxmedia.transmorpher.engine.TReader;
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
import fr.fluxmedia.transmorpher.utils.SystemResources;
import fr.fluxmedia.transmorpher.utils.TMException;
// Imported Java classes
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Runnable;
import java.net.*;
import java.util.Hashtable;
// Imported SAX and JAXP classes
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* This class provide a basic process which is
......@@ -83,7 +73,6 @@ public final class ReadFile extends TReader {
iReader = (parserFactory.newSAXParser()).getXMLReader();
} catch (Exception e) {
System.out.println("[ReadFile]Exception :" + e);
System.out.println("[ReadFile] " + SystemResources.getProperty("XML_READER"));
}//end try
}
......@@ -94,33 +83,10 @@ public final class ReadFile extends TReader {
*@exception SAXException can wrap others exceptions
*/
public void read() throws IOException, SAXException {
InputSource inSource;
try {
if (iFilename != null) {
if (iFilename.startsWith("http://")) {
URL url = new URL(iFilename);
URLConnection urlConnect = url.openConnection();
InputStream inStream = urlConnect.getInputStream();
inSource = new InputSource(inStream);
inSource.setSystemId(iFilename);
iReader.parse(inSource);
} else {
File file = new File(iFilename);
inSource = new InputSource(new FileInputStream(file));
inSource.setSystemId(file.toURL().toString());
iReader.parse(inSource);
}
} else {
iReader.parse(standardInput);
}
} catch (IOException e) {
// JE: There is a problem here either we report the error,
// either we send the empty document! And if we report it,
// this must be by raising an exception
System.err.println("File : " + iFilename + " can't be found.");
// Send the empty document on error!?
iReader.getContentHandler().startDocument();
iReader.getContentHandler().endDocument();
iReader.parse(getInputSource());
} catch (Exception e) {
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment