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