use of org.pentaho.di.core.ResultFile in project pentaho-kettle by pentaho.
the class XMLInput method openNextFile.
private boolean openNextFile() {
try {
if (data.filenr >= data.files.size()) {
// finished processing!
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "XMLInput.Log.FinishedProcessing"));
}
return false;
}
// Is this the last file?
data.last_file = (data.filenr == data.files.size() - 1);
data.file = data.files.get(data.filenr);
logBasic(BaseMessages.getString(PKG, "XMLInput.Log.OpeningFile", data.file.toString()));
// Move file pointer ahead!
data.filenr++;
String baseURI = this.environmentSubstitute(meta.getFileBaseURI());
if (Utils.isEmpty(baseURI)) {
baseURI = data.file.getParent().getName().getURI();
}
// Open the XML document
data.document = XMLHandler.loadXMLFile(data.file, baseURI, meta.isIgnoreEntities(), meta.isNamespaceAware());
// Add this to the result file names...
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file, getTransMeta().getName(), getStepname());
resultFile.setComment("File was read by an XML input step");
addResultFile(resultFile);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "XMLInput.Log.FileOpened", data.file.toString()));
}
// Position in the file...
data.section = data.document;
for (int i = 0; i < meta.getInputPosition().length - 1; i++) {
data.section = XMLHandler.getSubNode(data.section, meta.getInputPosition()[i]);
}
// Last element gets repeated: what's the name?
data.itemElement = meta.getInputPosition()[meta.getInputPosition().length - 1];
data.itemCount = XMLHandler.countNodes(data.section, data.itemElement);
data.itemPosition = meta.getNrRowsToSkip();
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "XMLInput.Log.UnableToOpenFile", "" + data.filenr, data.file.toString(), e.toString()));
stopAll();
setErrors(1);
return false;
}
return true;
}
use of org.pentaho.di.core.ResultFile in project pentaho-kettle by pentaho.
the class JobEntryXMLWellFormed method addFileToResultFilenames.
private void addFileToResultFilenames(String fileaddentry, Result result, Job parentJob) {
try {
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(fileaddentry, this), parentJob.getJobname(), toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobXMLWellFormed.Log.FileAddedToResultFilesName", fileaddentry));
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobXMLWellFormed.Error.AddingToFilenameResult", fileaddentry, e.getMessage()));
}
}
use of org.pentaho.di.core.ResultFile in project pentaho-kettle by pentaho.
the class JobEntryXSLT method processOneXMLFile.
private boolean processOneXMLFile(String xmlfilename, String xslfilename, String outputfilename, Result result, Job parentJob) {
boolean retval = false;
FileObject xmlfile = null;
FileObject xslfile = null;
FileObject outputfile = null;
try {
xmlfile = KettleVFS.getFileObject(xmlfilename, this);
xslfile = KettleVFS.getFileObject(xslfilename, this);
outputfile = KettleVFS.getFileObject(outputfilename, this);
if (xmlfile.exists() && xslfile.exists()) {
if (outputfile.exists() && iffileexists == 2) {
// Output file exists
// User want to fail
logError(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
return retval;
} else if (outputfile.exists() && iffileexists == 1) {
// Do nothing
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
}
retval = true;
return retval;
} else {
if (outputfile.exists() && iffileexists == 0) {
// the output file exists and user want to create new one with unique name
// Format Date
// Try to clean filename (without wildcard)
String wildcard = outputfilename.substring(outputfilename.length() - 4, outputfilename.length());
if (wildcard.substring(0, 1).equals(".")) {
// Find wildcard
outputfilename = outputfilename.substring(0, outputfilename.length() - 4) + "_" + StringUtil.getFormattedDateTimeNow(true) + wildcard;
} else {
// did not find wildcard
outputfilename = outputfilename + "_" + StringUtil.getFormattedDateTimeNow(true);
}
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileExists2.Label"));
logDebug(BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange1.Label") + outputfilename + BaseMessages.getString(PKG, "JobEntryXSLT.OuputFileNameChange2.Label"));
}
}
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInstance();
if (xsltfactory.equals(FACTORY_SAXON)) {
// Set the TransformerFactory to the SAXON implementation.
factory = new net.sf.saxon.TransformerFactoryImpl();
}
if (log.isDetailed()) {
log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactoryInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerFactory", factory.getClass().getName()));
}
InputStream xslInputStream = KettleVFS.getInputStream(xslfile);
InputStream xmlInputStream = KettleVFS.getInputStream(xmlfile);
OutputStream os = null;
try {
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(xslInputStream));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
if (log.isDetailed()) {
log.logDetailed(BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClassInfos"), BaseMessages.getString(PKG, "JobEntryXSL.Log.TransformerClass", xformer.getClass().getName()));
}
// Do we need to set output properties?
if (setOutputProperties) {
xformer.setOutputProperties(outputProperties);
}
// Do we need to pass parameters?
if (useParameters) {
for (int i = 0; i < nrParams; i++) {
xformer.setParameter(nameOfParams[i], valueOfParams[i]);
}
}
// Prepare the input and output files
Source source = new StreamSource(xmlInputStream);
os = KettleVFS.getOutputStream(outputfile, false);
StreamResult resultat = new StreamResult(os);
// Apply the xsl file to the source file and write the result to the output file
xformer.transform(source, resultat);
if (isAddFileToResult()) {
// Add output filename to output files
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject(outputfilename, this), parentJob.getJobname(), toString());
result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
}
// Everything is OK
retval = true;
} finally {
try {
xslInputStream.close();
} catch (IOException ignored) {
// ignore IO Exception on close
}
try {
xmlInputStream.close();
} catch (IOException ignored) {
// ignore IO Exception on close
}
try {
if (os != null) {
os.close();
}
} catch (IOException ignored) {
// ignore IO Exception on close
}
}
}
} else {
if (!xmlfile.exists()) {
logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label"));
}
if (!xslfile.exists()) {
logError(BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.FileDoesNotExist2.Label"));
}
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLST.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML1.Label") + xmlfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXML2.Label") + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL1.Label") + xslfilename + BaseMessages.getString(PKG, "JobEntryXSLT.ErrorXLSTXSL2.Label") + e.getMessage());
} finally {
try {
if (xmlfile != null) {
xmlfile.close();
}
if (xslfile != null) {
xslfile.close();
}
if (outputfile != null) {
outputfile.close();
}
} catch (IOException e) {
logError("Unable to close file", e);
}
}
return retval;
}
use of org.pentaho.di.core.ResultFile in project pentaho-kettle by pentaho.
the class XMLInputStream method openNextFile.
private boolean openNextFile() throws KettleException {
try {
closeFile();
if (data.filenr >= data.filenames.length) {
return false;
}
data.fileObject = KettleVFS.getFileObject(data.filenames[data.filenr], getTransMeta());
data.inputStream = KettleVFS.getInputStream(data.fileObject);
data.xmlEventReader = data.staxInstance.createXMLEventReader(data.inputStream, data.encoding);
} catch (IOException e) {
throw new KettleException(e);
} catch (XMLStreamException e) {
throw new KettleException(e);
}
data.filenr++;
if (meta.isAddResultFile()) {
// Add this to the result file names...
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.fileObject, getTransMeta().getName(), getStepname());
resultFile.setComment(BaseMessages.getString(PKG, "XMLInputStream.Log.ResultFileWasRead"));
addResultFile(resultFile);
}
return true;
}
use of org.pentaho.di.core.ResultFile in project pentaho-kettle by pentaho.
the class FilesFromResult method processRow.
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (data.resultFilesList == null || getLinesRead() >= data.resultFilesList.size()) {
setOutputDone();
return false;
}
ResultFile resultFile = data.resultFilesList.get((int) getLinesRead());
RowMetaAndData r = resultFile.getRow();
if (first) {
first = false;
data.outputRowMeta = new RowMeta();
smi.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
}
incrementLinesRead();
// copy row to possible alternate
putRow(data.outputRowMeta, r.getData());
if (checkFeedback(getLinesRead())) {
logBasic(BaseMessages.getString(PKG, "FilesFromResult.Log.LineNumber") + getLinesRead());
}
return true;
}
Aggregations