com.jpattern.core.xml.XmlReaderCommand Maven / Gradle / Ivy
package com.jpattern.core.xml;
import java.io.IOException;
import java.io.InputStream;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import com.jpattern.core.command.NullCommand;
import com.jpattern.core.command.ACommand;
import com.jpattern.core.command.ICommand;
import com.jpattern.core.command.ICommandResult;
import com.jpattern.logger.ILogger;
import com.jpattern.shared.result.ErrorMessage;
import com.jpattern.shared.util.GenericWrapper;
/**
*
* @author Francesco Cina'
*
* 16/giu/2010
*/
public class XmlReaderCommand extends ACommand {
private static final long serialVersionUID = 1L;
private InputStream xmlFilePath;
private IXmlReaderStrategy xmlParserStrategy;
private GenericWrapper trimValues;
public XmlReaderCommand(InputStream xmlFilePath, GenericWrapper trimValues, IXmlReaderStrategy xmlParserStrategy) {
this(xmlFilePath, trimValues, xmlParserStrategy, new NullCommand());
}
public XmlReaderCommand(InputStream xmlFilePath, GenericWrapper trimValues, IXmlReaderStrategy xmlParserStrategy, ICommand aSuccessor) {
super(aSuccessor);
this.xmlFilePath = xmlFilePath;
this.xmlParserStrategy = xmlParserStrategy;
this.trimValues = trimValues;
}
@Override
protected void internalRollBack(ICommandResult result) {
}
@Override
protected void result(ICommandResult result) {
ILogger logger = getProvider().getLoggerService().logger(XmlReaderCommand.class);
logger.info("result", "Starting build POJO from xml file " + xmlFilePath.toString() );
try {
// Create SAX 2 parser...
XMLReader xr = XMLReaderFactory.createXMLReader();
// Set the ContentHandler...
xr.setContentHandler(new SaxXmlDefaultHandler( xmlParserStrategy , trimValues.getValue() ));
// Parse the file...
xr.parse(new InputSource(xmlFilePath));
} catch (Exception e) {
result.addErrorMessage( new ErrorMessage("XmlParserCommand", "Error parsing xml file " + xmlFilePath.toString()));
logger.error("result", "error parsing xml file " + xmlFilePath.toString(), e);
} finally {
try {
if (xmlFilePath!=null) {
xmlFilePath.close();
}
} catch (IOException e) {
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy