Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* Copyright 1999 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.util;
import java.util.*;
import java.io.*;
import java.net.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import com.viaoa.ds.OASelect;
import com.viaoa.hub.Hub;
import com.viaoa.object.*;
// 20150906 created to be correct xml, removing OA specific format and tags. old version renamed to OAXMLReader1
/**
OAXMLReader using a SAXParser to parse and automatically create OAObjects from an XML file.
This will do the following to find the existing object:
1: if OAProperty.importMatch, then it will search to find a matching object
2: if objectId props, then it will search to find a matching object
3: use guid
if not found, then a new object will be created.
see: 20200127 OAJaxb.java
@see OAXMLWriter
*/
public class OAXMLReader {
private String fileName;
protected String value;
protected int indent;
protected int total;
protected boolean bWithinTag;
protected Object[] stack = new Object[10];
private String decodeMessage;
private static final String XML_ID = "XML_ID";
private static final String XML_IDREF = "XML_IDREF";
private static final String XML_VALUE = "XML_VALUE";
private static final String XML_CLASS = "XML_CLASS";
private static final String XML_OBJECT = "XML_OBJECT";
private static final String XML_HUB = "XML_HUB";
protected Class conversionClass; // type of class that value needs to be converted to
protected HashMap hashGuid;
private OAXMLReader1 xmlReader1;
private boolean bImportMatching = true;
private MyDefaultHandler myDefaultHandler;
// flag to know if OAXMLWriter wrote the object, which adds an additonal tag for the start of each object.
private int versionOAXML;
public OAXMLReader() {
}
/**
* Read the xml data from a file and load into objects.
*/
public Object[] readFile(String fileName) throws Exception {
parseFile(fileName);
ArrayList al = process();
Object[] objs = new Object[al.size()];
al.toArray(objs);
return objs;
}
public Object[] read(File file) throws Exception {
return readFile(file.getPath());
}
/**
* Read the xml data and load into objects.
*/
public Object[] readXML(String xmlText) throws Exception {
parseString(xmlText);
ArrayList al = process();
Object[] objs = new Object[al.size()];
al.toArray(objs);
return objs;
}
public void setImportMatching(boolean b) {
this.bImportMatching = b;
if (xmlReader1 != null) xmlReader1.setImportMatching(b);
}
public boolean getImportMatching() {
return this.bImportMatching;
}
/**
Used to unencrypt an XML file created by OAXMLWriter that used an encryption code.
@see OAXMLWriter#setEncodeMessage(String)
*/
public void setDecodeMessage(String msg) {
if (msg != null && msg.length() == 0) throw new IllegalArgumentException("DecodeMessage cant be an empty string");
decodeMessage = msg;
if (xmlReader1 != null) xmlReader1.setDecodeMessage(msg);
}
public String getDecodeMessage() {
return decodeMessage;
}
protected void reset() {
indent = 0;
total = 0;
bWithinTag = false;
hashGuid = new HashMap();
versionOAXML = 0;
xmlReader1 = null;
}
// Used to parse and create OAObjects from an XML file.
protected void parseFile(String fileName) throws Exception {
if (fileName == null) throw new IllegalArgumentException("fileName is required");
reset();
URI uri = null;
File f = new File(OAString.convertFileName(fileName));
if (f.exists()) uri = f.toURI();
else uri = new URI(fileName);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( uri.toString(), this.getDefaultHandler());
Object[] objs = new Object[indent + 1];
System.arraycopy(stack, 0, objs, 0, indent+1);
stack = objs;
}
// Used to parse and create OAObjects from an XML string.
protected void parseString(String xmlData) throws Exception {
if (xmlData == null) throw new IllegalArgumentException("xmlData is required");
reset();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new StringBufferInputStream(xmlData), this.getDefaultHandler());
}
protected ArrayList process() throws Exception {
if (xmlReader1 != null) {
ArrayList al = new ArrayList();
for (Object objx : xmlReader1.getRootObjects()) {
if (objx instanceof Hub) {
for (Object obj : ((Hub)objx)) {
al.add((OAObject) obj);
}
break;
}
al.add((OAObject) objx);
}
return al;
}
ArrayList