001: /*
002: * WbXslt.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.sql.wbcommands;
013:
014: import java.sql.SQLException;
015: import workbench.log.LogMgr;
016: import workbench.resource.ResourceMgr;
017: import workbench.sql.SqlCommand;
018: import workbench.sql.StatementRunnerResult;
019: import workbench.util.ArgumentParser;
020: import workbench.util.SqlUtil;
021: import workbench.util.XsltTransformer;
022:
023: /**
024: *
025: * @author support@sql-workbench.net
026: */
027: public class WbXslt extends SqlCommand {
028: public static final String VERB = "WBXSLT";
029: public static final String ARG_STYLESHEET = "stylesheet";
030: public static final String ARG_OUTPUT = "xsltOutput";
031: public static final String ARG_INPUT = "inputFile";
032:
033: public WbXslt() {
034: cmdLine = new ArgumentParser();
035: cmdLine.addArgument(ARG_STYLESHEET);
036: cmdLine.addArgument(ARG_OUTPUT);
037: cmdLine.addArgument(ARG_INPUT);
038: }
039:
040: public String getVerb() {
041: return VERB;
042: }
043:
044: protected boolean isConnectionRequired() {
045: return false;
046: }
047:
048: public StatementRunnerResult execute(String aSql)
049: throws SQLException {
050: StatementRunnerResult result = new StatementRunnerResult();
051: if (!aSql.trim().toUpperCase().startsWith(this .getVerb())) {
052: result.addMessage("Wrong command! " + this .getVerb()
053: + " expected.");
054: result.setFailure();
055: return result;
056: }
057:
058: String parm = SqlUtil.stripVerb(aSql);
059:
060: cmdLine.parse(parm);
061:
062: String inputFile = cmdLine.getValue(ARG_INPUT);
063: String outputFile = cmdLine.getValue(ARG_OUTPUT);
064: String xsltFile = cmdLine.getValue(ARG_STYLESHEET);
065:
066: if (!cmdLine.hasArguments()) {
067: result.addMessage(ResourceMgr
068: .getString("ErrXsltWrongParameter"));
069: result.setFailure();
070: return result;
071: }
072:
073: if (inputFile == null) {
074: result.addMessage(ResourceMgr
075: .getString("ErrXsltMissingInputFile"));
076: result.setFailure();
077: return result;
078: }
079:
080: if (outputFile == null) {
081: result.addMessage(ResourceMgr
082: .getString("ErrXsltMissingOutputFile"));
083: result.setFailure();
084: return result;
085: }
086:
087: if (xsltFile == null) {
088: result.addMessage(ResourceMgr
089: .getString("ErrXsltMissingStylesheet"));
090: result.setFailure();
091: return result;
092: }
093:
094: try {
095: XsltTransformer.transformFile(inputFile, outputFile,
096: xsltFile);
097: result.addMessage(ResourceMgr.getFormattedString(
098: "MsgXsltSuccessful", outputFile));
099: result.setSuccess();
100: } catch (Exception e) {
101: LogMgr.logError("WbXslt.execute()",
102: "Error when transforming '" + inputFile + "' to '"
103: + outputFile + "' using " + xsltFile, e);
104: result.addMessage(e.getMessage());
105: }
106: return result;
107: }
108:
109: }
|