001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wmps/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.ogcwebservices.wmps;
037    
038    import java.io.IOException;
039    import java.net.URL;
040    import java.util.Date;
041    import java.util.List;
042    
043    import org.deegree.datatypes.xlink.SimpleLink;
044    import org.deegree.framework.log.ILogger;
045    import org.deegree.framework.log.LoggerFactory;
046    import org.deegree.framework.util.Pair;
047    import org.deegree.framework.xml.NamespaceContext;
048    import org.deegree.framework.xml.XMLParsingException;
049    import org.deegree.framework.xml.XMLTools;
050    import org.deegree.model.metadata.iso19115.Address;
051    import org.deegree.model.metadata.iso19115.Keywords;
052    import org.deegree.model.metadata.iso19115.Phone;
053    import org.deegree.model.spatialschema.Envelope;
054    import org.deegree.ogcbase.CommonNamespaces;
055    import org.deegree.ogcwebservices.getcapabilities.DCPType;
056    import org.deegree.ogcwebservices.getcapabilities.HTTP;
057    import org.deegree.ogcwebservices.getcapabilities.MetadataURL;
058    import org.deegree.ogcwebservices.getcapabilities.Operation;
059    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
060    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
061    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilities;
062    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilitiesDocument;
063    import org.deegree.ogcwebservices.wmps.capabilities.WMPSOperationsMetadata;
064    import org.deegree.ogcwebservices.wmps.operation.DescribeTemplateResponse;
065    import org.deegree.ogcwebservices.wmps.operation.DescribeTemplateResponseDocument;
066    import org.deegree.ogcwebservices.wmps.operation.GetAvailableTemplatesResponse;
067    import org.deegree.ogcwebservices.wmps.operation.GetAvailableTemplatesResponseDocument;
068    import org.deegree.ogcwebservices.wmps.operation.PrintMapResponse;
069    import org.deegree.ogcwebservices.wmps.operation.PrintMapResponseDocument;
070    import org.deegree.ogcwebservices.wms.capabilities.Attribution;
071    import org.deegree.ogcwebservices.wms.capabilities.AuthorityURL;
072    import org.deegree.ogcwebservices.wms.capabilities.DataURL;
073    import org.deegree.ogcwebservices.wms.capabilities.Dimension;
074    import org.deegree.ogcwebservices.wms.capabilities.Extent;
075    import org.deegree.ogcwebservices.wms.capabilities.FeatureListURL;
076    import org.deegree.ogcwebservices.wms.capabilities.Identifier;
077    import org.deegree.ogcwebservices.wms.capabilities.Layer;
078    import org.deegree.ogcwebservices.wms.capabilities.LayerBoundingBox;
079    import org.deegree.ogcwebservices.wms.capabilities.LegendURL;
080    import org.deegree.ogcwebservices.wms.capabilities.LogoURL;
081    import org.deegree.ogcwebservices.wms.capabilities.ScaleHint;
082    import org.deegree.ogcwebservices.wms.capabilities.Style;
083    import org.deegree.ogcwebservices.wms.capabilities.StyleSheetURL;
084    import org.deegree.ogcwebservices.wms.capabilities.StyleURL;
085    import org.deegree.ogcwebservices.wms.capabilities.UserDefinedSymbolization;
086    import org.deegree.owscommon.OWSDomainType;
087    import org.w3c.dom.Element;
088    import org.w3c.dom.Node;
089    import org.xml.sax.SAXException;
090    
091    /**
092     * Helper class to create WMPS responses.
093     *
094     * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
095     * @version 2.0
096     *
097     */
098    public class XMLFactory extends org.deegree.owscommon.XMLFactory {
099    
100        private static final ILogger LOG = LoggerFactory.getLogger( XMLFactory.class );
101    
102        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
103    
104        /**
105         * Exports a <code>WMPSCapabilities</code> instance to a <code>WMPSCapabilitiesDocument</code>.
106         *
107         * @param capabilities
108         * @return DOM representation of the <code>WMPSCapabilities</code>
109         * @throws IOException
110         *             if XML template could not be loaded
111         */
112        public static WMPSCapabilitiesDocument export( WMPSCapabilities capabilities )
113                                throws IOException {
114    
115            WMPSCapabilitiesDocument capabilitiesDocument = new WMPSCapabilitiesDocument();
116            try {
117                capabilitiesDocument.createEmptyDocument();
118                Element root = capabilitiesDocument.getRootElement();
119                root.setAttribute( "version", capabilities.getVersion() );
120    
121                appendService( root, capabilities.getServiceIdentification(), capabilities.getServiceProvider() );
122    
123                String xPath = "./Capability";
124                appendUserDefinedSymbolization( (Element) XMLTools.getNode( root, xPath, nsContext ),
125                                                capabilities.getUserDefinedSymbolization() );
126    
127                appendCapabilityRequests( root, (WMPSOperationsMetadata) capabilities.getOperationMetadata() );
128    
129                appendCapabilityLayer( (Element) XMLTools.getNode( root, "./Capability", nsContext ),
130                                       capabilities.getLayer() );
131    
132            } catch ( SAXException e ) {
133                LOG.logError( e.getMessage(), e );
134            } catch ( XMLParsingException e ) {
135                LOG.logError( e.getMessage(), e );
136            }
137    
138            return capabilitiesDocument;
139        }
140    
141        /**
142         * Append User defined symbolization.
143         *
144         * @param root
145         * @param uds
146         */
147        protected static void appendUserDefinedSymbolization( Element root, UserDefinedSymbolization uds ) {
148    
149            Element elem = XMLTools.appendElement( root, null, "UserDefinedSymbolization" );
150            elem.setAttribute( "SupportSLD", Boolean.toString( uds.isSldSupported() ) );
151            elem.setAttribute( "UserLayer", Boolean.toString( uds.isUserLayerSupported() ) );
152            elem.setAttribute( "UserStyle", Boolean.toString( uds.isUserStyleSupported() ) );
153            elem.setAttribute( "RemoteWFS", Boolean.toString( uds.isRemoteWFSSupported() ) );
154    
155        }
156    
157        /**
158         * Append service element
159         *
160         * @param root
161         * @param identification
162         * @param provider
163         * @throws XMLParsingException
164         */
165        protected static void appendService( Element root, ServiceIdentification identification, ServiceProvider provider )
166                                throws XMLParsingException {
167    
168            root = (Element) XMLTools.getRequiredNode( root, "./Service", nsContext );
169    
170            Node node = XMLTools.getRequiredNode( root, "./Name", nsContext );
171            XMLTools.setNodeValue( (Element) node, identification.getTitle() );
172    
173            node = XMLTools.getRequiredNode( root, "./Title", nsContext );
174            XMLTools.setNodeValue( (Element) node, identification.getTitle() );
175    
176            String serviceAbstract = identification.getAbstract();
177            if ( serviceAbstract != null ) {
178                XMLTools.appendElement( root, null, "Abstract", serviceAbstract );
179            }
180    
181            Keywords[] keywords = identification.getKeywords();
182            if ( keywords.length > 0 ) {
183                String[] kw = keywords[0].getKeywords();
184                Element kwl = XMLTools.appendElement( root, null, "KeywordList" );
185                for ( int i = 0; i < kw.length; i++ ) {
186                    XMLTools.appendElement( kwl, null, "Keyword", kw[i] );
187                }
188            }
189    
190            node = XMLTools.getRequiredNode( root, "./OnlineResource", nsContext );
191            SimpleLink sLink = provider.getProviderSite();
192            ( (Element) node ).setAttribute( "xlink:href", sLink.getHref().toASCIIString() );
193    
194            appendContactInformation( root, provider );
195    
196            if ( identification.getFees() != null ) {
197                XMLTools.appendElement( root, null, "Fees", identification.getFees() );
198            } else {
199                XMLTools.appendElement( root, null, "Fees", "none" );
200            }
201    
202            if ( identification.getAccessConstraints().length > 0 ) {
203                XMLTools.appendElement( root, null, "AccessConstraints", identification.getAccessConstraints()[0] );
204            } else {
205                XMLTools.appendElement( root, null, "AccessConstraints", "none" );
206            }
207    
208        }
209    
210        /**
211         * Append contact information
212         *
213         * @param root
214         * @param provider
215         */
216        protected static void appendContactInformation( Element root, ServiceProvider provider ) {
217    
218            Element ciNode = XMLTools.appendElement( root, null, "ContactInformation" );
219            Element cppNode = XMLTools.appendElement( ciNode, null, "ContactPersonPrimary" );
220            if ( provider.getIndividualName() != null ) {
221                XMLTools.appendElement( cppNode, null, "ContactPerson", provider.getIndividualName() );
222            }
223            if ( provider.getProviderName() != null ) {
224                XMLTools.appendElement( cppNode, null, "ContactOrganization", provider.getProviderName() );
225            }
226            if ( provider.getPositionName() != null ) {
227                XMLTools.appendElement( ciNode, null, "ContactPosition", provider.getPositionName() );
228            }
229            Element caNode = XMLTools.appendElement( ciNode, null, "ContactAddress" );
230    
231            XMLTools.appendElement( caNode, null, "AddressType", "postal" );
232    
233            Address addr = provider.getContactInfo().getAddress();
234            String[] dp = addr.getDeliveryPoint();
235            if ( dp.length > 0 ) {
236                XMLTools.appendElement( caNode, null, "Address", dp[0] );
237            }
238            if ( addr.getCity() != null ) {
239                XMLTools.appendElement( caNode, null, "City", addr.getCity() );
240            }
241            if ( addr.getAdministrativeArea() != null ) {
242                XMLTools.appendElement( caNode, null, "StateOrProvince", addr.getAdministrativeArea() );
243            }
244            if ( addr.getPostalCode() != null ) {
245                XMLTools.appendElement( caNode, null, "PostCode", addr.getPostalCode() );
246            }
247            if ( addr.getCountry() != null ) {
248                XMLTools.appendElement( caNode, null, "Country", addr.getCountry() );
249            }
250            Phone phone = provider.getContactInfo().getPhone();
251            if ( phone.getVoice().length > 0 ) {
252                XMLTools.appendElement( ciNode, null, "ContactVoiceTelephone", phone.getVoice()[0] );
253            }
254            if ( phone.getFacsimile().length > 0 ) {
255                XMLTools.appendElement( ciNode, null, "ContactFacsimileTelephone", phone.getFacsimile()[0] );
256            }
257            if ( addr.getElectronicMailAddress().length > 0 ) {
258                XMLTools.appendElement( ciNode, null, "ContactElectronicMailAddress", addr.getElectronicMailAddress()[0] );
259            }
260    
261        }
262    
263        /**
264         * Append capability element.
265         *
266         * @param root
267         * @param operationsMetadata
268         * @throws XMLParsingException
269         */
270        protected static void appendCapabilityRequests( Element root, WMPSOperationsMetadata operationsMetadata )
271                                throws XMLParsingException {
272    
273            root = (Element) XMLTools.getRequiredNode( root, "./Capability/Request", nsContext );
274    
275            Operation[] ops = operationsMetadata.getOperations();
276            for ( int i = 0; i < ops.length; i++ ) {
277                if ( ops[i] != null ) {
278                    appendOperation( root, ops[i] );
279                }
280            }
281    
282        }
283    
284        /**
285         * Append Operations.
286         *
287         * @param root
288         * @param operation
289         */
290        protected static void appendOperation( Element root, Operation operation ) {
291    
292            String name = operation.getName();
293            root = XMLTools.appendElement( root, null, name );
294    
295            OWSDomainType odt = operation.getParameter( "Format" );
296            String[] values = odt.getValues();
297            for ( int i = 0; i < values.length; i++ ) {
298                XMLTools.appendElement( root, null, "Format", values[i] );
299            }
300    
301            DCPType[] dcps = operation.getDCPs();
302            for ( int i = 0; i < dcps.length; i++ ) {
303                Element http = XMLTools.appendElement( root, null, "DCPType" );
304                http = XMLTools.appendElement( http, null, "HTTP" );
305                HTTP ht = (HTTP) dcps[i].getProtocol();
306                URL[] urls = ht.getGetOnlineResources();
307                appendURLs( http, urls, "Get" );
308                urls = ht.getPostOnlineResources();
309                appendURLs( http, urls, "Post" );
310            }
311    
312        }
313    
314        /**
315         * Append URLs
316         *
317         * @param http
318         * @param urls
319         * @param type
320         */
321        protected static void appendURLs( Element http, URL[] urls, String type ) {
322            for ( int j = 0; j < urls.length; j++ ) {
323                Element olr = XMLTools.appendElement( http, null, type );
324                appendOnlineResource( olr, urls[j] );
325            }
326        }
327    
328        /**
329         * Append capability layer
330         *
331         * @param root
332         * @param layer
333         * @throws XMLParsingException
334         */
335        protected static void appendCapabilityLayer( Element root, Layer layer )
336                                throws XMLParsingException {
337    
338            root = XMLTools.appendElement( root, null, "Layer" );
339            root.setAttribute( "queryable", Boolean.toString( layer.isQueryable() ) );
340            root.setAttribute( "cascaded", Integer.toString( layer.getCascaded() ) );
341            root.setAttribute( "opaque", Boolean.toString( layer.isOpaque() ) );
342            root.setAttribute( "noSubsets", Boolean.toString( layer.hasNoSubsets() ) );
343            if ( layer.getFixedWidth() > 0 ) {
344                root.setAttribute( "fixedWidth", Integer.toString( layer.getFixedWidth() ) );
345            }
346            if ( layer.getFixedHeight() > 0 ) {
347                root.setAttribute( "fixedHeight", Integer.toString( layer.getFixedHeight() ) );
348            }
349    
350            if ( layer.getName() != null ) {
351                XMLTools.appendElement( root, null, "Name", layer.getName() );
352            }
353            XMLTools.appendElement( root, null, "Title", layer.getTitle() );
354    
355            if ( layer.getAbstract() != null ) {
356                XMLTools.appendElement( root, null, "Abstract", layer.getAbstract() );
357            }
358    
359            String[] keywords = layer.getKeywordList();
360            if ( keywords.length > 0 ) {
361                Element elem = XMLTools.appendElement( root, null, "KeywordList" );
362                for ( int i = 0; i < keywords.length; i++ ) {
363                    XMLTools.appendElement( elem, null, "Keyword", keywords[i] );
364                }
365            }
366    
367            String[] srs = layer.getSrs();
368            for ( int i = 0; i < srs.length; i++ ) {
369                XMLTools.appendElement( root, null, "SRS", srs[i] );
370            }
371    
372            Envelope llBox = layer.getLatLonBoundingBox();
373            appendLatLonBoundingBox( root, llBox );
374    
375            LayerBoundingBox[] lBoxes = layer.getBoundingBoxes();
376            for ( int i = 0; i < lBoxes.length; i++ ) {
377                appendLayerBoundingBox( root, lBoxes[i] );
378            }
379    
380            Dimension[] dims = layer.getDimension();
381            for ( int i = 0; i < dims.length; i++ ) {
382                appendDimension( root, dims[i] );
383            }
384    
385            Extent[] extents = layer.getExtent();
386            for ( int i = 0; i < extents.length; i++ ) {
387                appendExtent( root, extents[i] );
388            }
389    
390            Attribution attr = layer.getAttribution();
391            if ( attr != null ) {
392                appendAttribution( root, attr );
393            }
394    
395            AuthorityURL[] authorityURLs = layer.getAuthorityURL();
396            for ( int i = 0; i < authorityURLs.length; i++ ) {
397                appendAuthorityURL( root, authorityURLs[i] );
398            }
399    
400            Identifier[] identifiers = layer.getIdentifier();
401            for ( int i = 0; i < identifiers.length; i++ ) {
402                appendIdentifier( root, identifiers[i] );
403            }
404    
405            MetadataURL[] metadataURLs = layer.getMetadataURL();
406            for ( int i = 0; i < metadataURLs.length; i++ ) {
407                appendMetadataURL( root, metadataURLs[i] );
408            }
409    
410            DataURL[] dataURLs = layer.getDataURL();
411            for ( int i = 0; i < dataURLs.length; i++ ) {
412                appendDataURL( root, dataURLs[i] );
413            }
414    
415            FeatureListURL[] featureListURLs = layer.getFeatureListURL();
416            for ( int i = 0; i < featureListURLs.length; i++ ) {
417                appendFeatureListURL( root, featureListURLs[i] );
418            }
419    
420            Style[] styles = layer.getStyles();
421            for ( int i = 0; i < styles.length; i++ ) {
422                appendStyle( root, styles[i] );
423            }
424    
425            ScaleHint scaleHint = layer.getScaleHint();
426            Element elem = XMLTools.appendElement( root, null, "ScaleHint" );
427            elem.setAttribute( "min", "" + scaleHint.getMin() );
428            elem.setAttribute( "max", "" + scaleHint.getMax() );
429    
430            Layer[] layers = layer.getLayer();
431            for ( int i = 0; i < layers.length; i++ ) {
432                appendCapabilityLayer( root, layers[i] );
433            }
434    
435        }
436    
437        /**
438         * Append style
439         *
440         * @param root
441         * @param style
442         */
443        protected static void appendStyle( Element root, Style style ) {
444    
445            root = XMLTools.appendElement( root, null, "Style" );
446            XMLTools.appendElement( root, null, "Name", style.getName() );
447            if ( style.getTitle() != null ) {
448                XMLTools.appendElement( root, null, "Title", style.getTitle() );
449            }
450            if ( style.getAbstract() != null ) {
451                XMLTools.appendElement( root, null, "Abstract", style.getAbstract() );
452            }
453            LegendURL[] legendURLs = style.getLegendURL();
454            for ( int i = 0; i < legendURLs.length; i++ ) {
455                appendLegendURL( root, legendURLs[i] );
456            }
457    
458            StyleSheetURL styleSheetURL = style.getStyleSheetURL();
459            if ( styleSheetURL != null ) {
460                appendStyleSheetURL( root, styleSheetURL );
461            }
462    
463            StyleURL styleURL = style.getStyleURL();
464            if ( styleURL != null ) {
465                appendStyleURL( root, styleURL );
466            }
467    
468        }
469    
470        /**
471         * Append Style URL
472         *
473         * @param root
474         * @param styleURL
475         *
476         */
477        protected static void appendStyleURL( Element root, StyleURL styleURL ) {
478            Element elem = XMLTools.appendElement( root, null, "StyleURL" );
479            XMLTools.appendElement( elem, null, "Format", styleURL.getFormat() );
480            appendOnlineResource( elem, styleURL.getOnlineResource() );
481        }
482    
483        /**
484         * Append Style sheet.
485         *
486         * @param root
487         * @param styleSheetURL
488         */
489        protected static void appendStyleSheetURL( Element root, StyleSheetURL styleSheetURL ) {
490            Element elem = XMLTools.appendElement( root, null, "StyleSheetURL" );
491            XMLTools.appendElement( elem, null, "Format", styleSheetURL.getFormat() );
492            appendOnlineResource( elem, styleSheetURL.getOnlineResource() );
493        }
494    
495        /**
496         * Append legend url.
497         *
498         * @param root
499         * @param legendURL
500         */
501        protected static void appendLegendURL( Element root, LegendURL legendURL ) {
502            Element elem = XMLTools.appendElement( root, null, "LegendURL" );
503            elem.setAttribute( "width", "" + legendURL.getWidth() );
504            elem.setAttribute( "height", "" + legendURL.getWidth() );
505            XMLTools.appendElement( elem, null, "Format", legendURL.getFormat() );
506    
507            appendOnlineResource( elem, legendURL.getOnlineResource() );
508        }
509    
510        /**
511         * Append feature list url.
512         *
513         * @param root
514         * @param featureListURL
515         */
516        protected static void appendFeatureListURL( Element root, FeatureListURL featureListURL ) {
517            Element elem = XMLTools.appendElement( root, null, "FeatureListURL" );
518            XMLTools.appendElement( elem, null, "Format", featureListURL.getFormat() );
519            appendOnlineResource( elem, featureListURL.getOnlineResource() );
520        }
521    
522        /**
523         * Append data url.
524         *
525         * @param root
526         * @param dataURL
527         */
528        protected static void appendDataURL( Element root, DataURL dataURL ) {
529            Element elem = XMLTools.appendElement( root, null, "DataURL" );
530            XMLTools.appendElement( elem, null, "Format", dataURL.getFormat() );
531            appendOnlineResource( elem, dataURL.getOnlineResource() );
532        }
533    
534        /**
535         * Append metadata url.
536         *
537         * @param root
538         * @param metadataURL
539         */
540        protected static void appendMetadataURL( Element root, MetadataURL metadataURL ) {
541            Element elem = XMLTools.appendElement( root, null, "MetadataURL" );
542            elem.setAttribute( "type", metadataURL.getType() );
543            XMLTools.appendElement( elem, null, "Format", metadataURL.getFormat() );
544            appendOnlineResource( elem, metadataURL.getOnlineResource() );
545        }
546    
547        /**
548         * Append identifiers.
549         *
550         * @param root
551         * @param identifier
552         */
553        protected static void appendIdentifier( Element root, Identifier identifier ) {
554            Element elem = XMLTools.appendElement( root, null, "Identifier" );
555            elem.setAttribute( "authority", identifier.getAuthority() );
556            XMLTools.setNodeValue( elem, identifier.getValue() );
557        }
558    
559        /**
560         * Append authority url.
561         *
562         * @param root
563         * @param authorityURL
564         */
565        protected static void appendAuthorityURL( Element root, AuthorityURL authorityURL ) {
566            Element elem = XMLTools.appendElement( root, null, "AuthorityURL" );
567            elem.setAttribute( "name", authorityURL.getName() );
568            appendOnlineResource( elem, authorityURL.getOnlineResource() );
569        }
570    
571        /**
572         * Append attribution url.
573         *
574         * @param root
575         * @param attr
576         */
577        protected static void appendAttribution( Element root, Attribution attr ) {
578            Element elem = XMLTools.appendElement( root, null, "Attribution" );
579            XMLTools.appendElement( elem, null, "Title", attr.getTitle() );
580            appendOnlineResource( elem, attr.getOnlineResource() );
581            LogoURL logoURL = attr.getLogoURL();
582            if ( logoURL != null ) {
583                elem = XMLTools.appendElement( elem, null, "LogoURL" );
584                elem.setAttribute( "width", "" + logoURL.getWidth() );
585                elem.setAttribute( "height", "" + logoURL.getHeight() );
586                XMLTools.appendElement( elem, null, "Format", logoURL.getFormat() );
587                appendOnlineResource( elem, logoURL.getOnlineResource() );
588            }
589        }
590    
591        /**
592         * Append online resource.
593         *
594         * @param root
595         * @param url
596         */
597        protected static void appendOnlineResource( Element root, URL url ) {
598            Element olr = XMLTools.appendElement( root, null, "OnlineResource" );
599            olr.setAttribute( "xlink:type", "simple" );
600            olr.setAttribute( "xlink:href", url.toExternalForm() );
601        }
602    
603        /**
604         * Apppend extent.
605         *
606         * @param root
607         * @param extent
608         */
609        protected static void appendExtent( Element root, Extent extent ) {
610            Element exNode = XMLTools.appendElement( root, null, "Extent" );
611            exNode.setAttribute( "name", extent.getName() );
612            exNode.setAttribute( "default", extent.getDefault() );
613            exNode.setAttribute( "nearestValue", Boolean.toString( extent.useNearestValue() ) );
614            XMLTools.setNodeValue( exNode, extent.getValue() );
615        }
616    
617        /**
618         * Append dimension.
619         *
620         * @param root
621         * @param dim
622         */
623        protected static void appendDimension( Element root, Dimension dim ) {
624            Element dimNode = XMLTools.appendElement( root, null, "Dimension" );
625            dimNode.setAttribute( "name", dim.getName() );
626            dimNode.setAttribute( "units", dim.getUnits() );
627            dimNode.setAttribute( "unitSymbol", dim.getUnitSymbol() );
628        }
629    
630        /**
631         * Append layer bounding box.
632         *
633         * @param root
634         * @param lBox
635         */
636        protected static void appendLayerBoundingBox( Element root, LayerBoundingBox lBox ) {
637            Element bbNode = XMLTools.appendElement( root, null, "BoundingBox" );
638            bbNode.setAttribute( "minx", "" + lBox.getMin().getX() );
639            bbNode.setAttribute( "miny", "" + lBox.getMin().getY() );
640            bbNode.setAttribute( "maxx", "" + lBox.getMax().getX() );
641            bbNode.setAttribute( "maxy", "" + lBox.getMax().getY() );
642            bbNode.setAttribute( "resx", "" + lBox.getResx() );
643            bbNode.setAttribute( "resy", "" + lBox.getResy() );
644            bbNode.setAttribute( "SRS", "" + lBox.getSRS() );
645        }
646    
647        /**
648         * Append lat-lon bounding box.
649         *
650         * @param root
651         * @param llBox
652         */
653        protected static void appendLatLonBoundingBox( Element root, Envelope llBox ) {
654            Element bbNode = XMLTools.appendElement( root, null, "LatLonBoundingBox" );
655            bbNode.setAttribute( "minx", "" + llBox.getMin().getX() );
656            bbNode.setAttribute( "miny", "" + llBox.getMin().getY() );
657            bbNode.setAttribute( "maxx", "" + llBox.getMax().getX() );
658            bbNode.setAttribute( "maxy", "" + llBox.getMax().getY() );
659        }
660    
661        /**
662         * Export the print map initial response document.
663         *
664         * @param response
665         * @return PrintMapResponseDocument
666         * @throws XMLParsingException
667         *
668         */
669        public static PrintMapResponseDocument export( PrintMapResponse response )
670                                throws XMLParsingException {
671    
672            PrintMapResponseDocument document = new PrintMapResponseDocument( null );
673            try {
674                document.createEmptyDocument();
675                Element root = document.getRootElement();
676                root.setAttribute( "id", response.getId() );
677                appendEmailAddress( root, response.getEmailAddress() );
678                appendTimeStamp( root, response.getTimeStamp() );
679                String exception = response.getException();
680                String message = response.getMessage();
681                if ( exception != null ) {
682                    message = message + " " + exception;
683                }
684                appendMessage( root, message );
685                appendExpectedTime( root, response.getExpectedTime() );
686            } catch ( SAXException e ) {
687                LOG.logError( e.getMessage(), e );
688            } catch ( IOException e ) {
689                LOG.logError( e.getMessage(), e );
690            }
691    
692            return document;
693    
694        }
695        
696        /**
697         * Export the print map initial response document.
698         *
699         * @param response
700         * @return GetAvailableTemplatesResponseDocument
701         * @throws XMLParsingException
702         *
703         */
704        public static GetAvailableTemplatesResponseDocument export( GetAvailableTemplatesResponse response )
705                                throws XMLParsingException {
706    
707            GetAvailableTemplatesResponseDocument document = new GetAvailableTemplatesResponseDocument( null );
708            try {
709                document.createEmptyDocument();
710                Element root = document.getRootElement();
711                
712                List<String> list = response.getTemplates();
713                for ( String template : list ) {
714                    Element tmplNode = XMLTools.appendElement( root, CommonNamespaces.DEEGREEWMPS, "Template" );
715                    XMLTools.setNodeValue( tmplNode, template );
716                }
717                
718            } catch ( SAXException e ) {
719                LOG.logError( e.getMessage(), e );
720            } catch ( IOException e ) {
721                LOG.logError( e.getMessage(), e );
722            }
723    
724            return document;
725    
726        }
727        
728        /**
729         * @param response
730         * @return DescribeTemplateResponse as XML document
731         */
732        public static Object export( DescribeTemplateResponse response ) {
733            DescribeTemplateResponseDocument document = new DescribeTemplateResponseDocument( null );
734            try {
735                document.createEmptyDocument();
736                Element root = document.getRootElement();
737                
738                List<Pair<String, String>> list = response.getParamter();
739                for ( Pair<String, String> pair : list ) {                
740                    Element tmplNode = XMLTools.appendElement( root, CommonNamespaces.DEEGREEWMPS, "Parameter" );
741                    tmplNode.setAttribute( "name", pair.first );
742                    tmplNode.setAttribute( "type", pair.second );
743                }
744                
745            } catch ( SAXException e ) {
746                LOG.logError( e.getMessage(), e );
747            } catch ( IOException e ) {
748                LOG.logError( e.getMessage(), e );
749            }
750    
751            return document;
752    
753        }
754    
755        /**
756         * Append email address.
757         *
758         * @param root
759         * @param emailAddress
760         * @throws XMLParsingException
761         */
762        private static void appendEmailAddress( Element root, String emailAddress )
763                                throws XMLParsingException {
764    
765            Node node;
766            try {
767                node = XMLTools.getRequiredNode( root, "deegreewmps:EmailAddress", nsContext );
768            } catch ( XMLParsingException e ) {
769                throw new XMLParsingException( "Error getting node 'deegreewmps:EmailAddress'. "
770                                               + "Please check the WMPSInitialResponseTemplate "
771                                               + "to confirm its presence." );
772            }
773            XMLTools.setNodeValue( (Element) node, emailAddress );
774    
775        }
776    
777        /**
778         * Append expected processing time.
779         *
780         * @param root
781         * @param expectedTime
782         * @throws XMLParsingException
783         */
784        private static void appendExpectedTime( Element root, Date expectedTime )
785                                throws XMLParsingException {
786    
787            Node node;
788            try {
789                node = XMLTools.getRequiredNode( root, "deegreewmps:ExpectedProcessingTime", nsContext );
790            } catch ( XMLParsingException e ) {
791                throw new XMLParsingException( "Error getting node " + "'deegreewmps:expectedProcessingTime'. "
792                                               + "Please check the WMPSInitialResponseTemplate "
793                                               + "to confirm its presence." );
794    
795            }
796            XMLTools.setNodeValue( (Element) node, expectedTime.toString() );
797    
798        }
799    
800        /**
801         * Append message to be displayed to the user.
802         *
803         * @param root
804         * @param message
805         * @throws XMLParsingException
806         */
807        private static void appendMessage( Element root, String message )
808                                throws XMLParsingException {
809    
810            Node node;
811            try {
812                node = XMLTools.getRequiredNode( root, "deegreewmps:Message", nsContext );
813            } catch ( XMLParsingException e ) {
814                throw new XMLParsingException( "Error getting node 'deegreewmps:message'. "
815                                               + "Please check the WMPSInitialResponseTemplate "
816                                               + "to confirm its presence." );
817    
818            }
819            XMLTools.setNodeValue( (Element) node, message );
820    
821        }
822    
823        /**
824         * Append time stamp.
825         *
826         * @param root
827         * @param timeStamp
828         * @throws XMLParsingException
829         */
830        private static void appendTimeStamp( Element root, Date timeStamp )
831                                throws XMLParsingException {
832    
833            Node node;
834            try {
835                node = XMLTools.getRequiredNode( root, "deegreewmps:Timestamp", nsContext );
836            } catch ( XMLParsingException e ) {
837                throw new XMLParsingException( "Error getting node 'deegreewmps:timestamp'. "
838                                               + "Please check the WMPSInitialResponseTemplate "
839                                               + "to confirm its presence." );
840            }
841            XMLTools.setNodeValue( (Element) node, timeStamp.toString() );
842    
843        }
844       
845    
846    }