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.
/*
* ====================================================================
* Project: openMDX/Core, http://www.openmdx.org/
* Description: Import Handler
* Owner: OMEX AG, Switzerland, http://www.omex.ch
* ====================================================================
*
* This software is published under the BSD license as listed below.
*
* Copyright (c) 2004-2014, OMEX AG, Switzerland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the openMDX team nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ------------------
*
* This product includes software developed by other organizations as
* listed in the NOTICE file.
*/
package org.openmdx.application.xml.spi;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.logging.Level;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilderFactory;
import org.openmdx.base.exception.ServiceException;
import org.openmdx.base.mof.cci.Multiplicity;
import org.openmdx.base.naming.Path;
import org.openmdx.base.rest.cci.ObjectRecord;
import org.openmdx.base.rest.spi.Facades;
import org.openmdx.base.rest.spi.Object_2Facade;
import org.openmdx.base.text.conversion.Base64;
import org.openmdx.kernel.exception.BasicException;
import org.openmdx.kernel.exception.Throwables;
import org.openmdx.kernel.loading.Resources;
import org.openmdx.kernel.log.SysLog;
import org.w3c.spi2.Datatypes;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Import Handler
*/
public class ImportHandler extends DefaultHandler {
/**
* Constructor
*
* @param target
* @param documentURL
* @param defaultImportMode
*/
public ImportHandler(
ImportTarget target,
InputSource source,
ImportMode defaultImportMode
) {
this.target = target;
this.url = getDocumentURL(source);
this.binary = isBinary(source);
this.defaultImportMode = defaultImportMode;
}
/**
* Be aware, SimpleDateFormat instances are not thread safe.
*/
private final SimpleDateFormat localSecondFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/**
* Be aware, SimpleDateFormat instances are not thread safe.
*/
private final SimpleDateFormat localMillisecondFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
// stack of objects currently open
private Stack objectStack = null;
// Path length of the request collection's first element
private int pendingAt = -1;
/**
* set of XML elements which denote object elements, i.e. elements with a
* qualifier This set is required to determine on endElement operations
* whether an attribute/ struct/reference is closed or an object.
*/
private Map> objectElements = null;
// state/context of current object
private ObjectRecord currentObject = null;
/**
* The current object's operation
*/
private ImportMode currentObjectOperation;
private Path currentPath = null;
private StringBuilder currentAttributeValue = null;
private String currentAttributeName = null;
private String currentLocalpartObject = null;
private int nextTemporaryId = -1;
private int currentAttributeOffset = 0;
private int currentAttributePosition = -1;
private String currentAttributeKey = null;
private Multiplicity currentAttributeMultiplicity = null;
private String currentAttributeOperation = null;
// true when the last call was endElement(). Is set to false by
// startElement()
private boolean previousElementEnded = true;
private final ImportTarget target;
private final URL url;
private final ImportMode defaultImportMode;
/**
* true in case of a WBXML input
*/
private final boolean binary;
// cached type definitions (global in classloader)
private static final Set loadedSchemas = new HashSet();
// attributes types as (, attributeTypes = new HashMap();
// attribute multiplicities
private static final Map attributeMultiplicities = new HashMap();
// qualifier names of a complex schema type.
private static final Map qualifierNames = new HashMap();
private static final List cachedParentPaths = new ArrayList();
/**
* Suffix marking date/time values as UTC based
*/
private static String[] UTC_IDS = {
"Z", // canonical form
"+00", "-00",
"+00:00", "-00:00"
};
private static final String LEGACY_RESOURCE_RPEFIX = "xri:+resource/";
private static final String STANDARD_RESOURCE_PREFIX = "xri://+resource/";
private static final String FALLBACK_ATTRIBUTE_TYPE = "org.w3c.string";
/**
* Determine the document URL
*
* @param source
* the InputSource
*
* @return the document URL
*/
private static URL getDocumentURL(
InputSource source
) {
String uri = source.getSystemId();
if (uri == null) {
return null;
} else {
try {
return new URL(uri);
} catch (MalformedURLException exception) {
try {
return new File(uri).toURI().toURL();
} catch (MalformedURLException exception1) {
return null;
}
}
}
}
/**
* Determine the document is binary
*
* @param source
* the InputSource
*
* @return true in case of WBXML input
*/
private static boolean isBinary(
InputSource source
) {
if (source.getCharacterStream() != null) {
return false;
}
String uri = source.getSystemId();
if (uri != null) {
uri = uri.toLowerCase();
if (uri.endsWith(".wbxml")) {
return true;
}
if (uri.endsWith(".xml")) {
return false;
}
}
String encoding = source.getEncoding();
return encoding == null;
}
/**
* Tells whether we are reading an XML or a WBXML source
*
* @return true in case of a WBXML source
*/
public boolean isBinary() {
return this.binary;
}
// -----------------------------------------------------------------------
// Implements EntityResolver
// -----------------------------------------------------------------------
/*
* (non-Javadoc)
*
* @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
*/
@Override
public InputSource resolveEntity(
String publicId,
String systemId
)
throws SAXException {
return this.getSchemaInputSource(systemId);
}
/**
* Initialize qualifierNames, attributeTypes, attributeMultiplicities
*
* @param schemaDocument
*
* @throws ServiceException
*/
private void initTypes(
org.w3c.dom.Document schemaDocument
)
throws ServiceException {
org.w3c.dom.Element docElement = schemaDocument.getDocumentElement();
org.w3c.dom.NodeList complexTypeNodes = docElement.getElementsByTagName("xsd:complexType");
// iterate all xsd:complexType
int complexTypeNodesLength = complexTypeNodes.getLength();
for (int i = 0; i < complexTypeNodesLength; i++) {
org.w3c.dom.Node complexType = complexTypeNodes.item(i);
org.w3c.dom.NamedNodeMap complexTypeAttributes = complexType.getAttributes();
org.w3c.dom.Attr complexTypeName = (org.w3c.dom.Attr) complexTypeAttributes.getNamedItem("name");
if (complexTypeName != null) {
// get qualifierName of complex type
org.w3c.dom.NodeList attributeNodes = ((org.w3c.dom.Element) complexType).getElementsByTagName("xsd:attribute");
int attributeNodesLength = attributeNodes.getLength();
for (int j = 0; j < attributeNodesLength; j++) {
org.w3c.dom.Node attributeNode = attributeNodes.item(j);
if ("_qualifier".equals(attributeNode.getAttributes().getNamedItem("name").getNodeValue())) {
ImportHandler.qualifierNames.put(
complexTypeName
.getNodeValue(), attributeNode
.getAttributes()
.getNamedItem("fixed")
.getNodeValue()
);
}
}
// get qualifierTypes, qualifierMultiplicities
org.w3c.dom.NodeList elementNodes = ((org.w3c.dom.Element) complexType).getElementsByTagName("xsd:element");
int elementNodesLength = elementNodes.getLength();
for (int j = 0; j < elementNodesLength; j++) {
org.w3c.dom.Node element = elementNodes.item(j);
org.w3c.dom.NamedNodeMap elementAttributes = element.getAttributes();
org.w3c.dom.Attr attributeName = (org.w3c.dom.Attr) elementAttributes.getNamedItem("name");
org.w3c.dom.Attr attributeType = (org.w3c.dom.Attr) elementAttributes.getNamedItem("type");
// default multiplicity Multiplicities.SINGLE_VALUE
if (attributeName != null &&
!"_content".equals(attributeName.getNodeValue()) &&
!"_object".equals(attributeName.getNodeValue()) &&
!"_item".equals(attributeName.getNodeValue()) &&
attributeName.getNodeValue().indexOf('.') < 0) {
String attributeTypeName = null;
// multi-valued attribute defined as complexType
if (attributeType == null) {
// find xsd:attribute name=""_multiplicity"" of
// complexType
org.w3c.dom.NodeList l0 = ((org.w3c.dom.Element) element).getElementsByTagName("xsd:complexType");
for (int i0 = 0; i0 < l0.getLength(); i0++) {
org.w3c.dom.Node n0 = l0.item(i0);
org.w3c.dom.NodeList l1 = ((org.w3c.dom.Element) n0).getElementsByTagName("xsd:attribute");
for (int i1 = 0; i1 < l1.getLength(); i1++) {
org.w3c.dom.Node n1 = l1.item(i1);
if ("_multiplicity".equals(n1.getAttributes().getNamedItem("name").getNodeValue())) {
ImportHandler.attributeMultiplicities.put(
complexTypeName.getNodeValue() + ":" + attributeName.getNodeValue(),
n1.getAttributes().getNamedItem("fixed").getNodeValue()
);
}
}
}
// xsd:extension base=
org.w3c.dom.Node extension = element;
while (extension != null &&
!"xsd:extension".equals(extension.getNodeName())) {
org.w3c.dom.NodeList children = extension.getChildNodes();
extension = null;
for (int k = 0; k < children.getLength(); k++) {
if (children.item(k).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
extension = children.item(k);
break;
}
}
}
if (extension != null) {
org.w3c.dom.NamedNodeMap extensionAttributes = extension.getAttributes();
org.w3c.dom.Attr extensionBase = (org.w3c.dom.Attr) extensionAttributes.getNamedItem("base");
if (extensionBase != null) {
attributeTypeName = extensionBase.getValue();
}
}
} else {
//
// A simple type is defined as:
//
//