All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.xml.core.Support Maven / Gradle / Ivy

Go to download

Simple is a high performance XML serialization and configuration framework for Java

The newest version!
/*
 * Support.java May 2006
 *
 * Copyright (C) 2006, Niall Gallagher 
 *
 * 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 org.simpleframework.xml.core;

import static org.simpleframework.xml.DefaultType.FIELD;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.simpleframework.xml.DefaultType;
import org.simpleframework.xml.filter.Filter;
import org.simpleframework.xml.filter.PlatformFilter;
import org.simpleframework.xml.strategy.Value;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.stream.Style;
import org.simpleframework.xml.transform.Matcher;
import org.simpleframework.xml.transform.Transform;
import org.simpleframework.xml.transform.Transformer;

/**
 * The Support object is used to provide support to the
 * serialization engine for processing and transforming strings. This
 * contains a Transformer which will create objects from
 * strings and will also reverse this process converting an object
 * to a string. This is used in the conversion of primitive types.
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.xml.transform.Transformer
 */
class Support implements Filter {
   
   /**
    * This is the factory that is used to create the scanners.
    */
   private final InstanceFactory instances;
   
   /**
    * This will perform the scanning of types are provide scanners.
    */
   private final ScannerFactory scanners;
   
   /**
    * This is used to extract the defaults for a specific class.
    */
   private final DetailExtractor defaults;
   
   /**
    * This is used to extract the details for a specific class.
    */
   private final DetailExtractor details;
   
   /**
    * This is used to extract the labels for a specific contact.
    */
   private final LabelExtractor labels;
   
   /**
    * This is the transformer used to transform objects to text.
    */
   private final Transformer transform;
   
   /**
    * This is the matcher used to acquire the transform objects.
    */
   private final Matcher matcher;
   
   /**
    * This is the filter used to transform the template variables.
    */
   private final Filter filter;
   
   /**
    * This is the format used by this persistence support object.
    */
   private final Format format;
   
   /**
    * Constructor for the Support object. This will
    * create a support object with a default matcher and default
    * platform filter. This ensures it contains enough information
    * to process a template and transform basic primitive types.
    */
   public Support() {
      this(new PlatformFilter());
   }

   /**
    * Constructor for the Support object. This will
    * create a support object with a default matcher and the filter
    * provided. This ensures it contains enough information to 
    * process a template and transform basic primitive types.
    * 
    * @param filter this is the filter to use with this support
    */
   public Support(Filter filter) {
      this(filter, new EmptyMatcher());
   }
   
   /**
    * Constructor for the Support object. This will
    * create a support object with the matcher and filter provided.
    * This allows the user to override the transformations that
    * are used to convert types to strings and back again.
    * 
    * @param filter this is the filter to use with this support
    * @param matcher this is the matcher used for transformations
    */
   public Support(Filter filter, Matcher matcher) {
      this(filter, matcher, new Format());
   }
   
   /**
    * Constructor for the Support object. This will
    * create a support object with the matcher and filter provided.
    * This allows the user to override the transformations that
    * are used to convert types to strings and back again.
    * 
    * @param filter this is the filter to use with this support
    * @param matcher this is the matcher used for transformations
    * @param format this contains all the formatting for the XML
    */
   public Support(Filter filter, Matcher matcher, Format format) {
      this.defaults = new DetailExtractor(this, FIELD);
      this.transform = new Transformer(matcher);
      this.scanners = new ScannerFactory(this);
      this.details = new DetailExtractor(this);
      this.labels = new LabelExtractor(format);
      this.instances = new InstanceFactory();
      this.matcher = matcher;
      this.filter = filter;
      this.format = format;
   }
   
   /**
    * Replaces the text provided with some property. This method 
    * acts much like a the get method of the Map
    * object, in that it uses the provided text as a key to some 
    * value. However it can also be used to evaluate expressions
    * and output the result for inclusion in the generated XML.
    *
    * @param text this is the text value that is to be replaced
    * 
    * @return returns a replacement for the provided text value
    */
   public String replace(String text) {
      return filter.replace(text);
   }
   
   /**
    * This is used to acquire the Style for the format.
    * This requires that the style is not null, if a null style is
    * returned from the format this will break serialization.
    * 
    * @return this returns the style used for this format object
    */
   public Style getStyle() {
      return format.getStyle();
   }
   
   /**
    * This is used to acquire the Format for this.
    * The format should never be null and contains information that
    * relates to the indentation that is to be used with XML elements.
    * 
    * @return this returns the format to be used for serialization
    */
   public Format getFormat() {
      return format;
   }
   
   /**
    * This will create an Instance that can be used
    * to instantiate objects of the specified class. This leverages
    * an internal constructor cache to ensure creation is quicker.
    * 
    * @param value this contains information on the object instance
    * 
    * @return this will return an object for instantiating objects
    */
   public Instance getInstance(Value value) {
      return instances.getInstance(value);
   }
   
   /**
    * This will create an Instance that can be used
    * to instantiate objects of the specified class. This leverages
    * an internal constructor cache to ensure creation is quicker.
    * 
    * @param type this is the type that is to be instantiated
    * 
    * @return this will return an object for instantiating objects
    */
   public Instance getInstance(Class type) {
      return instances.getInstance(type);
   }
   
   /**
    * This is used to match a Transform using the type
    * specified. If no transform can be acquired then this returns
    * a null value indicating that no transform could be found.
    * 
    * @param type this is the type to acquire the transform for
    * 
    * @return returns a transform for processing the type given
    */ 
   public Transform getTransform(Class type) throws Exception {
      return matcher.match(type);
   }

   /**
    * Creates a Label using the provided contact and XML
    * annotation. The label produced contains all information related
    * to an object member. It knows the name of the XML entity, as
    * well as whether it is required. Once created the converter can
    * transform an XML node into Java object and vice versa.
    * 
    * @param contact this is contact that the label is produced for
    * @param label represents the XML annotation for the contact
    * 
    * @return returns the label instantiated for the contact
    */
   public Label getLabel(Contact contact, Annotation label) throws Exception {
      return labels.getLabel(contact, label);
   }
   
   /**
    * Creates a List using the provided contact and XML
    * annotation. The labels produced contain all information related
    * to an object member. It knows the name of the XML entity, as
    * well as whether it is required. Once created the converter can
    * transform an XML node into Java object and vice versa.
    * 
    * @param contact this is contact that the label is produced for
    * @param label represents the XML annotation for the contact
    * 
    * @return returns the list of labels associated with the contact
    */
   public List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy