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