~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.ext.wadl/src/org/restlet/ext/wadl/GrammarsInfo.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.ext.wadl;
 
35
 
 
36
import static org.restlet.ext.wadl.WadlRepresentation.APP_NAMESPACE;
 
37
 
 
38
import java.util.ArrayList;
 
39
import java.util.List;
 
40
import java.util.Map;
 
41
 
 
42
import org.restlet.ext.xml.XmlWriter;
 
43
import org.xml.sax.SAXException;
 
44
 
 
45
/**
 
46
 * Describes the grammars used by representation descriptions. This is
 
47
 * especially useful to formally describe XML representations using XML Schema
 
48
 * or Relax NG standards.
 
49
 * 
 
50
 * @author Jerome Louvel
 
51
 */
 
52
public class GrammarsInfo extends DocumentedInfo {
 
53
 
 
54
    /** Definitions of data format descriptions to be included by reference. */
 
55
    private List<IncludeInfo> includes;
 
56
 
 
57
    /**
 
58
     * Constructor.
 
59
     */
 
60
    public GrammarsInfo() {
 
61
        super();
 
62
    }
 
63
 
 
64
    /**
 
65
     * Constructor with a single documentation element.
 
66
     * 
 
67
     * @param documentation
 
68
     *            A single documentation element.
 
69
     */
 
70
    public GrammarsInfo(DocumentationInfo documentation) {
 
71
        super(documentation);
 
72
    }
 
73
 
 
74
    /**
 
75
     * Constructor with a list of documentation elements.
 
76
     * 
 
77
     * @param documentations
 
78
     *            The list of documentation elements.
 
79
     */
 
80
    public GrammarsInfo(List<DocumentationInfo> documentations) {
 
81
        super(documentations);
 
82
    }
 
83
 
 
84
    /**
 
85
     * Constructor with a single documentation element.
 
86
     * 
 
87
     * @param documentation
 
88
     *            A single documentation element.
 
89
     */
 
90
    public GrammarsInfo(String documentation) {
 
91
        super(documentation);
 
92
    }
 
93
 
 
94
    /**
 
95
     * Returns the list of include elements.
 
96
     * 
 
97
     * @return The list of include elements.
 
98
     */
 
99
    public List<IncludeInfo> getIncludes() {
 
100
        // Lazy initialization with double-check.
 
101
        List<IncludeInfo> i = this.includes;
 
102
        if (i == null) {
 
103
            synchronized (this) {
 
104
                i = this.includes;
 
105
                if (i == null) {
 
106
                    this.includes = i = new ArrayList<IncludeInfo>();
 
107
                }
 
108
            }
 
109
        }
 
110
        return i;
 
111
    }
 
112
 
 
113
    /**
 
114
     * Sets the list of include elements.
 
115
     * 
 
116
     * @param includes
 
117
     *            The list of include elements.
 
118
     */
 
119
    public void setIncludes(List<IncludeInfo> includes) {
 
120
        this.includes = includes;
 
121
    }
 
122
 
 
123
    @Override
 
124
    public void updateNamespaces(Map<String, String> namespaces) {
 
125
        namespaces.putAll(resolveNamespaces());
 
126
 
 
127
        for (final IncludeInfo includeInfo : getIncludes()) {
 
128
            includeInfo.updateNamespaces(namespaces);
 
129
        }
 
130
    }
 
131
 
 
132
    /**
 
133
     * Writes the current object as an XML element using the given SAX writer.
 
134
     * 
 
135
     * @param writer
 
136
     *            The SAX writer.
 
137
     * @throws SAXException
 
138
     */
 
139
    public void writeElement(XmlWriter writer) throws SAXException {
 
140
 
 
141
        if (getDocumentations().isEmpty() && getIncludes().isEmpty()) {
 
142
            writer.emptyElement(APP_NAMESPACE, "grammars");
 
143
        } else {
 
144
            writer.startElement(APP_NAMESPACE, "grammars");
 
145
 
 
146
            for (final DocumentationInfo documentationInfo : getDocumentations()) {
 
147
                documentationInfo.writeElement(writer);
 
148
            }
 
149
 
 
150
            for (final IncludeInfo includeInfo : getIncludes()) {
 
151
                includeInfo.writeElement(writer);
 
152
            }
 
153
 
 
154
            writer.endElement(APP_NAMESPACE, "grammars");
 
155
        }
 
156
    }
 
157
 
 
158
}