Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv15776/modules/core/src/com/babeldoc/core/pipeline/stage Modified Files: GenericWriterPipelineStage.java FileWriterPipelineStage.java Added Files: XmlToSqlPipelineStage.java EncodingConversionPipelineStage.java Log Message: news pipeline stage --- NEW FILE: XmlToSqlPipelineStage.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/XmlToSqlPipelineStage.java,v 1.1 2007/12/18 10:02:55 josem_dev Exp $ * $DateTime$ * $Author: josem_dev $ * */package com.babeldoc.core.pipeline.stage; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import com.babeldoc.core.I18n; //import com.babeldoc.core.LogService; import com.babeldoc.core.NameValuePair; import com.babeldoc.core.option.ComplexConfigOptionType; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.option.ServiceTypeConfigOptionType; import com.babeldoc.core.pipeline.PipelineException; import com.babeldoc.core.pipeline.PipelineStage; import com.babeldoc.core.pipeline.PipelineStageInfo; import com.babeldoc.core.pipeline.PipelineStageResult; import com.babeldoc.core.pipeline.PipelineStageType; import com.babeldoc.core.resource.IResource; import com.babeldoc.core.resource.ResourceException; import com.babeldoc.core.resource.ResourceFactory; import org.apache.xerces.parsers.DOMParser; //import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; //import org.xml.sax.SAXNotRecognizedException; //import org.xml.sax.SAXNotSupportedException; //import org.xml.sax.SAXParseException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; /** * Converts a XML to a SQL script (SQL insert's or/and update's commands) * * @author josem_dev * @version 1.0 */ public class XmlToSqlPipelineStage extends PipelineStage { /** * (mandatory) Name of the resource that contains Database Connection. */ protected static final String RESOURCE_NAME = "resourceName"; /** * (mandatory) Schema (usually XSD) DB table. */ protected static final String SCHEMA_URL = "schemaUrl"; /** * (mandatory) List of row names for the SQL WHERE clause, usually the primary key. */ protected static final String WHERE_ROW_NAMES = "whereRowNames"; /** * (optional) XML node name to convert, else convert all XML level 1 nodes (note: root element it's level 0) */ protected static final String TARGET_NODE = "targetNode"; /** * (mandatory) DB table name where insert/update data */ protected static final String TABLE_NAME = "tableName"; // Connection pool private IResource resource = null; // JDBC connectionn private Connection conn = null; // JDBC statement private Statement stmt = null; // DOM Document DB schema private Document schema; ///** // * Class for schema validation. // */ /* private class Validator implements ErrorHandler { public SAXParseException saxParseException = null; public void error(SAXParseException exception) throws SAXException { saxParseException = exception; } public void fatalError(SAXParseException exception) throws SAXException { saxParseException = exception; } public void warning(SAXParseException exception) throws SAXException { saxParseException = exception; } } */ /** * Construct this stage - create the information object */ public XmlToSqlPipelineStage() { super(new PipelineStageInfo() { public String getName() { return "XmlToSql"; } public String getDescription() { return I18n.get("conversion.pipeline.stage.XmlToSql.desc"); } public Collection getTypeSpecificOptions() { Collection options; IConfigOptionType whereRowNamesType; options = new ArrayList(); options.add( new ConfigOption( RESOURCE_NAME, IConfigOptionType.STRING, null, true, I18n.get("sql.101") ) ); options.add( new ConfigOption( TABLE_NAME, IConfigOptionType.STRING, null, true, I18n.get("sql.1009") ) ); options.add( new ConfigOption( SCHEMA_URL, IConfigOptionType.STRING, null, true, I18n.get("sql.1010") ) ); whereRowNamesType = new ComplexConfigOptionType( new ConfigOption[]{ new ConfigOption( "whereRowNamesType", new ServiceTypeConfigOptionType( PipelineStageType.SERVICE_PREFIX ), I18n.get("sql.1011") ) } ); options.add( new ConfigOption( WHERE_ROW_NAMES, whereRowNamesType, I18n.get("sql.1011") ) ); options.add( new ConfigOption( TARGET_NODE, IConfigOptionType.STRING, null, true, I18n.get("sql.1012") ) ); return options; } }); } /** * Process. * * @return array of pipelinestage results * @throws PipelineException */ public PipelineStageResult[] process() throws PipelineException { String script; checkOptions(); LoadOptions(); script = buildScript(); if(script.length() > 0) { this.getDocument().setBytes(script.getBytes()); } else { throw new PipelineException(I18n.get("sql.1001")); } return super.processHelper(); } /** * Check mandatory options. * @throws PipelineException */ private void checkOptions() throws PipelineException { if (!hasOption(RESOURCE_NAME)) { throw new PipelineException(I18n.get("sql.201")); } // !hasOption(WHERE_ROW_NAMES) if(getOptionList(new String[]{WHERE_ROW_NAMES}) == null ) { throw new PipelineException(I18n.get("sql.1005")); } if(!hasOption(TABLE_NAME)) { throw new PipelineException(I18n.get("sql.1006")); } if(!hasOption(SCHEMA_URL)) { throw new PipelineException("sql.1007"); } } private void LoadOptions() throws PipelineException { String schemaUrl; DOMParser parser; schemaUrl = getOptions(SCHEMA_URL); parser = new DOMParser(); try { parser.parse( new InputSource( new FileInputStream(schemaUrl) ) ); } catch (IOException e){ throw new PipelineException(I18n.get("sql.1002")); } catch (SAXException e) { throw new PipelineException(I18n.get("sql.1003")); } schema = parser.getDocument(); } private String buildScript() throws PipelineException { NameValuePair[] whereRows; NodeList nodeList, nodeChildList; Node node; int i; StringBuffer sql, query; String lineSeparator; sql = new StringBuffer(""); lineSeparator = System.getProperty("line.separator", "\n"); whereRows = getWhereRowsList(); nodeList = getDomNodeList(); try { conn = getConnection(); stmt = conn.createStatement(); for (i = 0; i < nodeList.getLength(); i++) { nodeChildList = nodeList.item(i).getChildNodes(); if(existsRow(nodeChildList, whereRows)) { query = sqlUpdate(nodeChildList, whereRows); sql.append(query); } else { query = sqlInsert(nodeChildList, whereRows); sql.append(query); } sql.append(lineSeparator); } } catch(ResourceException e) { throw new PipelineException(I18n.get("sql.103"), e); } catch (SQLException e) { throw new PipelineException(I18n.get("sql.104"), e); } finally { try { if (stmt != null) { stmt.close(); } if (conn != null) { resource.checkIn(conn); } } catch (Exception ignoreThis) {} } return sql.toString(); } private boolean existsRow(NodeList childList, NameValuePair[] whereRows) throws PipelineException { String query, value; ResultSet rs = null; boolean ret = false; query = sqlSelect(childList, whereRows); try { rs = stmt.executeQuery(query); rs.next(); value = rs.getString(1); if ((value != null) && Long.parseLong(value) > 0) { ret = true; } } catch (SQLException e) { throw new PipelineException(I18n.get("sql.104", query), e); } finally { try { if (rs != null) { rs.close(); } } catch (Exception ignoreThis) {} } return ret; } private NodeList getDomNodeList() throws PipelineException { String targetNode;//, schemaUrl; DOMParser parser; Document doc; NodeList nodeList; //Validator validador; //schemaUrl = getOptions(SCHEMA_URL); targetNode = getOptions(TARGET_NODE); parser = new DOMParser(); /* validador = new Validator(); try { parser.setFeature("http://xml.org/sax/features/validation", true); parser.setFeature("http://apache.org/xml/features/validation/schema", true); parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", schemaUrl ); // "http://apache.org/xml/properties/schema/external-schemaLocation" parser.setErrorHandler(validador); } catch(SAXNotRecognizedException e) { throw new PipelineException(I18n.get("")); // todo } catch(SAXNotSupportedException e) { throw new PipelineException(I18n.get("")); // todo } */ try { parser.parse( new InputSource( new ByteArrayInputStream( this.getDocument().getContents().getBytes() ) ) ); } catch (IOException ie){ throw new PipelineException(I18n.get("sql.1002")); } catch (SAXException e) { throw new PipelineException(I18n.get("sql.1003")); } /* if(validador.saxParseException != null) { throw new PipelineException(I18n.get(""), validador.saxParseException); // todo } */ doc = parser.getDocument(); if (targetNode != null && (!targetNode.equals(""))) { nodeList = doc.getElementsByTagName(targetNode); } else { nodeList = doc.getElementsByTagName("*"); } if (nodeList == null || nodeList.getLength() == 0) { throw new PipelineException(I18n.get("sql.1004")); } return nodeList; } private String getSchemaNodeType(String nodeName) { String out; NodeList nodeList; Node node; int i, j, alt; NamedNodeMap attr; nodeList = schema.getElementsByTagName("xs:element"); if(nodeList != null && nodeList.getLength() > 0) { for(i = 0; i < nodeList.getLength(); i++) { attr = nodeList.item(i).getAttributes(); if(attr != null && attr.getLength() > 1) { alt = -1; for (j = 0; j < attr.getLength(); j++) { if( attr.item(j).getNodeName().equalsIgnoreCase("name") && attr.item(j).getNodeValue().equalsIgnoreCase(nodeName)) { alt = j; break; } } if (alt > -1) { for (j = 0; j < attr.getLength(); j++) { // recycle if(j != alt && attr.item(j).getNodeName().equalsIgnoreCase("type")) { out = attr.item(j).getNodeValue(); alt = out.indexOf(':'); // recycle if (alt > -1) { return out.substring(alt + 1).toLowerCase(); } else { return out.toLowerCase(); } } } } } } } return ""; } private NameValuePair[] getWhereRowsList() throws PipelineException { NameValuePair[] attributes; attributes = this.getOptionList(new String[]{WHERE_ROW_NAMES}); if (attributes == null) { throw new PipelineException(I18n.get("sql.1008")); } return attributes; } /** * * @param nodeName DOM node name * @param whereRows where row names list */ private boolean isWhereRow(String nodeName, NameValuePair[] whereRows) { String val; int i; for (i = 0; i < whereRows.length; i++) { val = whereRows[i].getValue(); if ((val != null) && !val.equals("")) { if (val.equals(nodeName)) { return true; } } } return false; } private String sqlSelect(NodeList childList, NameValuePair[] whereRows) { return "SELECT COUNT(*) FROM " + getOptions(TABLE_NAME) + whereClause(childList, whereRows); } private StringBuffer whereClause(NodeList childList, NameValuePair[] whereRows) { StringBuffer buf; int j; Node node; buf = new StringBuffer(""); for(j = 0; j < childList.getLength(); j++) { if (childList.item(j).getNodeType() == Node.ELEMENT_NODE) { node = childList.item(j); if (isWhereRow(node.getNodeName(), whereRows)) { if (buf.length() > 0) { buf.append(" AND"); } buf.append(" "); buf.append(node.getNodeName()); buf.append(getNodeValue(node, true)); } } } if(buf.length() > 0) { buf.insert(0, " WHERE"); } return buf; } private StringBuffer getNodeValue(Node node, boolean withOperator) { String value; StringBuffer buf; boolean isStr; Node nodeChild; buf = new StringBuffer(""); if(withOperator) { if(isTypeNull(node)) { buf.append(" is "); } else { buf.append(" = "); } } isStr = isTypeString(node); nodeChild = node.getFirstChild(); if(isStr && nodeChild != null) { buf.append("'"); } if (nodeChild == null) { buf.append("null"); } else { value = nodeChild.getNodeValue(); if (isStr) { value = value.replaceAll("[']", "''"); } buf.append(value); } if(isStr && nodeChild != null) { buf.append("'"); } return buf; } private boolean isTypeString(Node node) { String typeName; typeName = getSchemaNodeType(node.getNodeName()); if ((typeName != null) && !typeName.equals("")) { if (typeName.equals("string") || typeName.equals("date")) { return true; } } return false; } private boolean isTypeNull(Node node) { String typeName; typeName = getSchemaNodeType(node.getNodeName()); if ((typeName != null) && !typeName.equals("")) { if (typeName.equals("null")) { return true; } } return false; } private StringBuffer sqlInsert(NodeList childList, NameValuePair[] whereRows) { StringBuffer buf, bufValues; int j, rowCount; Node node; buf = new StringBuffer(""); bufValues = new StringBuffer(""); buf.append("INSERT INTO "); buf.append(getOptions(TABLE_NAME)); buf.append(" ("); rowCount = 0; for(j = 0; j < childList.getLength(); j++) { if (childList.item(j).getNodeType() == Node.ELEMENT_NODE) { node = childList.item(j); if(rowCount > 0){ buf.append(", "); bufValues.append(", "); } buf.append(node.getNodeName()); bufValues.append(getNodeValue(node, false)); rowCount++; } } buf.append(") VALUES ("); buf.append(bufValues); buf.append(")"); return buf; } private StringBuffer sqlUpdate(NodeList childList, NameValuePair[] whereRows) { StringBuffer buf; int j, rowCount; Node node; buf = new StringBuffer(""); buf.append("UPDATE "); buf.append(getOptions(TABLE_NAME)); buf.append(" SET "); rowCount = 0; for(j = 0; j < childList.getLength(); j++) { if (childList.item(j).getNodeType() == Node.ELEMENT_NODE) { node = childList.item(j); if (!isWhereRow(node.getNodeName(), whereRows)) { if(rowCount > 0) { buf.append(", "); } buf.append(node.getNodeName()); buf.append(getNodeValue(node, true)); rowCount++; } } } buf.append(whereClause(childList, whereRows)); return buf; } /** * Get the named resource name from the resource factory * * @return JDBC Connection * * @throws ResourceException */ private Connection getConnection() throws ResourceException { if (resource == null) { resource = ResourceFactory.getResource(this.getOptions(RESOURCE_NAME)); if (resource == null) { throw new ResourceException(I18n.get("sql.103")); } } return ((Connection) resource.checkOut()); } } Index: FileWriterPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/FileWriterPipelineStage.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** FileWriterPipelineStage.java 4 Aug 2007 11:37:14 -0000 1.9 --- FileWriterPipelineStage.java 18 Dec 2007 10:02:55 -0000 1.10 *************** *** 119,124 **** options.add(new ConfigOption(DONE_FILE, IConfigOptionType.FILENAME, null, false, I18n.get("100021"))); ! options.add(new ConfigOption(ENCODING, IConfigOptionType.STRING, ! null, false, I18n.get("core.pipeline.stage.FileWriterPipelineStage.charset"))); return options; --- 119,128 ---- options.add(new ConfigOption(DONE_FILE, IConfigOptionType.FILENAME, null, false, I18n.get("100021"))); ! // 20071114 jmenriquez ! // ENCODING it's a general Option and NEW_LINE it's undefined ! //options.add(new ConfigOption(ENCODING, IConfigOptionType.STRING, ! // null, false, I18n.get("core.pipeline.stage.FileWriterPipelineStage.charset"))); ! options.add(new ConfigOption(NEW_LINE, IConfigOptionType.BOOLEAN, ! "false", false, I18n.get("100022"))); return options; *************** *** 172,175 **** --- 176,180 ---- if (document.isBinary()) { + LogService.getInstance().logDebug("FileWriterPipeline.writeFileMessage Write binary file"); FileOutputStream fos = new FileOutputStream(outfile, append); fos.write(document.getBytes()); *************** *** 177,184 **** } else { if(encoding==null){ ! LogService.getInstance().logDebug("FileWriterPipeline.writeFileMessage Write File with null encoding"); BufferedWriter writer = new BufferedWriter(new FileWriter(outfile, append)); ! writer.write(new String(document.getBytes())); if (newLine) { --- 182,191 ---- } else { if(encoding==null){ ! LogService.getInstance().logDebug("FileWriterPipeline.writeFileMessage Write File with PipelineDocument.encoding"); BufferedWriter writer = new BufferedWriter(new FileWriter(outfile, append)); ! // 20071114 jmenriquez ! //writer.write(new String(document.getBytes())); ! writer.write(document.getContents()); if (newLine) { *************** *** 190,196 **** else{ ! LogService.getInstance().logDebug("FileWriterPipeline.writeFileMessage Write File using encoding:" + encoding); ! BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile, append),encoding)); //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileWriter(outfile, append),encoding)) ; writer.write(new String(document.getBytes())); --- 197,203 ---- else{ ! LogService.getInstance().logDebug("FileWriterPipeline.writeFileMessage Write File using PipelineStage.encoding:" + encoding); ! BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile, append),encoding)); //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileWriter(outfile, append),encoding)) ; writer.write(new String(document.getBytes())); *************** *** 227,231 **** boolean newLine = "true".equalsIgnoreCase(this.getOptions(NEW_LINE)); ! String encoding = this.getOptions(ENCODING); // Get the donefile suffix --- 234,240 ---- boolean newLine = "true".equalsIgnoreCase(this.getOptions(NEW_LINE)); ! String encoding = this.getOptions(ENCODING);//PipelineStage.encoding ! //if encoding not null then writeFileMessage write using this encoding, ! //else use de default (PipelineDocument) encoding // Get the donefile suffix Index: GenericWriterPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/GenericWriterPipelineStage.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** GenericWriterPipelineStage.java 30 Jul 2004 01:33:04 -0000 1.6 --- GenericWriterPipelineStage.java 18 Dec 2007 10:02:55 -0000 1.7 *************** *** 124,127 **** --- 124,131 ---- newDocument.setMimeType(this.getDocument().getMimeType()); newDocument.setBinary(this.getDocument().isBinary()); + // 20071114 jmenriquez + // why not setEncoding and setName? + newDocument.setEncoding(this.getDocument().getEncoding()); + newDocument.setName(this.getDocument().getName()); return newDocument; --- NEW FILE: EncodingConversionPipelineStage.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/EncodingConversionPipelineStage.java,v 1.1 2007/12/18 10:02:55 josem_dev Exp $ * $DateTime$ * $Author: josem_dev $ * */ package com.babeldoc.core.pipeline.stage; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.pipeline.PipelineException; import com.babeldoc.core.pipeline.PipelineStage; import com.babeldoc.core.pipeline.PipelineStageInfo; import com.babeldoc.core.pipeline.PipelineStageResult; import com.babeldoc.core.pipeline.PipelineDocument; /** * Encoding conversión of the document with the defined encoding. * * @author josem_dev * @version 1.0 */ public class EncodingConversionPipelineStage extends PipelineStage { /** * Construct this stage - create the information object */ public EncodingConversionPipelineStage() { super(new PipelineStageInfo() { public String getName() { return "EncodingConversion"; } public String getDescription() { return I18n.get("core.pipeline.stage.EncodingConversion.desc"); } public Collection getTypeSpecificOptions() { ArrayList options = new ArrayList(); options.add( new ConfigOption( ENCODING, IConfigOptionType.STRING, null, true, I18n.get("core.pipeline.stage.EncodingConversionPipelineStage.charset") ) ); return options; } }); } /** * Encoding conversion of de input document with the output encoding. * * @param inputDocument pipeline document to convert * @param outputEncoding desired encoding */ public void convert(PipelineDocument inputDocument, String outputEncoding) throws PipelineException { byte[] newBuffer; if(inputDocument.isBinary()) { if (LogService.getInstance().isWarnEnabled()) { LogService.getInstance().logWarn("EncodingConversionPipelineStage.convert() document is binary, not encoding conversion"); } } else { try { newBuffer = inputDocument.getContents().getBytes(outputEncoding); this.getDocument().setBytes(newBuffer); } catch (UnsupportedEncodingException x) { throw new PipelineException( "[EncodingConversionPipelineStage.convert] UnsupportedEncodingException, encoding: " + outputEncoding, x ); } } } /** * Process. * * @return array of pipelinestage results * @throws PipelineException */ public PipelineStageResult[] process() throws PipelineException { try { convert(this.getDocument(), this.getOptions(ENCODING)); return super.processHelper(); } catch (PipelineException pe) { throw pe; } catch (Exception x) { throw new PipelineException( "[EncodingConversionPipelineStage.process] exception", x ); } } } |