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