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