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.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.polygene.serialization.javaxxml;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.Base64;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.polygene.api.PolygeneAPI;
import org.apache.polygene.api.association.AssociationStateHolder;
import org.apache.polygene.api.common.Optional;
import org.apache.polygene.api.composite.Composite;
import org.apache.polygene.api.composite.CompositeInstance;
import org.apache.polygene.api.composite.StatefulAssociationCompositeDescriptor;
import org.apache.polygene.api.entity.EntityReference;
import org.apache.polygene.api.injection.scope.This;
import org.apache.polygene.api.injection.scope.Uses;
import org.apache.polygene.api.mixin.Initializable;
import org.apache.polygene.api.serialization.Converter;
import org.apache.polygene.api.serialization.Converters;
import org.apache.polygene.api.serialization.SerializationException;
import org.apache.polygene.api.service.ServiceDescriptor;
import org.apache.polygene.api.type.ArrayType;
import org.apache.polygene.api.type.EnumType;
import org.apache.polygene.api.type.MapType;
import org.apache.polygene.api.type.StatefulAssociationValueType;
import org.apache.polygene.spi.serialization.AbstractTextSerializer;
import org.apache.polygene.spi.serialization.XmlSerializer;
import org.apache.polygene.spi.util.ArrayIterable;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.polygene.api.util.Collectors.toMap;
/**
* XML Serializer.
*/
public class JavaxXmlSerializer extends AbstractTextSerializer
implements XmlSerializer, Initializable
{
private static final String NULL_ELEMENT_NAME = "null";
@This
private JavaxXmlFactories xmlFactories;
@This
private Converters converters;
@This
private JavaxXmlAdapters adapters;
@Uses
private ServiceDescriptor descriptor;
private JavaxXmlSettings settings;
@Override
public void initialize() throws Exception
{
settings = JavaxXmlSettings.orDefault( descriptor.metaInfo( JavaxXmlSettings.class ) );
}
@Override
public void serialize( Options options, Writer writer, @Optional Object object )
{
Document xmlDocument = toXml( options, object );
if( xmlDocument == null )
{
return;
}
try
{
// We want plain text nodes to be serialized without surrounding elements
if( xmlDocument.getNodeType() == Node.TEXT_NODE )
{
writer.write( xmlDocument.getNodeValue() );
}
else
{
xmlFactories.serializationTransformer().transform( new DOMSource( xmlDocument ),
new StreamResult( writer ) );
}
}
catch( IOException ex )
{
throw new UncheckedIOException( ex );
}
catch( TransformerException ex )
{
throw new SerializationException( "Unable to transform XML Document to String", ex );
}
}
@Override
public Function toXmlFunction( Options options )
{
return object -> doSerializeRoot( options, object );
}
private Document doSerializeRoot( Options options, T object )
{
Document doc = xmlFactories.newDocumentForSerialization();
Element stateElement = doc.createElement( settings.getRootTagName() );
Node node = doSerialize( doc, options, object, true );
stateElement.appendChild( node );
doc.appendChild( stateElement );
return doc;
}
private Node doSerialize( Document document, Options options, T object, boolean root )
{
if( object == null )
{
return document.createElement( NULL_ELEMENT_NAME );
}
Class> objectClass = object.getClass();
Converter