org.glassfish.hk2.xml.internal.XmlStreamImpl Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016-2017 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.hk2.xml.internal;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Unmarshaller.Listener;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
import org.glassfish.hk2.utilities.general.IndentingXMLStreamWriter;
import org.glassfish.hk2.utilities.reflection.ClassReflectionHelper;
import org.glassfish.hk2.utilities.reflection.Logger;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
import org.glassfish.hk2.xml.api.XmlHk2ConfigurationBean;
import org.glassfish.hk2.xml.api.XmlRootHandle;
import org.glassfish.hk2.xml.api.XmlService;
import org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean;
import org.glassfish.hk2.xml.spi.Model;
/**
* @author jwells
*
*/
public class XmlStreamImpl {
private final static boolean DEBUG_PARSING = XmlServiceImpl.DEBUG_PARSING;
private final static QName ANY_ATTRIBUTE_QNAME = QNameUtilities.createQName(XmlService.DEFAULT_NAMESPACE, GeneratorUtilities.XML_ANY_ATTRIBUTE_LOCAL_PART);
@SuppressWarnings("unchecked")
public static T parseRoot(XmlServiceImpl xmlService,
Model rootModel,
XMLStreamReader reader,
Unmarshaller.Listener listener) throws Exception {
Class> rootProxyClass = rootModel.getProxyAsClass();
ClassReflectionHelper classReflectionHelper = xmlService.getClassReflectionHelper();
BaseHK2JAXBBean hk2Root = Utilities.createBean(rootProxyClass);
hk2Root._setClassReflectionHelper(classReflectionHelper);
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug Created root bean with model " + hk2Root._getModel());
}
Map referenceMap = new HashMap();
List unresolved = new LinkedList();
while(reader.hasNext()) {
int event = reader.next();
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug got xml event (A) " + eventToString(event));
}
switch(event) {
case XMLStreamConstants.START_ELEMENT:
HashMap rootNamespace = new HashMap();
String defaultNamespace = null;
int namespaceCount = reader.getNamespaceCount();
for (int nLcv = 0; nLcv < namespaceCount; nLcv++) {
String namespacePrefix = reader.getNamespacePrefix(nLcv);
String namespaceURI = reader.getNamespaceURI(nLcv);
if (namespacePrefix == null) {
defaultNamespace = namespaceURI;
}
else {
rootNamespace.put(namespacePrefix, namespaceURI);
}
}
String elementTagNamespace = QNameUtilities.getNamespace(reader.getName(), defaultNamespace);
String elementTag = reader.getName().getLocalPart();
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug starting document tag " + elementTag);
}
handleElement(hk2Root,
null,
reader,
classReflectionHelper,
listener,
referenceMap,
unresolved,
elementTagNamespace,
elementTag,
rootNamespace,
defaultNamespace);
break;
case XMLStreamConstants.END_DOCUMENT:
// Resolve any forward references
Utilities.fillInUnfinishedReferences(referenceMap, unresolved);
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug finished reading document");
}
return (T) hk2Root;
default:
// Do nothing
}
}
throw new IllegalStateException("Unexpected end of XMLReaderStream");
}
private static void handleElement(BaseHK2JAXBBean target,
BaseHK2JAXBBean parent,
XMLStreamReader reader,
ClassReflectionHelper classReflectionHelper,
Listener listener,
Map referenceMap,
List unresolved,
String outerElementNamespace,
String outerElementTag,
Map namespaceMap,
String defaultNamespace) throws Exception {
listener.beforeUnmarshal(target, parent);
Map> listChildren = new HashMap>();
Map> arrayChildren = new HashMap>();
Map> listNonChild = new HashMap>();
Map arrayNonChild = new HashMap();
ModelImpl targetModel = target._getModel();
Map nonChildProperties = targetModel.getNonChildProperties();
Map childProperties = targetModel.getChildrenByName();
Set allWrappers = targetModel.getAllXmlWrappers();
Map xmlAnyAttributeData = new LinkedHashMap();
int numAttributes = reader.getAttributeCount();
for (int lcv = 0; lcv < numAttributes; lcv++) {
String attributeNamespace = QNameUtilities.fixNamespace(reader.getAttributeNamespace(lcv));
String attributeName = reader.getAttributeLocalName(lcv);
String attributeValue = reader.getAttributeValue(lcv);
QName attributeQName = QNameUtilities.createQName(attributeNamespace, attributeName, defaultNamespace);
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug handling attribute " + attributeQName + " with value " + attributeValue);
}
ChildDataModel childDataModel = nonChildProperties.get(attributeQName);
if (childDataModel == null) {
xmlAnyAttributeData.put(attributeQName, attributeValue);
continue;
}
if (!Format.ATTRIBUTE.equals(childDataModel.getFormat())) continue;
Class> childType = targetModel.getNonChildType(attributeNamespace, attributeName);
if (!childDataModel.isReference()) {
Object convertedValue = Utilities.getDefaultValue(attributeValue, childType, namespaceMap);
target._setProperty(attributeNamespace, attributeName, convertedValue);
}
else {
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug attribute " + attributeName + " is a reference");
}
// Reference
ReferenceKey rk = new ReferenceKey(childDataModel.getChildType(), attributeValue);
BaseHK2JAXBBean reference = referenceMap.get(rk);
if (reference != null) {
target._setProperty(attributeNamespace, attributeName, reference);
}
else {
unresolved.add(new UnresolvedReference(childDataModel.getChildType(), attributeValue, attributeNamespace, attributeName, target));
}
}
}
ChildDataModel childDataModel = nonChildProperties.get(ANY_ATTRIBUTE_QNAME);
if (childDataModel != null) {
target._setProperty(ANY_ATTRIBUTE_QNAME, xmlAnyAttributeData);
}
while(reader.hasNext()) {
int event = reader.next();
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug got xml event (B) " + eventToString(event));
}
switch(event) {
case XMLStreamConstants.START_ELEMENT:
Map effectiveNamespaceMap = null;
{
effectiveNamespaceMap = new HashMap(namespaceMap);
int namespaceCount = reader.getNamespaceCount();
for (int nLcv = 0; nLcv < namespaceCount; nLcv++) {
String namespacePrefix = reader.getNamespacePrefix(nLcv);
String namespaceURI = reader.getNamespaceURI(nLcv);
if (namespacePrefix == null) {
defaultNamespace = namespaceURI;
}
else {
effectiveNamespaceMap.put(reader.getNamespacePrefix(nLcv), reader.getNamespaceURI(nLcv));
}
}
}
String elementTagNamespace = QNameUtilities.getNamespace(reader.getName(), defaultNamespace);
String elementTag = reader.getName().getLocalPart();
QName elementTagQName = QNameUtilities.createQName(elementTagNamespace, elementTag, defaultNamespace);
if (DEBUG_PARSING) {
Logger.getLogger().debug("XmlServiceDebug starting parse of element " + elementTag);
}
ChildDataModel cdm = nonChildProperties.get(elementTagQName);
if (cdm != null && Format.ELEMENT.equals(cdm.getFormat())) {
String elementValue = advanceNonChildElement(reader, elementTag);
Class> childType = cdm.getChildTypeAsClass();
if (!cdm.isReference()) {
if (List.class.equals(childType)) {
Class> listType = cdm.getChildListTypeAsClass();
Object convertedValue = Utilities.getDefaultValue(elementValue, listType, effectiveNamespaceMap);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy