Implemented: converted EntitySaxReader to use the default Sax reader instead
of the legacy implementation from Javolution; removed a bunch of unused or deprecated methods; created some unit tests to test the EntitySaxReader in isolation. Removed the dependency from Javolution since this was the last class dependent on it. (OFBIZ-5169) git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1762079 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -1041,13 +1041,15 @@ public class EntityTestSuite extends EntityTestCase {
|
||||
* Tests EntitySaxReader, verification loading data with tag create, create-update, create-replace, delete
|
||||
*/
|
||||
public void testEntitySaxReaderCreation() throws Exception {
|
||||
String xmlContentLoad =
|
||||
String xmlContentLoad =
|
||||
"<entity-engine-xml>" +
|
||||
"<TestingType testingTypeId=\"JUNIT-TEST\" description=\"junit test\"/>" +
|
||||
"<create>" +
|
||||
" <TestingType testingTypeId=\"JUNIT-TEST2\" description=\"junit test\"/>" +
|
||||
" <Testing testingId=\"T1\" testingTypeId=\"JUNIT-TEST\" testingName=\"First test\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" +
|
||||
"</create>" +
|
||||
"<Testing testingId=\"T2\" testingTypeId=\"JUNIT-TEST2\" testingName=\"Second test\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>";
|
||||
"<Testing testingId=\"T2\" testingTypeId=\"JUNIT-TEST2\" testingName=\"Second test\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>" +
|
||||
"</entity-engine-xml>";
|
||||
EntitySaxReader reader = new EntitySaxReader(delegator);
|
||||
long numberLoaded = reader.parse(xmlContentLoad);
|
||||
assertEquals("Create Entity loaded ", 4, numberLoaded);
|
||||
@@ -1068,8 +1070,10 @@ public class EntityTestSuite extends EntityTestCase {
|
||||
|
||||
public void testEntitySaxReaderCreateSkip() throws Exception {
|
||||
String xmlContentLoad =
|
||||
"<entity-engine-xml>" +
|
||||
"<TestingType testingTypeId=\"reader-create-skip\" description=\"reader create skip\"/>" +
|
||||
"<Testing testingId=\"reader-create-skip\" testingTypeId=\"reader-create-skip\" testingName=\"reader create skip\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>";
|
||||
"<Testing testingId=\"reader-create-skip\" testingTypeId=\"reader-create-skip\" testingName=\"reader create skip\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" +
|
||||
"</entity-engine-xml>";
|
||||
EntitySaxReader reader = new EntitySaxReader(delegator);
|
||||
long numberLoaded = reader.parse(xmlContentLoad);
|
||||
xmlContentLoad =
|
||||
@@ -1089,13 +1093,15 @@ public class EntityTestSuite extends EntityTestCase {
|
||||
|
||||
public void testEntitySaxReaderUpdate() throws Exception {
|
||||
String xmlContentLoad =
|
||||
"<entity-engine-xml>" +
|
||||
"<TestingType testingTypeId=\"create-update\" description=\"create update\"/>" +
|
||||
"<TestingType testingTypeId=\"create-updated\" description=\"create update updated\"/>" +
|
||||
"<Testing testingId=\"create-update-T3\" testingTypeId=\"create-update\" testingName=\"Test 3\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" +
|
||||
"<create-update>" +
|
||||
" <Testing testingId=\"create-update-T1\" testingTypeId=\"create-update\" testingName=\"First test update\" testingSize=\"20\" testingDate=\"2010-01-01 00:00:00\"/>" +
|
||||
" <Testing testingId=\"create-update-T3\" testingTypeId=\"create-updated\" testingName=\"Third test\" testingSize=\"30\" testingDate=\"2010-03-01 00:00:00\"/>" +
|
||||
"</create-update>";
|
||||
"</create-update>" +
|
||||
"</entity-engine-xml>";
|
||||
EntitySaxReader reader = new EntitySaxReader(delegator);
|
||||
long numberLoaded = reader.parse(xmlContentLoad);
|
||||
assertEquals("Update Entity loaded ", 5, numberLoaded);
|
||||
@@ -1116,12 +1122,14 @@ public class EntityTestSuite extends EntityTestCase {
|
||||
|
||||
public void testEntitySaxReaderReplace() throws Exception {
|
||||
String xmlContentLoad =
|
||||
"<entity-engine-xml>" +
|
||||
"<TestingType testingTypeId=\"create-replace\" description=\"reader create skip\"/>" +
|
||||
"<Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T1\" testingName=\"First test\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" +
|
||||
"<create-replace>" +
|
||||
" <Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T1\" testingName=\"First test replace\" />" +
|
||||
"</create-replace>" +
|
||||
"<Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T2\" testingName=\"Second test update\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>";
|
||||
"<Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T2\" testingName=\"Second test update\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>" +
|
||||
"</entity-engine-xml>";
|
||||
EntitySaxReader reader = new EntitySaxReader(delegator);
|
||||
long numberLoaded = reader.parse(xmlContentLoad);
|
||||
assertEquals("Replace Entity loaded ", 4, numberLoaded);
|
||||
|
||||
@@ -27,10 +27,14 @@ import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
import org.apache.ofbiz.base.location.FlexibleLocation;
|
||||
import org.apache.ofbiz.base.util.Base64;
|
||||
@@ -53,24 +57,20 @@ import org.apache.ofbiz.entity.transaction.TransactionUtil;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import freemarker.ext.dom.NodeModel;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateHashModel;
|
||||
import javolution.text.CharArray;
|
||||
import javolution.text.Text;
|
||||
import javolution.xml.sax.Attributes;
|
||||
import javolution.xml.sax.XMLReaderImpl;
|
||||
|
||||
/**
|
||||
* SAX XML Parser Content Handler for Entity Engine XML files
|
||||
*/
|
||||
public class EntitySaxReader implements javolution.xml.sax.ContentHandler, ErrorHandler {
|
||||
|
||||
public class EntitySaxReader extends DefaultHandler {
|
||||
public static final String module = EntitySaxReader.class.getName();
|
||||
public static final int DEFAULT_TX_TIMEOUT = 7200;
|
||||
|
||||
@@ -79,7 +79,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
protected EntityEcaHandler<?> ecaHandler = null;
|
||||
protected GenericValue currentValue = null;
|
||||
protected CharSequence currentFieldName = null;
|
||||
protected CharSequence currentFieldValue = null;
|
||||
private char[] currentFieldValue = null;
|
||||
protected long numberRead = 0;
|
||||
protected long numberCreated = 0;
|
||||
protected long numberUpdated = 0;
|
||||
@@ -94,16 +94,14 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
protected boolean maintainTxStamps = false;
|
||||
protected boolean createDummyFks = false;
|
||||
protected boolean checkDataOnly = false;
|
||||
@Deprecated
|
||||
protected boolean doCacheClear = true;
|
||||
protected boolean disableEeca = false;
|
||||
protected enum Action {CREATE, CREATE_UPDATE, CREATE_REPLACE, DELETE};
|
||||
protected List<String> actionTags = UtilMisc.toList("create", "create-update", "create-replace", "delete");
|
||||
protected Action currentAction = Action.CREATE_UPDATE;
|
||||
protected List<Object> messageList = null;
|
||||
|
||||
protected List<GenericValue> valuesToWrite = new ArrayList<GenericValue>(valuesPerWrite);
|
||||
protected List<GenericValue> valuesToDelete = new ArrayList<GenericValue>(valuesPerWrite);
|
||||
protected List<GenericValue> valuesToWrite = new ArrayList<>(valuesPerWrite);
|
||||
protected List<GenericValue> valuesToDelete = new ArrayList<>(valuesPerWrite);
|
||||
|
||||
protected boolean isParseForTemplate = false;
|
||||
protected CharSequence templatePath = null;
|
||||
@@ -124,22 +122,6 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
this(delegator, DEFAULT_TX_TIMEOUT);
|
||||
}
|
||||
|
||||
public int getValuesPerWrite() {
|
||||
return this.valuesPerWrite;
|
||||
}
|
||||
|
||||
public void setValuesPerWrite(int valuesPerWrite) {
|
||||
this.valuesPerWrite = valuesPerWrite;
|
||||
}
|
||||
|
||||
public int getValuesPerMessage() {
|
||||
return this.valuesPerMessage;
|
||||
}
|
||||
|
||||
public void setValuesPerMessage(int valuesPerMessage) {
|
||||
this.valuesPerMessage = valuesPerMessage;
|
||||
}
|
||||
|
||||
public int getTransactionTimeout() {
|
||||
return this.transactionTimeout;
|
||||
}
|
||||
@@ -155,26 +137,14 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getMaintainTxStamps() {
|
||||
return this.maintainTxStamps;
|
||||
}
|
||||
|
||||
public void setMaintainTxStamps(boolean maintainTxStamps) {
|
||||
this.maintainTxStamps = maintainTxStamps;
|
||||
}
|
||||
|
||||
public boolean getCreateDummyFks() {
|
||||
return this.createDummyFks;
|
||||
}
|
||||
|
||||
public void setCreateDummyFks(boolean createDummyFks) {
|
||||
this.createDummyFks = createDummyFks;
|
||||
}
|
||||
|
||||
public boolean getCheckDataOnly() {
|
||||
return this.checkDataOnly;
|
||||
}
|
||||
|
||||
public void setCheckDataOnly(boolean checkDataOnly) {
|
||||
this.checkDataOnly = checkDataOnly;
|
||||
}
|
||||
@@ -183,20 +153,6 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
this.placeholderValues = placeholderValues;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public boolean getDoCacheClear() {
|
||||
return this.doCacheClear;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setDoCacheClear(boolean doCacheClear) {
|
||||
this.doCacheClear = doCacheClear;
|
||||
}
|
||||
|
||||
public boolean getDisableEeca() {
|
||||
return this.disableEeca;
|
||||
}
|
||||
|
||||
public List<Object> getMessageList() {
|
||||
if (this.checkDataOnly && this.messageList == null) {
|
||||
messageList = new LinkedList<Object>();
|
||||
@@ -204,10 +160,6 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
return this.messageList;
|
||||
}
|
||||
|
||||
public void setMessageList(List<Object> messageList) {
|
||||
this.messageList = messageList;
|
||||
}
|
||||
|
||||
public void setDisableEeca(boolean disableEeca) {
|
||||
this.disableEeca = disableEeca;
|
||||
if (disableEeca) {
|
||||
@@ -262,34 +214,12 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
|
||||
public long parse(InputStream is, String docDescription) throws SAXException, java.io.IOException {
|
||||
|
||||
/* NOTE: this method is not used because it doesn't work with various parsers...
|
||||
String orgXmlSaxDriver = System.getProperty("org.xml.sax.driver");
|
||||
if (UtilValidate.isEmpty(orgXmlSaxDriver)) orgXmlSaxDriver = "org.apache.xerces.parsers.SAXParser";
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader(orgXmlSaxDriver);
|
||||
*/
|
||||
|
||||
/* This code is for a standard SAXParser and XMLReader like xerces or such; for speed we are using the Javolution reader
|
||||
XMLReader reader = null;
|
||||
|
||||
SAXParser parser;
|
||||
try {
|
||||
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||
SAXParser parser = parserFactory.newSAXParser();
|
||||
|
||||
reader = parser.getXMLReader();
|
||||
} catch (javax.xml.parsers.ParserConfigurationException e) {
|
||||
Debug.logError(e, "Failed to get a SAX XML parser", module);
|
||||
throw new IllegalStateException("Failed to get a SAX XML parser");
|
||||
parser = SAXParserFactory.newInstance().newSAXParser();
|
||||
} catch(ParserConfigurationException pce) {
|
||||
throw new SAXException("Unable to create the SAX parser", pce);
|
||||
}
|
||||
*/
|
||||
|
||||
XMLReaderImpl parser = new XMLReaderImpl();
|
||||
|
||||
parser.setContentHandler(this);
|
||||
parser.setErrorHandler(this);
|
||||
// LocalResolver lr = new UtilXml.LocalResolver(new DefaultHandler());
|
||||
// reader.setEntityResolver(lr);
|
||||
|
||||
numberRead = 0;
|
||||
try {
|
||||
boolean beganTransaction = false;
|
||||
@@ -298,7 +228,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
Debug.logImportant("Transaction Timeout set to " + transactionTimeout / 3600 + " hours (" + transactionTimeout + " seconds)", module);
|
||||
}
|
||||
try {
|
||||
parser.parse(is);
|
||||
parser.parse(is, this);
|
||||
// make sure all of the values to write got written...
|
||||
if (! valuesToWrite.isEmpty()) {
|
||||
writeValues(valuesToWrite);
|
||||
@@ -335,6 +265,16 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
}
|
||||
|
||||
private void countValue(boolean skip, boolean exist) {
|
||||
if (skip) numberSkipped++;
|
||||
else if (Action.DELETE == currentAction) numberDeleted++;
|
||||
else if (Action.CREATE == currentAction || ! exist) numberCreated++;
|
||||
else if (Action.CREATE_REPLACE == currentAction) numberReplaced++;
|
||||
else numberUpdated++;
|
||||
}
|
||||
|
||||
// ======== ContentHandler interface implementation ========
|
||||
|
||||
public void characters(char[] values, int offset, int count) throws org.xml.sax.SAXException {
|
||||
if (isParseForTemplate) {
|
||||
// if null, don't worry about it
|
||||
@@ -346,22 +286,22 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
|
||||
if (currentValue != null && currentFieldName != null) {
|
||||
Text value = Text.valueOf(values, offset, count);
|
||||
|
||||
// Debug.logInfo("characters: value=" + value, module);
|
||||
char[] newChunk = Arrays.copyOfRange(values, offset, offset + count);
|
||||
if (currentFieldValue == null) {
|
||||
currentFieldValue = value;
|
||||
// this is the first chunk
|
||||
currentFieldValue = newChunk;
|
||||
} else {
|
||||
currentFieldValue = Text.valueOf(currentFieldValue).concat(value);
|
||||
// append the new chunk to currentFieldValue
|
||||
char[] combined = new char[currentFieldValue.length + newChunk.length];
|
||||
System.arraycopy(currentFieldValue, 0, combined, 0, currentFieldValue.length);
|
||||
System.arraycopy(newChunk, 0, combined, currentFieldValue.length, newChunk.length);
|
||||
currentFieldValue = combined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void endDocument() throws org.xml.sax.SAXException {}
|
||||
|
||||
public void endElement(CharArray namespaceURI, CharArray localName, CharArray fullName) throws org.xml.sax.SAXException {
|
||||
if (Debug.verboseOn()) Debug.logVerbose("endElement: localName=" + localName + ", fullName=" + fullName + ", numberRead=" + numberRead, module);
|
||||
String fullNameString = fullName.toString();
|
||||
public void endElement(String namespaceURI, String localName, String fullNameString) throws org.xml.sax.SAXException {
|
||||
if (Debug.verboseOn()) Debug.logVerbose("endElement: localName=" + localName + ", fullName=" + fullNameString + ", numberRead=" + numberRead, module);
|
||||
if ("entity-engine-xml".equals(fullNameString)) {
|
||||
return;
|
||||
}
|
||||
@@ -435,13 +375,10 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
ModelField modelField = modelEntity.getField(currentFieldName.toString());
|
||||
String type = modelField.getType();
|
||||
if (type != null && type.equals("blob")) {
|
||||
byte strData[] = new byte[currentFieldValue.length()];
|
||||
strData = currentFieldValue.toString().getBytes();
|
||||
byte binData[] = new byte[currentFieldValue.length()];
|
||||
binData = Base64.base64Decode(strData);
|
||||
byte[] binData = Base64.base64Decode((new String(currentFieldValue)).getBytes());
|
||||
currentValue.setBytes(currentFieldName.toString(), binData);
|
||||
} else {
|
||||
currentValue.setString(currentFieldName.toString(), currentFieldValue.toString());
|
||||
currentValue.setString(currentFieldName.toString(), new String(currentFieldValue));
|
||||
}
|
||||
} else {
|
||||
Debug.logWarning("Ignoring invalid field name [" + currentFieldName + "] found for the entity: " + currentValue.getEntityName() + " with value=" + currentFieldValue, module);
|
||||
@@ -465,7 +402,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
boolean exist = true;
|
||||
boolean skip = false;
|
||||
//if verbose on, check if entity exist on database for count each action
|
||||
//It's necessay to check also for specific action CREATE and DELETE to ensure it's ok
|
||||
//It's necessary to check also for specific action CREATE and DELETE to ensure it's ok
|
||||
if (Action.CREATE == currentAction || Action.DELETE == currentAction || Debug.verboseOn()) {
|
||||
GenericHelper helper = delegator.getEntityHelper(currentValue.getEntityName());
|
||||
if (currentValue.containsPrimaryKey()) {
|
||||
@@ -527,35 +464,12 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
}
|
||||
|
||||
//Use for detail the loading entities
|
||||
protected void countValue(boolean skip, boolean exist) {
|
||||
if (skip) numberSkipped++;
|
||||
else if (Action.DELETE == currentAction) numberDeleted++;
|
||||
else if (Action.CREATE == currentAction || ! exist) numberCreated++;
|
||||
else if (Action.CREATE_REPLACE == currentAction) numberReplaced++;
|
||||
else numberUpdated++;
|
||||
}
|
||||
|
||||
public void endPrefixMapping(CharArray prefix) throws org.xml.sax.SAXException {}
|
||||
|
||||
public void ignorableWhitespace(char[] values, int offset, int count) throws org.xml.sax.SAXException {
|
||||
// String value = new String(values, offset, count);
|
||||
// Debug.logInfo("ignorableWhitespace: value=" + value, module);
|
||||
}
|
||||
|
||||
public void processingInstruction(CharArray target, CharArray instruction) throws org.xml.sax.SAXException {}
|
||||
|
||||
public void setDocumentLocator(org.xml.sax.Locator locator) {
|
||||
this.locator = locator;
|
||||
}
|
||||
|
||||
public void skippedEntity(CharArray name) throws org.xml.sax.SAXException {}
|
||||
|
||||
public void startDocument() throws org.xml.sax.SAXException {}
|
||||
|
||||
public void startElement(CharArray namepsaceURI, CharArray localName, CharArray fullName, Attributes attributes) throws org.xml.sax.SAXException {
|
||||
if (Debug.verboseOn()) Debug.logVerbose("startElement: localName=" + localName + ", fullName=" + fullName + ", attributes=" + attributes, module);
|
||||
String fullNameString = fullName.toString();
|
||||
public void startElement(String namepsaceURI, String localName, String fullNameString, Attributes attributes) throws org.xml.sax.SAXException {
|
||||
if (Debug.verboseOn()) Debug.logVerbose("startElement: localName=" + localName + ", fullName=" + fullNameString + ", attributes=" + attributes, module);
|
||||
if ("entity-engine-xml".equals(fullNameString)) {
|
||||
// check the maintain-timestamp flag
|
||||
CharSequence maintainTx = attributes.getValue("maintain-timestamps");
|
||||
@@ -563,13 +477,6 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
this.setMaintainTxStamps("true".equalsIgnoreCase(maintainTx.toString()));
|
||||
}
|
||||
|
||||
// check the do-cache-clear flag
|
||||
@Deprecated
|
||||
CharSequence doCacheClear = attributes.getValue("do-cache-clear");
|
||||
if (doCacheClear != null) {
|
||||
this.setDoCacheClear("true".equalsIgnoreCase(doCacheClear.toString()));
|
||||
}
|
||||
|
||||
// check the disable-eeca flag
|
||||
CharSequence ecaDisable = attributes.getValue("disable-eeca");
|
||||
if (ecaDisable != null) {
|
||||
@@ -626,7 +533,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
|
||||
if (currentValue != null) {
|
||||
// we have a nested value/CDATA element
|
||||
currentFieldName = fullName;
|
||||
currentFieldName = fullNameString;
|
||||
} else {
|
||||
String entityName = fullNameString;
|
||||
|
||||
@@ -694,10 +601,7 @@ public class EntitySaxReader implements javolution.xml.sax.ContentHandler, Error
|
||||
}
|
||||
}
|
||||
|
||||
//public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException {}
|
||||
public void startPrefixMapping(CharArray arg0, CharArray arg1) throws SAXException {}
|
||||
|
||||
// ======== ErrorHandler interface implementations ========
|
||||
// ======== ErrorHandler interface implementation ========
|
||||
|
||||
public void error(org.xml.sax.SAXParseException exception) throws org.xml.sax.SAXException {
|
||||
Debug.logWarning(exception, "Error reading XML on line " + exception.getLineNumber() + ", column " + exception.getColumnNumber(), module);
|
||||
|
||||
Reference in New Issue
Block a user