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

org.apache.juneau.xml.XmlClassMeta Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * 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.juneau.xml;

import static org.apache.juneau.internal.StringUtils.*;
import static org.apache.juneau.internal.ReflectionUtils.*;


import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.xml.annotation.*;

/**
 * Metadata on classes specific to the XML serializers and parsers pulled from the {@link Xml @Xml} annotation on the
 * class.
 */
public class XmlClassMeta extends ClassMetaExtended {

	private final Namespace namespace;
	private final Xml xml;
	private final XmlFormat format;
	private final String childName;

	/**
	 * Constructor.
	 *
	 * @param cm The class that this annotation is defined on.
	 */
	public XmlClassMeta(ClassMeta cm) {
		super(cm);
		Class c = getInnerClass();
		this.namespace = findNamespace(c);
		this.xml = ReflectionUtils.getAnnotation(Xml.class, c);
		if (xml != null) {
			this.format = xml.format();
			this.childName = nullIfEmpty(xml.childName());

		} else {
			this.format = XmlFormat.DEFAULT;
			this.childName = null;
		}
	}

	/**
	 * Returns the {@link Xml} annotation defined on the class.
	 *
	 * @return
	 * 	The value of the {@link Xml} annotation defined on the class, or null if annotation is not specified.
	 */
	protected Xml getAnnotation() {
		return xml;
	}

	/**
	 * Returns the {@link Xml#format()} annotation defined on the class.
	 *
	 * @return The value of the {@link Xml#format()} annotation, or {@link XmlFormat#DEFAULT} if not specified.
	 */
	protected XmlFormat getFormat() {
		return format;
	}

	/**
	 * Returns the {@link Xml#childName()} annotation defined on the class.
	 *
	 * @return The value of the {@link Xml#childName()} annotation, or null if not specified.
	 */
	protected String getChildName() {
		return childName;
	}

	/**
	 * Returns the XML namespace associated with this class.
	 *
	 * 

* Namespace is determined in the following order: *

    *
  1. {@link Xml#prefix()} annotation defined on class. *
  2. {@link Xml#prefix()} annotation defined on package. *
  3. {@link Xml#prefix()} annotation defined on superclasses. *
  4. {@link Xml#prefix()} annotation defined on superclass packages. *
  5. {@link Xml#prefix()} annotation defined on interfaces. *
  6. {@link Xml#prefix()} annotation defined on interface packages. *
* * @return The namespace associated with this class, or null if no namespace is associated with it. */ protected Namespace getNamespace() { return namespace; } private static Namespace findNamespace(Class c) { if (c == null) return null; List xmls = findAnnotations(Xml.class, c); List schemas = findAnnotations(XmlSchema.class, c); return XmlUtils.findNamespace(xmls, schemas); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy