Commits

tbrugz committed 884a55f

datadump: xml: added props '.escape' & '.escapecols4table@<table-name>'

Comments (0)

Files changed (6)

sqldump.template.properties

 sqldump.datadump.xml.dumprowelement4table@<table-name>=true|false
 sqldump.datadump.xml.dumpheader4innertables=false|true
 sqldump.datadump.xml.dumptablenameasrowtag=ALWAYS | IFHASDATA | NEVER
+# escape XML special chars (inherited by HTML syntax)
+sqldump.datadump.xml.escape=false | true
+sqldump.datadump.xml.escapecols4table@<table-name>=COL1, COL2
 
 # HTML syntax props
 sqldump.datadump.html.nullvalue=

src/tbrugz/sqldump/datadump/DataDumpUtils.java

 
 	//dumpers: XML, HTML
 	//XXXdone: XML format: translate '<', '>', '&'?
-	public static String getFormattedXMLValue(Object elem, Class<?> type, NumberFormat floatFormatter, DateFormat df, String nullValue) {
-		String value = getFormattedXMLValue(elem, type, floatFormatter, df);
+	public static String getFormattedXMLValue(Object elem, Class<?> type, NumberFormat floatFormatter, DateFormat df, String nullValue, boolean escape) {
+		String value = getFormattedXMLValue(elem, type, floatFormatter, df, escape);
 		if(value == null) {
 			return nullValue;
 		}
 		return value;
 	} 
 
-	static String getFormattedXMLValue(Object elem, Class<?> type, NumberFormat floatFormatter, DateFormat df) {
+	static String getFormattedXMLValue(Object elem, Class<?> type, NumberFormat floatFormatter, DateFormat df, boolean escape) {
 		if(elem == null) {
 			return null;
 		}
 			return null;
 		}
 
-		return xmlEscapeText(getString(elem));
+		return escape?xmlEscapeText(getString(elem)):getString(elem);
 	}
 	
 	/*

src/tbrugz/sqldump/datadump/DocbookTable.java

 		fos.write("\t<row"+drawRowSepStr+">");
 		
 		for(int i=0;i<numCol;i++) {
-			Object value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter, nullValueStr);
+			Object value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter, nullValueStr, escape);
 			fos.write("<entry>"+value+"</entry>");
 		}
 		fos.write("</row>\n");

src/tbrugz/sqldump/datadump/HTMLDataDump.java

 import java.io.IOException;
 import java.io.Writer;
 import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
 import tbrugz.sqldump.util.SQLUtils;
 
 //XXX: prop for stylesheet?
-//XXX: should extend XMLDataDump?
-public class HTMLDataDump extends DumpSyntax {
+//XXXdone: should extend XMLDataDump?
+public class HTMLDataDump extends XMLDataDump {
 
 	static final Log log = LogFactory.getLog(HTMLDataDump.class);
 
 	static final String PROP_HTML_PREPEND = "sqldump.datadump.html.prepend";
 	static final String PROP_HTML_APPEND = "sqldump.datadump.html.append";
 	
-	protected String tableName;
-	protected int numCol;
-	protected List<String> lsColNames = new ArrayList<String>();
-	protected List<Class<?>> lsColTypes = new ArrayList<Class<?>>();
+	//protected String tableName;
+	//protected int numCol;
+	//protected List<String> lsColNames = new ArrayList<String>();
+	//protected List<Class<?>> lsColTypes = new ArrayList<Class<?>>();
 
 	protected String padding = "";
 	
 	
 	@Override
 	public void procProperties(Properties prop) {
-		procStandardProperties(prop);
+		super.procProperties(prop);
+		//procStandardProperties(prop);
 		prepend = prop.getProperty(PROP_HTML_PREPEND);
 		append = prop.getProperty(PROP_HTML_APPEND);
 	}
 
-	@Override
+	/*@Override
 	public void initDump(String tableName, List<String> pkCols, ResultSetMetaData md) throws SQLException {
 		this.tableName = tableName;
 		numCol = md.getColumnCount();
 			lsColNames.add(md.getColumnName(i+1));
 			lsColTypes.add(SQLUtils.getClassFromSqlType(md.getColumnType(i+1), md.getPrecision(i+1), md.getScale(i+1)));
 		}
-	}
+	}*/
 	
 	@Override
 	public void dumpHeader(Writer fos) throws IOException {
 				sb.append("\n\t</td>");
 			}
 			else {
-				Object value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter, nullValueStr);
+				Object value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter, nullValueStr,
+						escape || (cols2Escape!=null && cols2Escape.contains(lsColNames.get(i))));
 				//Object value = getValueNotNull( vals.get(i) );
 				sb.append( "<td>"+ value +"</td>");
 			}
 		if(append!=null) { out(append, fos); }
 	}
 
-	protected void out(String s, Writer pw) throws IOException {
+	/*protected void out(String s, Writer pw) throws IOException {
 		pw.write(padding+s);
-	}
+	}*/
 	
 	@Override
 	public String getSyntaxId() {

src/tbrugz/sqldump/datadump/XMLDataDump.java

 	static final String PREFIX_DUMPROWELEMENT4TABLE = "sqldump.datadump.xml.dumprowelement4table@";
 	static final String PROP_DUMPHEADER4INNERTABLES = "sqldump.datadump.xml.dumpheader4innertables";
 	static final String PROP_DUMPTABLENAMEASROWTAG = "sqldump.datadump.xml.dumptablenameasrowtag";
+
+	static final String PROP_XML_ESCAPE = "sqldump.datadump.xml.escape";
+	static final String PREFIX_ESCAPECOLS_4TABLE = "sqldump.datadump.xml.escapecols4table@";
 	
 	//static final String PREFIX_ROWELEMENT4COLUMN = "sqldump.datadump.xml.rowelement4column@";
 	//static final String PREFIX_DUMPROWELEMENT4COLUMN = "sqldump.datadump.xml.dumprowelement4column@";
 	boolean dumpNullValues = true;
 	HeaderFooterDump dumpHeader4InnerTables = HeaderFooterDump.ALWAYS;
 	boolean dumpTableNameAsRowTag = false;
+	protected boolean escape = false;
+	List<String> cols2Escape = null;
 
 	//dumper properties
-	String tableName;
-	int numCol;
+	protected String tableName;
+	protected int numCol;
 	String rowElement = defaultRowElement;
 	boolean dumpRowElement = defaultDumpRowElement;
 	
-	final List<String> lsColNames = new ArrayList<String>();
-	final List<Class<?>> lsColTypes = new ArrayList<Class<?>>();
+	protected final List<String> lsColNames = new ArrayList<String>();
+	protected final List<Class<?>> lsColTypes = new ArrayList<Class<?>>();
 	
 	@Override
 	public void procProperties(Properties prop) {
 		dumpNullValues = Utils.getPropBool(prop, PROP_DUMPNULLVALUES, dumpNullValues);
 		dumpHeader4InnerTables = HeaderFooterDump.valueOf(prop.getProperty(PROP_DUMPHEADER4INNERTABLES, dumpHeader4InnerTables.name()));
 		dumpTableNameAsRowTag = Utils.getPropBool(prop, PROP_DUMPTABLENAMEASROWTAG, dumpTableNameAsRowTag);
+		escape = Utils.getPropBool(prop, PROP_XML_ESCAPE, escape);
 		this.prop = prop;
 	}
 
 		lsColTypes.clear();
 		for(int i=0;i<numCol;i++) {
 			lsColNames.add(md.getColumnName(i+1));
-		}
-		for(int i=0;i<numCol;i++) {
 			lsColTypes.add(SQLUtils.getClassFromSqlType(md.getColumnType(i+1), md.getPrecision(i+1), md.getScale(i+1)));
 		}
 		
 		rowElement = prop.getProperty(PREFIX_ROWELEMENT4TABLE+tableName, dumpTableNameAsRowTag?tableName:defaultRowElement);
 		dumpRowElement = Utils.getPropBool(prop, PREFIX_DUMPROWELEMENT4TABLE+tableName, defaultDumpRowElement);
+		cols2Escape = Utils.getStringListFromProp(prop, PREFIX_ESCAPECOLS_4TABLE+tableName, ",");
 	}
 	
 	@Override
 				//sb.append("\t");
 			}
 			else {
-				String value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter);
+				String value = DataDumpUtils.getFormattedXMLValue(vals.get(i), lsColTypes.get(i), floatFormatter, dateFormatter,
+						escape || (cols2Escape!=null && cols2Escape.contains(lsColNames.get(i))));
 				if(value==null) {
 					if(dumpNullValues) {
 						sb.append( "<"+lsColNames.get(i)+">"+ nullValueStr +"</"+lsColNames.get(i)+">" );
 		}
 	}
 
-	void out(String s, Writer pw) throws IOException {
+	protected void out(String s, Writer pw) throws IOException {
 		pw.write(padding+s);
 	}
 	

src_test/tbrugz/sqldump/datadump/DataDumpTest.java

 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 import tbrugz.sqldump.SQLDump;
 import tbrugz.sqldump.TestUtil;
 	}
 	
 	public void dump1() throws ClassNotFoundException, SQLException, NamingException, IOException {
+		dump1(null);
+	}
+	
+	public void dump1(String[] xtraparams) throws ClassNotFoundException, SQLException, NamingException, IOException {
 		String[] vmparamsDump = {
 				"-Dsqldump.grabclass=JDBCSchemaGrabber",
 				"-Dsqldump.processingclasses=DataDump",
 				"-Dsqldump.datadump.dumpsyntaxes=insertinto, csv, xml, html, json",
 				"-Dsqldump.datadump.outfilepattern="+DIR_OUT+"/data_[tablename].[syntaxfileext]",
 				"-Dsqldump.datadump.writebom=false",
+				"-Dsqldump.datadump.xml.escape=true",
 				"-Dsqldump.driverclass=org.h2.Driver",
 				"-Dsqldump.dburl=jdbc:h2:"+dbpath,
 				"-Dsqldump.user=h",
 		SQLDump sqld = new SQLDump();
 		Properties p = new Properties();
 		TestUtil.setProperties(p, vmparamsDump);
+		if(xtraparams!=null) {
+			TestUtil.setProperties(p, xtraparams);
+		}
 		sqld.doMain(params, p, null);
 	}
 	
 		Assert.assertEquals(6, countElements(n.getChildNodes()));
 	}
 
+	@Test(expected=SAXParseException.class)
+	public void testXMLNoEscape() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, SQLException, NamingException {
+		dump1(new String[]{
+				"-Dsqldump.datadump.xml.escape=false",
+				});
+		File f = new File(DIR_OUT+"/data_ETC.xml");
+		parseXML(f);
+	}
+
+	@Test
+	public void testHTMLEscapeCol() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, SQLException, NamingException {
+		dump1(new String[]{
+				"-Dsqldump.datadump.xml.escape=false",
+				"-Dsqldump.datadump.xml.escapecols4table@ETC=DESCRIPTION",
+				});
+		File f = new File(DIR_OUT+"/data_ETC.html");
+		Document doc = parseXML(f);
+		
+		Node n = doc.getChildNodes().item(0);
+		Assert.assertEquals(7, countElements(n.getChildNodes()));
+	}
+	
 	@Test
 	public void testHTML() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, SQLException, NamingException {
 		dump1();