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

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

Go to download

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

The newest version!
/*
 * LabelFactory.java July 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 java.util.Collections.emptyList;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementArray;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementListUnion;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.ElementMapUnion;
import org.simpleframework.xml.ElementUnion;
import org.simpleframework.xml.Text;
import org.simpleframework.xml.Version;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.util.Cache;
import org.simpleframework.xml.util.ConcurrentCache;

/**
 * The LabelExtractor object is used to create instances of
 * the Label object that can be used to convert an XML
 * node into a Java object. Each label created requires the contact it
 * represents and the XML annotation it is marked with.  
 * 

* The Label objects created by this factory a selected * using the XML annotation type. If the annotation type is not known * the factory will throw an exception, otherwise a label instance * is created that will expose the properties of the annotation. * * @author Niall Gallagher */ class LabelExtractor { /** * This is used to cache the list of labels that have been created. */ private final Cache cache; /** * Contains the format that is associated with the serializer. */ private final Format format; /** * Constructor for the LabelExtractor object. This * creates an extractor that will extract labels for a specific * contact. Labels are cached within the extractor so that they * can be looked up without having to rebuild it each time. * * @param format this is the format used by the serializer */ public LabelExtractor(Format format) { this.cache = new ConcurrentCache(); this.format = format; } /** * 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 { Object key = getKey(contact, label); LabelGroup list = getGroup(contact, label, key); if(list != null) { return list.getPrimary(); } return null; } /** * 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