/**
* Copyright 2005-2014 Restlet
*
* The contents of this file are subject to the terms of one of the following
* open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
* select the license that you prefer but you may not use this file except in
* compliance with one of these Licenses.
*
* You can obtain a copy of the Apache 2.0 license at
* http://www.opensource.org/licenses/apache-2.0
*
* You can obtain a copy of the EPL 1.0 license at
* http://www.opensource.org/licenses/eclipse-1.0
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://restlet.com/products/restlet-framework
*
* Restlet is a registered trademark of Restlet S.A.S.
*/
package org.restlet.ext.wadl;
import static org.restlet.ext.wadl.WadlRepresentation.APP_NAMESPACE;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.restlet.ext.xml.XmlWriter;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* Describes a reusable type of resources.
*
* @author Jerome Louvel
*/
public class ResourceTypeInfo extends DocumentedInfo {
/** Identifier for that element. */
private String identifier;
/** List of supported methods. */
private List<MethodInfo> methods;
/** List of parameters. */
private List<ParameterInfo> parameters;
/**
* Constructor.
*/
public ResourceTypeInfo() {
super();
}
/**
* Constructor with a single documentation element.
*
* @param documentation
* A single documentation element.
*/
public ResourceTypeInfo(DocumentationInfo documentation) {
super(documentation);
}
/**
* Constructor with a list of documentation elements.
*
* @param documentations
* The list of documentation elements.
*/
public ResourceTypeInfo(List<DocumentationInfo> documentations) {
super(documentations);
}
/**
* Constructor with a single documentation element.
*
* @param documentation
* A single documentation element.
*/
public ResourceTypeInfo(String documentation) {
super(documentation);
}
/**
* Returns the identifier for that element.
*
* @return The identifier for that element.
*/
public String getIdentifier() {
return this.identifier;
}
/**
* Returns the list of supported methods.
*
* @return The list of supported methods.
*/
public List<MethodInfo> getMethods() {
// Lazy initialization with double-check.
List<MethodInfo> m = this.methods;
if (m == null) {
synchronized (this) {
m = this.methods;
if (m == null) {
this.methods = m = new ArrayList<MethodInfo>();
}
}
}
return m;
}
/**
* Returns the list of parameters.
*
* @return The list of parameters.
*/
public List<ParameterInfo> getParameters() {
// Lazy initialization with double-check.
List<ParameterInfo> p = this.parameters;
if (p == null) {
synchronized (this) {
p = this.parameters;
if (p == null) {
this.parameters = p = new ArrayList<ParameterInfo>();
}
}
}
return p;
}
/**
* Sets the identifier for that element.
*
* @param identifier
* The identifier for that element.
*/
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
/**
* Sets the list of supported methods.
*
* @param methods
* The list of supported methods.
*/
public void setMethods(List<MethodInfo> methods) {
this.methods = methods;
}
/**
* Sets the list of parameters.
*
* @param parameters
* The list of parameters.
*/
public void setParameters(List<ParameterInfo> parameters) {
this.parameters = parameters;
}
@Override
public void updateNamespaces(Map<String, String> namespaces) {
namespaces.putAll(resolveNamespaces());
for (final MethodInfo methodInfo : getMethods()) {
methodInfo.updateNamespaces(namespaces);
}
for (final ParameterInfo parameterInfo : getParameters()) {
parameterInfo.updateNamespaces(namespaces);
}
}
/**
* Writes the current object as an XML element using the given SAX writer.
*
* @param writer
* The SAX writer.
* @throws SAXException
*/
public void writeElement(XmlWriter writer) throws SAXException {
final AttributesImpl attributes = new AttributesImpl();
if ((getIdentifier() != null) && !getIdentifier().equals("")) {
attributes.addAttribute("", "id", null, "xs:ID", getIdentifier());
}
if (getDocumentations().isEmpty() && getMethods().isEmpty()
&& getParameters().isEmpty()) {
writer.emptyElement(APP_NAMESPACE, "resource_type", null,
attributes);
} else {
writer.startElement(APP_NAMESPACE, "resource_type", null,
attributes);
for (final DocumentationInfo documentationInfo : getDocumentations()) {
documentationInfo.writeElement(writer);
}
for (final MethodInfo methodInfo : getMethods()) {
methodInfo.writeElement(writer);
}
for (final ParameterInfo parameterInfo : getParameters()) {
parameterInfo.writeElement(writer);
}
writer.endElement(APP_NAMESPACE, "resource_type");
}
}
}