📄 entitysaxreader.java
字号:
/* * $Id: EntitySaxReader.java 6257 2005-12-06 18:04:10Z jaz $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.entity.util;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.StringWriter;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.Map;import freemarker.ext.beans.BeansWrapper;import freemarker.ext.dom.NodeModel;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;import freemarker.template.TemplateHashModel;import javolution.lang.Text;import javolution.util.FastMap;import javolution.xml.sax.Attributes;import javolution.xml.sax.RealtimeParser;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.ofbiz.base.util.Base64;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilURL;import org.ofbiz.base.util.UtilXml;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.eca.EntityEcaHandler;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelField;import org.ofbiz.entity.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;/** * SAX XML Parser Content Handler for Entity Engine XML files * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6257 $ * @since 2.0 */public class EntitySaxReader implements javolution.xml.sax.ContentHandler, ErrorHandler { public static final String module = EntitySaxReader.class.getName(); public static final int DEFAULT_TX_TIMEOUT = 7200; protected org.xml.sax.Locator locator; protected GenericDelegator delegator; protected EntityEcaHandler ecaHandler = null; protected GenericValue currentValue = null; protected CharSequence currentFieldName = null; protected CharSequence currentFieldValue = null; protected long numberRead = 0; protected int valuesPerWrite = 100; protected int valuesPerMessage = 1000; protected int transactionTimeout = 7200; protected boolean useTryInsertMethod = false; protected boolean maintainTxStamps = false; protected boolean createDummyFks = false; protected boolean doCacheClear = true; protected boolean disableEeca = false; protected List valuesToWrite = new ArrayList(valuesPerWrite); protected boolean isParseForTemplate = false; protected CharSequence templatePath = null; protected Node rootNodeForTemplate = null; protected Node currentNodeForTemplate = null; protected Document documentForTemplate = null; protected EntitySaxReader() {} public EntitySaxReader(GenericDelegator delegator, int transactionTimeout) { // clone the delegator right off so there is no chance of making change to the initial object this.delegator = delegator.cloneDelegator(); this.transactionTimeout = transactionTimeout; } public EntitySaxReader(GenericDelegator delegator) { 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; } public void setUseTryInsertMethod(boolean value) { this.useTryInsertMethod = value; } public void setTransactionTimeout(int transactionTimeout) throws GenericTransactionException { if (this.transactionTimeout != transactionTimeout) { TransactionUtil.setTransactionTimeout(transactionTimeout); this.transactionTimeout = transactionTimeout; } } 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 getDoCacheClear() { return this.doCacheClear; } public void setDoCacheClear(boolean doCacheClear) { this.doCacheClear = doCacheClear; } public boolean getDisableEeca() { return this.disableEeca; } public void setDisableEeca(boolean disableEeca) { this.disableEeca = disableEeca; if (disableEeca) { if (this.ecaHandler == null) { this.ecaHandler = delegator.getEntityEcaHandler(); } this.delegator.setEntityEcaHandler(null); } else { if (ecaHandler != null) { this.delegator.setEntityEcaHandler(ecaHandler); } } } public long parse(String content) throws SAXException, java.io.IOException { if (content == null) { Debug.logWarning("content was null, doing nothing", module); return 0; } ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes()); return this.parse(bis, "Internal Content"); } public long parse(URL location) throws SAXException, java.io.IOException { if (location == null) { Debug.logWarning("location URL was null, doing nothing", module); return 0; } Debug.logImportant("Beginning import from URL: " + location.toExternalForm(), module); return this.parse(location.openStream(), location.toString()); } 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; 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"); } */ RealtimeParser parser = new RealtimeParser(16384); parser.setContentHandler(this); parser.setErrorHandler(this); // LocalResolver lr = new UtilXml.LocalResolver(new DefaultHandler()); // reader.setEntityResolver(lr); numberRead = 0; try { boolean beganTransaction = false; if (transactionTimeout > -1) { beganTransaction = TransactionUtil.begin(transactionTimeout); Debug.logImportant("Transaction Timeout set to " + transactionTimeout / 3600 + " hours (" + transactionTimeout + " seconds)", module); } try { parser.parse(is); // make sure all of the values to write got written... if (valuesToWrite.size() > 0) { writeValues(valuesToWrite); valuesToWrite.clear(); } TransactionUtil.commit(beganTransaction); } catch (Exception e) { String errMsg = "An error occurred saving the data, rolling back transaction (" + beganTransaction + ")"; Debug.logError(e, errMsg, module); TransactionUtil.rollback(beganTransaction, errMsg, e); throw new SAXException("A transaction error occurred reading data", e); } } catch (GenericTransactionException e) { throw new SAXException("A transaction error occurred reading data", e); } Debug.logImportant("Finished " + numberRead + " values from " + docDescription, module); return numberRead; } protected void writeValues(List valuesToWrite) throws GenericEntityException { delegator.storeAll(valuesToWrite, doCacheClear, createDummyFks); } public void characters(char[] values, int offset, int count) throws org.xml.sax.SAXException { if (isParseForTemplate) { // if null, don't worry about it if (this.currentNodeForTemplate != null) { Node newNode = this.documentForTemplate.createTextNode(new String(values, offset, count)); this.currentNodeForTemplate.appendChild(newNode); } return; } if (currentValue != null && currentFieldName != null) { Text value = Text.valueOf(values, offset, count); // Debug.logInfo("characters: value=" + value, module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -