001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/owscommon_new/XMLFactory.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.owscommon_new;
037    
038    import java.net.URI;
039    import java.util.List;
040    
041    import org.deegree.datatypes.Code;
042    import org.deegree.datatypes.values.TypedLiteral;
043    import org.deegree.framework.xml.XMLTools;
044    import org.deegree.model.metadata.iso19115.CitedResponsibleParty;
045    import org.deegree.model.metadata.iso19115.Constraints;
046    import org.deegree.model.metadata.iso19115.Keywords;
047    import org.deegree.model.metadata.iso19115.OnlineResource;
048    import org.deegree.ogcbase.CommonNamespaces;
049    import org.w3c.dom.Element;
050    
051    /**
052     * <code>XMLFactory</code> for generation of Capabilities XML documents according to the OWS
053     * common specification 1.0.0.
054     *
055     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
059     *
060     * @since 2.0
061     */
062    
063    public class XMLFactory extends org.deegree.ogcbase.XMLFactory {
064    
065        private static URI OWS = CommonNamespaces.OWSNS;
066    
067        private static String POWS = CommonNamespaces.OWS_PREFIX + ":";
068    
069        /**
070         * Exports the given capabilities as XML.
071         *
072         * @param root
073         *            the root capabilities element according to the specification of the service
074         * @param caps
075         */
076        public static void appendBaseCapabilities( Element root, OWSCommonCapabilities caps ) {
077            ServiceIdentification identification = caps.getServiceIdentification();
078            if ( identification != null )
079                appendServiceIdentification( root, identification );
080    
081            ServiceProvider provider = caps.getServiceProvider();
082            if ( provider != null )
083                appendServiceProvider( root, provider );
084    
085            OperationsMetadata metadata = caps.getOperationsMetadata();
086            if ( metadata != null )
087                appendOperationsMetadata( root, metadata );
088    
089            root.setAttribute( "version", caps.getVersion() );
090    
091            String updateSequence = caps.getUpdateSequence();
092            if ( updateSequence != null )
093                root.setAttribute( "updateSequence", updateSequence );
094        }
095    
096        /**
097         * Appends a <code>ServiceIdentification</code> object as XML.
098         *
099         * @param root
100         * @param identification
101         */
102        public static void appendServiceIdentification( Element root, ServiceIdentification identification ) {
103            Element elem = XMLTools.appendElement( root, OWS, POWS + "ServiceIdentification" );
104    
105            String title = identification.getTitle();
106            if ( title != null )
107                XMLTools.appendElement( elem, OWS, POWS + "Title", title );
108    
109            String abstractString = identification.getAbstractString();
110            if ( abstractString != null )
111                XMLTools.appendElement( elem, OWS, POWS + "Abstract", abstractString );
112    
113            List<Keywords> keywords = identification.getKeywords();
114            for ( Keywords keys : keywords )
115                org.deegree.model.metadata.iso19115.XMLFactory.appendKeywords( elem, keys );
116    
117            Code serviceType = identification.getServiceType();
118            if ( serviceType != null )
119                org.deegree.model.metadata.iso19115.XMLFactory.appendCode( elem, "Code", serviceType );
120    
121            List<String> versions = identification.getServiceTypeVersions();
122            for ( String version : versions )
123                XMLTools.appendElement( elem, OWS, POWS + "ServiceTypeVersion", version );
124    
125            List<Constraints> accessConstraints = identification.getAccessConstraints();
126            if ( accessConstraints.size() > 0 ) {
127                // append the first fee data from the constraints, ignore the rest
128                String fees = accessConstraints.get( 0 ).getFees();
129                XMLTools.appendElement( elem, OWS, POWS + "Fees", fees );
130    
131                for ( Constraints constraints : accessConstraints )
132                    org.deegree.model.metadata.iso19115.XMLFactory.appendAccessConstraint( elem, constraints );
133            }
134        }
135    
136        /**
137         * Appends a <code>ServiceProvider</code> object as XML.
138         *
139         * @param root
140         *            where to append
141         * @param provider
142         *            the object to append
143         */
144        public static void appendServiceProvider( Element root, ServiceProvider provider ) {
145            Element elem = XMLTools.appendElement( root, OWS, POWS + "ServiceProvider" );
146    
147            String name = provider.getProviderName();
148            if ( name != null )
149                XMLTools.appendElement( elem, OWS, POWS + "ProviderName", name );
150    
151            OnlineResource resource = provider.getProviderSite();
152            if ( resource != null ) {
153                Element resElement = XMLTools.appendElement( elem, OWS, POWS + "ProviderSite" );
154                org.deegree.model.metadata.iso19115.XMLFactory.appendOnlineResource( resElement, resource );
155            }
156    
157            CitedResponsibleParty party = provider.getServiceContact();
158            org.deegree.model.metadata.iso19115.XMLFactory.appendCitedResponsibleParty( elem, party );
159        }
160    
161        /**
162         * Appends an <code>OperationsMetadata</code> object as XML.
163         *
164         * @param root
165         *            where to append
166         * @param data
167         *            what to append
168         */
169        public static void appendOperationsMetadata( Element root, OperationsMetadata data ) {
170            Element elem = XMLTools.appendElement( root, OWS, POWS + "OperationsMetadata" );
171    
172            List<Operation> operations = data.getOperations();
173            for ( Operation operation : operations )
174                appendOperation( elem, operation );
175    
176            List<Parameter> parameters = data.getParameters();
177            for ( Parameter parameter : parameters ) {
178                if ( parameter instanceof DomainType )
179                    appendDomainType( elem, "Parameter", (DomainType) parameter );
180            }
181    
182            List<DomainType> constraints = data.getConstraints();
183            for ( DomainType constraint : constraints )
184                appendDomainType( elem, "Constraint", constraint );
185    
186            // extended capabilities are ignored
187        }
188    
189        /**
190         * Appends an <code>Operation</code> object as XML.
191         *
192         * @param root
193         *            where to append
194         * @param operation
195         *            what to append
196         */
197        public static void appendOperation( Element root, Operation operation ) {
198            Element elem = XMLTools.appendElement( root, OWS, POWS + "Operation" );
199    
200            List<DCP> dcps = operation.getDCP();
201            for ( DCP dcp : dcps )
202                appendDCP( elem, dcp );
203    
204            List<Parameter> parameters = operation.getParameters();
205            for ( Parameter parameter : parameters ) {
206                if ( parameter instanceof DomainType )
207                    appendDomainType( elem, "Parameter", (DomainType) parameter );
208            }
209    
210            List<DomainType> constraints = operation.getConstraints();
211            for ( DomainType constraint : constraints )
212                appendDomainType( elem, "Constraint", constraint );
213    
214            Object md = operation.getMetadata();
215            if ( md instanceof List ) {
216                List<?> metadata = (List) md;
217                for ( Object obj : metadata ) {
218                    if ( obj instanceof Metadata )
219                        appendMetadata( elem, (Metadata) obj );
220                }
221            }
222    
223            elem.setAttribute( "name", operation.getName().getPrefixedName() );
224        }
225    
226        /**
227         * Appends a <code>DCP</code> object as XML.
228         *
229         * @param root
230         *            where to append
231         * @param dcp
232         *            what to append
233         */
234        public static void appendDCP( Element root, DCP dcp ) {
235            Element dcpElem = XMLTools.appendElement( root, OWS, POWS + "DCP" );
236    
237            if ( dcp instanceof HTTP )
238                appendHTTP( dcpElem, (HTTP) dcp );
239        }
240    
241        /**
242         * Appends a <code>HTTP</code> object as XML.
243         *
244         * @param root
245         *            where to append
246         * @param http
247         *            what to append
248         */
249        public static void appendHTTP( Element root, HTTP http ) {
250            Element elem = XMLTools.appendElement( root, OWS, POWS + "HTTP" );
251    
252            List<HTTP.Type> types = http.getTypes();
253            List<List<DomainType>> constraints = http.getConstraints();
254            List<OnlineResource> links = http.getLinks();
255    
256            for ( int i = 0; i < types.size(); ++i ) {
257                String type = ( types.get( i ) == HTTP.Type.Get ) ? "Get" : "Post";
258                Element getpost = XMLTools.appendElement( elem, OWS, POWS + type );
259                org.deegree.model.metadata.iso19115.XMLFactory.appendOnlineResource( getpost, links.get( i ) );
260                List<DomainType> constraintList = constraints.get( i );
261                for ( DomainType constraint : constraintList )
262                    appendDomainType( getpost, "Constraint", constraint );
263            }
264        }
265    
266        /**
267         * Appends a <code>DomainType</code> object as XML.
268         *
269         * @param root
270         *            where to append
271         * @param tagName
272         *            under which name to append
273         * @param data
274         *            what to append
275         */
276        public static void appendDomainType( Element root, String tagName, DomainType data ) {
277            Element elem = XMLTools.appendElement( root, OWS, POWS + tagName );
278    
279            List<TypedLiteral> values = data.getValues();
280            for ( TypedLiteral value : values )
281                XMLTools.appendElement( elem, OWS, POWS + "Value", value.getValue() );
282    
283            Object md = data.getMetadata();
284            if ( md instanceof List ) {
285                List<?> metadata = (List) md;
286                for ( Object obj : metadata ) {
287                    if ( obj instanceof Metadata )
288                        appendMetadata( elem, (Metadata) obj );
289                }
290            }
291    
292            elem.setAttribute( "name", data.getName().getPrefixedName() );
293        }
294    
295        /**
296         * Appends a <code>Metadata</code> object as XML.
297         *
298         * @param root
299         *            where to append
300         * @param data
301         *            what to append
302         */
303        public static void appendMetadata( Element root, Metadata data ) {
304            Element elem = XMLTools.appendElement( root, OWS, POWS + "Metadata" );
305            elem.setAttribute( "about", data.getAbout().toASCIIString() );
306            appendSimpleLinkAttributes( elem, data.getLink() );
307        }
308    
309    }