001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/portal/context/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     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    
044    package org.deegree.portal.context;
045    
046    import java.awt.Rectangle;
047    import java.net.MalformedURLException;
048    import java.net.URI;
049    import java.net.URL;
050    
051    import javax.xml.parsers.ParserConfigurationException;
052    
053    import org.deegree.framework.util.Parameter;
054    import org.deegree.framework.util.ParameterList;
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.framework.xml.XMLTools;
057    import org.deegree.model.metadata.iso19115.CitedResponsibleParty;
058    import org.deegree.model.metadata.iso19115.ContactInfo;
059    import org.deegree.model.spatialschema.Point;
060    import org.deegree.ogcbase.BaseURL;
061    import org.deegree.ogcbase.CommonNamespaces;
062    import org.deegree.ogcbase.ImageURL;
063    import org.deegree.ogcwebservices.OWSUtils;
064    import org.w3c.dom.Attr;
065    import org.w3c.dom.Document;
066    import org.w3c.dom.Element;
067    import org.w3c.dom.Node;
068    import org.w3c.dom.Text;
069    
070    /**
071     * This is a factory class to export a <code>ViewContext</code> and a
072     * <code>ViewContextCollection</code> as an xml <code>org.w3c.dom.Document</code>.
073     * 
074     * 
075     * @version $Revision: 9346 $
076     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
077     * @author last edited by: $Author: apoth $
078     * 
079     * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
080     * 
081     * @since 2.0
082     */
083    public class XMLFactory {
084    
085        // Import and define constants
086        private static URI OGC_CONTEXT_NS = CommonNamespaces.CNTXTNS;
087    
088        private static URI D_CONTEXT_NS = CommonNamespaces.DGCNTXTNS;
089    
090        private static URI SLD_NS = CommonNamespaces.SLDNS;
091    
092        private static URI XSI_NS = CommonNamespaces.buildNSURI( "http://www.w3.org/2001/XMLSchema-instance" );
093    
094        private static URI XLINK_NS = CommonNamespaces.buildNSURI( "http://www.w3.org/1999/xlink" );
095    
096        // Common objects
097        protected static javax.xml.parsers.DocumentBuilderFactory factory = null;
098    
099        protected static javax.xml.parsers.DocumentBuilder builder = null;
100    
101        protected static Document document = null;
102    
103        // Forbid instantiation
104        private XMLFactory() {
105        }
106    
107        /**
108         * Convenience method for creating a common document builder. Implementation copied from
109         * XmlDocument (by tf and ap).
110         * 
111         * @throws ParserConfigurationException
112         */
113        protected static void initDocBuilder()
114                                throws ParserConfigurationException {
115    
116            if ( builder == null ) {
117                if ( factory == null ) {
118                    factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
119                    factory.setIgnoringElementContentWhitespace( true );
120                    factory.setNamespaceAware( false );
121                    factory.setExpandEntityReferences( false );
122                }
123                builder = factory.newDocumentBuilder();
124            }
125    
126        }
127    
128        /**
129         * Creates a new <code>org.w3c.dom.Document</code> using the internal document builder.
130         * 
131         * @return new <code>Document</code> instance
132         * @throws ParserConfigurationException
133         */
134        protected static Document createDocument()
135                                throws ParserConfigurationException {
136    
137            initDocBuilder();
138    
139            return builder.newDocument();
140        }
141    
142        /**
143         * Creates a new <code>org.w3c.dom.Element</code>.
144         * 
145         * @param namespace
146         *            the element namespace
147         * @param elemName
148         *            the element name
149         * @return new <code>Element</code> instance
150         */
151        private static Element createElement( URI namespace, String elemName ) {
152    
153            return document.createElementNS( namespace == null ? null : namespace.toString(), elemName );
154        }
155    
156        /**
157         * Creates a new <code>org.w3c.dom.Attr</code>.
158         * 
159         * @param attName
160         *            the attribute name
161         * @param value
162         *            the attribute value
163         * @return new <code>Attr</code> instance
164         */
165        private static Attr createAttribute( String attName, String value ) {
166    
167            Attr attr = document.createAttribute( attName );
168            attr.setValue( value );
169    
170            return attr;
171        }
172    
173        /**
174         * Creates a new <code>org.w3c.dom.Text</code>. This is the textual content of an element.
175         * 
176         * @param text
177         *            the attribute name (if <code>null</code>, then context stays empty)
178         * @return new <code>Text</code> instance
179         */
180        private static Text createTextNode( String text ) {
181    
182            String t = "";
183            if ( text != null ) {
184                t = text;
185            }
186    
187            return document.createTextNode( t );
188        }
189    
190        /**
191         * Creates a new <code>org.w3c.dom.Document</code> describing a <code>ViewContext</code>.
192         * 
193         * @param viewContext
194         *            the <code>ViewContext</code> to be exported
195         * @return
196         * @throws ParserConfigurationException
197         *             if an XML parser couldn't be found
198         */
199        public static XMLFragment export( ViewContext viewContext )
200                                throws ParserConfigurationException {
201    
202            document = createDocument();
203            // start appending nodes...
204            appendViewContext( document, viewContext );
205    
206            XMLFragment xml = null;
207            try {
208                xml = new XMLFragment( document, XMLFragment.DEFAULT_URL );
209            } catch ( MalformedURLException neverHappens ) {
210                neverHappens.printStackTrace();
211            }
212    
213            return xml;
214        }
215    
216        /**
217         * Creates a new <code>org.w3c.dom.Document</code> describing a
218         * <code>ViewContextCollection</code>.
219         * 
220         * @param viewContCollec
221         *            the <code>ViewContextCollection</code> to be exported
222         * @return
223         * @throws ParserConfigurationException
224         *             if an XML parser couldn't be found
225         * 
226         */
227        public static Document export( ViewContextCollection viewContCollec )
228                                throws ParserConfigurationException {
229    
230            document = createDocument();
231            // start appending nodes...
232            appendViewContextCollection( document, viewContCollec );
233    
234            return document;
235        }
236    
237        /**
238         * Appends the XML representation of a <code>ViewContext</code> to a <code>Node</code> using
239         * the <code>namespace</code>.
240         * 
241         * @param toNode
242         *            the <code>Node</code> to append the new element to
243         * @param viewContxt
244         *            the <code>ViewContext</code> to be appended as new element
245         * 
246         * 
247         */
248        protected static void appendViewContext( Node toNode, ViewContext viewContxt ) {
249    
250            if ( viewContxt != null ) {
251                Element e = createElement( OGC_CONTEXT_NS, "ViewContext" );
252                Element rootNode = (Element)toNode.appendChild( e );
253                XMLTools.appendNSBinding( rootNode, "sld", SLD_NS );
254                XMLTools.appendNSBinding( rootNode, "xlink", XLINK_NS );
255                XMLTools.appendNSBinding( rootNode, "deegree", D_CONTEXT_NS );
256                XMLTools.appendNSBinding( rootNode, "xsi", XSI_NS );
257    //            XMLTools.appendNSBinding( rootNode, "", OGC_CONTEXT_NS );
258                XMLTools.appendNSDefaultBinding( rootNode, OGC_CONTEXT_NS );
259                
260    //            e.setAttributeNode( createAttribute( "xmlns", OGC_CONTEXT_NS.toString() ) );
261    //            e.setAttributeNode( createAttribute( "xmlns:sld",  ) );
262    //            e.setAttributeNode( createAttribute( "xmlns:xlink", .toString() ) );
263    //            e.setAttributeNode( createAttribute( "xmlns:deegree", D_CONTEXT_NS.toString() ) );
264    //            e.setAttributeNode( createAttribute( "xmlns:", XSI_NS.toString() ) );
265                rootNode.setAttribute( "version", "1.0.0" );
266                rootNode.setAttribute( "id", "viewContext_id" );
267    
268                appendGeneral( rootNode, viewContxt.getGeneral() );
269                appendLayerList( rootNode, viewContxt.getLayerList() );
270    
271                //toNode.appendChild( e );
272            }
273    
274        }
275    
276        /**
277         * Appends the XML representation of a <code>General</code> to a <code>Node</code> using the
278         * <code>namespace</code>.
279         * 
280         * @param toNode
281         *            the <code>Node</code> to append the new element to
282         * @param gen
283         *            the <code>General</code> to be appended as new element
284         * 
285         * contains illegal characters
286         */
287        protected static void appendGeneral( Node toNode, General gen ) {
288    
289            if ( gen != null ) {
290                Element e = createElement( OGC_CONTEXT_NS, "General" );
291                appendWindow( e, gen.getWindow() );
292                appendBoundingBox( e, gen.getBoundingBox() );
293                appendTitle( e, gen.getTitle() );
294                appendAbstract( e, gen.getAbstract() );
295                appendKeywords( e, gen.getKeywords() );
296                appendDescriptionURL( e, gen.getDescriptionURL() );
297                appendLogoURL( e, gen.getLogoURL() );
298                appendContactInformation( e, gen.getContactInformation() );
299                // append deegree-specific extension
300                appendGeneralExtension( e, gen.getExtension() );
301    
302                toNode.appendChild( e );
303            }
304    
305        }
306    
307        /**
308         * Appends the XML representation of a <code>Rectangle</code> to a <code>Node</code> using
309         * the <code>namespace</code>. <p/> Note that the XML representation of a
310         * <code>Rectangle</code> is given by a <code>&lt;Window&gt;</code> element.
311         * 
312         * @param toNode
313         *            the <code>Node</code> to append the new element to
314         * @param r
315         *            the <code>Rectangle</code> to be appended as new element
316         * 
317         * contains illegal characters
318         */
319        protected static void appendWindow( Node toNode, Rectangle r ) {
320    
321            if ( r != null ) {
322                Element window = createElement( OGC_CONTEXT_NS, "Window" );
323    
324                window.setAttribute( "width", String.valueOf( r.width ) );
325                window.setAttribute( "height", String.valueOf( r.height ) );
326    
327                toNode.appendChild( window );
328            }
329    
330        }
331    
332        /**
333         * Appends the XML representation of a <code>GM_Point[]</code> to a <code>Node</code> using
334         * the <code>namespace</code>. <p/> Note that the XML representation of a
335         * <code>GM_Point[]</code> is given by a <code>&lt;BoundingBox&gt;</code> element.
336         * 
337         * @param toNode
338         *            the <code>Node</code> to append the new element to
339         * @param points
340         *            the <code>GM_Point[]</code> to be appended as new element
341         * 
342         * contains illegal characters
343         */
344        protected static void appendBoundingBox( Node toNode, Point[] points ) {
345    
346            if ( points != null && points.length == 2 ) {
347                Element bbox = createElement( OGC_CONTEXT_NS, "BoundingBox" );
348                String srs = "UNKNOWN_SRS";
349                try {
350                    srs = points[0].getCoordinateSystem().getName();
351                } catch ( Exception e ) {
352                    e.printStackTrace();
353                }
354    
355                bbox.setAttributeNode( createAttribute( "SRS", srs ) );
356    
357                bbox.setAttribute( "minx", String.valueOf( points[0].getX() ) );
358                bbox.setAttribute( "miny", String.valueOf( points[0].getY() ) );
359                bbox.setAttribute( "maxx", String.valueOf( points[1].getX() ) );
360                bbox.setAttribute( "maxy", String.valueOf( points[1].getY() ) );
361    
362                toNode.appendChild( bbox );
363    
364            }
365    
366        }
367    
368        /**
369         * Appends the XML representation of a <code>Title</code> to a <code>Node</code> using the
370         * <code>namespace</code>.
371         * 
372         * @param toNode
373         *            the <code>Node</code> to append the new element to
374         * @param title
375         *            the <code>String</code> to be appended as new element
376         * 
377         * contains illegal characters
378         */
379        protected static void appendTitle( Node toNode, String title ) {
380    
381            String t = "";
382            if ( t != null ) {
383                t = title;
384            }
385            Element te = createElement( OGC_CONTEXT_NS, "Title" );
386            te.appendChild( createTextNode( t ) );
387            toNode.appendChild( te );
388    
389        }
390    
391        /**
392         * Appends the XML representation of an <code>Abstract</code> to a <code>Node</code> using
393         * the <code>namespace</code>.
394         * 
395         * @param toNode
396         *            the <code>Node</code> to append the new element to
397         * @param abstr
398         *            the <code>String</code> to be appended as new element
399         * 
400         */
401        protected static void appendAbstract( Node toNode, String abstr ) {
402    
403            if ( abstr != null ) {
404                Element te = createElement( OGC_CONTEXT_NS, "Abstract" );
405                te.appendChild( createTextNode( abstr ) );
406                toNode.appendChild( te );
407            }
408    
409        }
410    
411        /**
412         * Appends the XML representation of an <code>ImageURL</code> to a <code>Node</code> using
413         * the <code>namespace</code>.
414         * 
415         * @param toNode
416         *            the <code>Node</code> to append the new element to
417         * @param logoURL
418         *            the <code>ImageURL</code> to be appended as new element
419         * 
420         * contains illegal characters
421         */
422        protected static void appendLogoURL( Node toNode, ImageURL logoURL ) {
423    
424            if ( logoURL != null && logoURL.getOnlineResource() != null ) {
425                Element e = createElement( OGC_CONTEXT_NS, "LogoURL" );
426                appendOnlineResource( e, logoURL.getOnlineResource() );
427                toNode.appendChild( e );
428            }
429    
430        }
431    
432        /**
433         * Appends the XML representation of a keyword list as a <code>String[]</code> to a
434         * <code>Node</code> using the <code>namespace</code>. <p/> Note that the keywords are
435         * appended to a <code>&lt;KeywordList&gt;</code> element.
436         * 
437         * @param toNode
438         *            the <code>Node</code> to append the new element to
439         * @param keywords
440         *            the <code>ImageURL</code> to be appended as new element
441         * 
442         * contains illegal characters
443         */
444        protected static void appendKeywords( Node toNode, String[] keywords ) {
445    
446            if ( keywords != null ) {
447                Element kWordList = createElement( OGC_CONTEXT_NS, "KeywordList" );
448                for ( int i = 0; i < keywords.length; i++ ) {
449                    Element kw = createElement( OGC_CONTEXT_NS, "Keyword" );
450                    kw.appendChild( createTextNode( keywords[i] ) );
451                    kWordList.appendChild( kw );
452                }
453                toNode.appendChild( kWordList );
454            }
455    
456        }
457    
458        /**
459         * Appends the XML representation of a <code>BaseURL</code>, the <code>DescriptionURL</code>,
460         * to a <code>Node</code> using the <code>namespace</code>.
461         * 
462         * @param toNode
463         *            the <code>Node</code> to append the new element to
464         * @param bURL
465         *            the <code>BaseURL</code> to be appended as new element
466         * 
467         * contains illegal characters
468         */
469        protected static void appendDescriptionURL( Node toNode, BaseURL bURL ) {
470    
471            if ( bURL != null ) {
472                Element du = createElement( OGC_CONTEXT_NS, "DescriptionURL" );
473                String f = bURL.getFormat();
474                if ( f != null ) {
475                    du.setAttribute( "format", f );
476                }
477    
478                URL onlineRes = bURL.getOnlineResource();
479                appendOnlineResource( du, onlineRes );
480    
481                toNode.appendChild( du );
482            }
483    
484        }
485    
486        /**
487         * Appends the XML representation of a <code>URL</code> to a <code>Node</code> as a
488         * <code>&lt;OnlineResource&gt;</code> using the <code>namespace</code>.
489         * 
490         * @param toNode
491         *            the <code>Node</code> to append the new element to
492         * @param onlineRes
493         *            the <code>URL</code> to be appended as new element
494         * 
495         * contains illegal characters
496         */
497        protected static void appendOnlineResource( Node toNode, URL onlineRes ) {
498    
499            if ( onlineRes != null ) {
500                Element or = createElement( OGC_CONTEXT_NS, "OnlineResource" );
501                or.setAttributeNS( XLINK_NS.toASCIIString(), "xlink:type", "simple" );
502    
503                String href = onlineRes.toExternalForm();
504                if ( href != null ) {
505                    // according to OGC WMS 1.3 Testsuite a URL to a service operation
506                    // via HTTPGet must end with '?' or '&'
507                    if ( href.indexOf( '.' ) < 0 ) {
508                        href = OWSUtils.validateHTTPGetBaseURL( href );
509                    }
510                    or.setAttributeNS( XLINK_NS.toASCIIString(), "xlink:href", href );
511                }
512                toNode.appendChild( or );
513            }
514    
515        }
516    
517        /**
518         * Appends the XML representation of a <code>ContactInformation</code> to a <code>Node</code>
519         * using the <code>namespace</code>.
520         * 
521         * @param toNode
522         *            the <code>Node</code> to append the new element to
523         * @param respParty
524         * @param contInfo
525         *            the <code>ContactInformation</code> to be appended as new element
526         * 
527         * contains illegal characters
528         */
529        protected static void appendContactInformation( Node toNode, CitedResponsibleParty respParty ) {
530    
531            if ( respParty != null ) {
532                Element ci = createElement( OGC_CONTEXT_NS, "ContactInformation" );
533    
534                appendContactPersonPrimary( ci, respParty );
535    
536                Element pos = createElement( OGC_CONTEXT_NS, "ContactPosition" );
537                pos.appendChild( createTextNode( respParty.getPositionName()[0] ) );
538                ci.appendChild( pos );
539                ContactInfo[] conInf = respParty.getContactInfo();
540    
541                if ( conInf != null && conInf.length > 0 ) {
542                    appendContactAddress( ci, conInf[0] );
543                    if ( conInf[0].getPhone().getVoice() != null
544                         && conInf[0].getPhone().getVoice().length > 0 ) {
545                        Element e = createElement( OGC_CONTEXT_NS, "ContactVoiceTelephone" );
546                        e.appendChild( createTextNode( conInf[0].getPhone().getVoice()[0] ) );
547                        ci.appendChild( e );
548                    }
549                    if ( conInf[0].getAddress().getElectronicMailAddress() != null
550                         && conInf[0].getAddress().getElectronicMailAddress().length > 0 ) {
551                        Element e = createElement( OGC_CONTEXT_NS, "ContactElectronicMailAddress" );
552                        e.appendChild( createTextNode( conInf[0].getAddress().getElectronicMailAddress()[0] ) );
553                        ci.appendChild( e );
554                    }
555                }
556    
557                toNode.appendChild( ci );
558            }
559    
560        }
561    
562        /**
563         * Appends the XML representation of a <code>ContactPersonPrimary</code> to a
564         * <code>Node</code> using the <code>namespace</code>.
565         * 
566         * @param toNode
567         *            the <code>Node</code> to append the new element to
568         * @param respParty
569         * @param contPersonPrim
570         *            the <code>ContactPersonPrimary</code> to be appended as new element
571         * 
572         * contains illegal characters
573         */
574        protected static void appendContactPersonPrimary( Node toNode, CitedResponsibleParty respParty ) {
575    
576            if ( respParty.getIndividualName() != null && respParty.getIndividualName().length > 0 ) {
577                Element cpp = createElement( OGC_CONTEXT_NS, "ContactPersonPrimary" );
578    
579                Element p = createElement( OGC_CONTEXT_NS, "ContactPerson" );
580    
581                p.appendChild( createTextNode( respParty.getIndividualName()[0] ) );
582                cpp.appendChild( p );
583    
584                Element org = createElement( OGC_CONTEXT_NS, "ContactOrganization" );
585                org.appendChild( createTextNode( respParty.getOrganisationName()[0] ) );
586                cpp.appendChild( org );
587    
588                toNode.appendChild( cpp );
589            }
590    
591        }
592    
593        /**
594         * Appends the XML representation of a <code>ContactAddress</code> to a <code>Node</code>
595         * using the <code>namespace</code>.
596         * 
597         * @param toNode
598         *            the <code>Node</code> to append the new element to
599         * @param ci
600         *            the <code>ContactAddress</code> to be appended as new element
601         * 
602         */
603        protected static void appendContactAddress( Node toNode, ContactInfo ci ) {
604    
605            if ( ci != null ) {
606                Element ca = createElement( OGC_CONTEXT_NS, "ContactAddress" );
607    
608                Element e = createElement( OGC_CONTEXT_NS, "AddressType" );
609                e.appendChild( createTextNode( "postal" ) );
610                ca.appendChild( e );
611    
612                e = createElement( OGC_CONTEXT_NS, "Address" );
613                e.appendChild( createTextNode( ci.getAddress().getDeliveryPoint()[0] ) );
614                ca.appendChild( e );
615    
616                e = createElement( OGC_CONTEXT_NS, "City" );
617                e.appendChild( createTextNode( ci.getAddress().getCity() ) );
618                ca.appendChild( e );
619    
620                e = createElement( OGC_CONTEXT_NS, "StateOrProvince" );
621                e.appendChild( createTextNode( ci.getAddress().getAdministrativeArea() ) );
622                ca.appendChild( e );
623    
624                e = createElement( OGC_CONTEXT_NS, "PostCode" );
625                e.appendChild( createTextNode( ci.getAddress().getPostalCode() ) );
626                ca.appendChild( e );
627    
628                e = createElement( OGC_CONTEXT_NS, "Country" );
629                e.appendChild( createTextNode( ci.getAddress().getCountry() ) );
630                ca.appendChild( e );
631    
632                toNode.appendChild( ca );
633            }
634    
635        }
636    
637        /**
638         * Appends the XML representation of a <code>LayerList</code> to a <code>Node</code> using
639         * the <code>namespace</code>.
640         * 
641         * @param toNode
642         *            the <code>Node</code> to append the new element to
643         * @param lList
644         *            the <code>LayerList</code> to be appended as new element
645         * 
646         */
647        protected static void appendLayerList( Node toNode, LayerList lList ) {
648    
649            if ( lList != null ) {
650                Element list = createElement( OGC_CONTEXT_NS, "LayerList" );
651    
652                Layer[] ls = lList.getLayers();
653                if ( ls != null ) {
654                    for ( int i = 0; i < ls.length; i++ ) {
655                        appendLayer( list, ls[i] );
656                    }
657                }
658                toNode.appendChild( list );
659            }
660    
661        }
662    
663        /**
664         * Appends the XML representation of a <code>Layer</code> to a <code>Node</code> using the
665         * <code>namespace</code>.
666         * 
667         * @param toNode
668         *            the <code>Node</code> to append the new element to
669         * @param layer
670         *            the <code>Layer</code> to be appended as new element
671         * 
672         */
673        protected static void appendLayer( Node toNode, Layer layer ) {
674    
675            if ( layer != null ) {
676                Element le = createElement( OGC_CONTEXT_NS, "Layer" );
677    
678                le.setAttribute( "queryable", stringValue01( layer.isQueryable() ) );
679                le.setAttribute( "hidden", stringValue01( layer.isHidden() ) );
680    
681                appendServer( le, layer.getServer() );
682    
683                Element n = createElement( OGC_CONTEXT_NS, "Name" );
684                n.appendChild( createTextNode( layer.getName() ) );
685                le.appendChild( n );
686    
687                if ( layer.getAbstract() != null ) {
688                    n = createElement( OGC_CONTEXT_NS, "Abstract" );
689                    n.appendChild( createTextNode( layer.getAbstract() ) );
690                    le.appendChild( n );
691                }
692    
693                n = createElement( OGC_CONTEXT_NS, "Title" );
694                n.appendChild( createTextNode( layer.getTitle() ) );
695                le.appendChild( n );
696    
697                if ( layer.getMetadataURL() != null ) {
698                    n = createElement( OGC_CONTEXT_NS, "MetadataURL" );
699                    le.appendChild( n );
700                    appendOnlineResource( n, layer.getMetadataURL().getOnlineResource() );
701                }
702    
703                appendSrs( le, layer.getSrs() );
704                appendFormatList( le, layer.getFormatList() );
705                appendStyleList( le, layer.getStyleList() );
706    
707                appendLayerExtension( le, layer.getExtension() );
708    
709                toNode.appendChild( le );
710            }
711    
712        }
713    
714        /**
715         * Appends the XML representation of a <code>Server</code> to a <code>Node</code> using the
716         * <code>namespace</code>.
717         * 
718         * @param toNode
719         *            the <code>Node</code> to append the new element to
720         * @param server
721         *            the <code>Server</code> to be appended as new element
722         * 
723         */
724        protected static void appendServer( Node toNode, Server server ) {
725    
726            if ( server != null ) {
727                Element serv = createElement( OGC_CONTEXT_NS, "Server" );
728    
729                if ( server.getService() != null ) {
730                    serv.setAttribute( "service", server.getService() );
731                }
732                if ( server.getService() != null ) {
733                    serv.setAttribute( "version", server.getVersion() );
734                }
735                if ( server.getService() != null ) {
736                    serv.setAttribute( "title", server.getTitle() );
737                }
738    
739                appendOnlineResource( serv, server.getOnlineResource() );
740    
741                toNode.appendChild( serv );
742            }
743    
744        }
745    
746        /**
747         * Appends the XML representation of a list of SRSs as a <code>String[]</code> to a
748         * <code>Node</code> using the <code>namespace</code>.
749         * 
750         * @param toNode
751         *            the <code>Node</code> to append the new element to
752         * @param srsList
753         *            the <code>String[]</code> to be appended as new element
754         * 
755         */
756        protected static void appendSrs( Node toNode, String[] srsList ) {
757    
758            if ( srsList != null ) {
759                StringBuffer sBuf = new StringBuffer( 100 );
760                for ( int i = 0; i < srsList.length; i++ ) {
761                    sBuf.append( srsList[i] );
762                    if ( i < srsList.length - 1 )
763                        sBuf.append( ";" );
764    
765                }
766                Element e = createElement( OGC_CONTEXT_NS, "SRS" );
767                e.appendChild( createTextNode( sBuf.toString() ) );
768                toNode.appendChild( e );
769            }
770    
771        }
772    
773        /**
774         * Appends the XML representation of a list of a <code>FormatList</code> to a
775         * <code>Node</code> using the <code>namespace</code>.
776         * 
777         * @param toNode
778         *            the <code>Node</code> to append the new element to
779         * @param formatList
780         *            the <code>FormatList</code> to be appended as new element
781         * 
782         * contains illegal characters
783         */
784        protected static void appendFormatList( Node toNode, FormatList formatList ) {
785    
786            if ( formatList != null ) {
787    
788                Format[] formats = formatList.getFormats();
789                if ( formats != null ) {
790                    Element e = createElement( OGC_CONTEXT_NS, "FormatList" );
791    
792                    for ( int i = 0; i < formats.length; i++ ) {
793                        if ( formats[i] != null ) {
794                            Element f = createElement( OGC_CONTEXT_NS, "Format" );
795                            f.setAttribute( "current", stringValue01( formats[i].isCurrent() ) );
796                            if ( formats[i].getName() != null )
797                                f.appendChild( createTextNode( formats[i].getName() ) );
798                            e.appendChild( f );
799                        }
800                    }
801                    toNode.appendChild( e );
802                }
803            }
804    
805        }
806    
807        /**
808         * Appends the XML representation of a list of a <code>StyleList</code> to a <code>Node</code>
809         * using the <code>namespace</code>.
810         * 
811         * @param toNode
812         *            the <code>Node</code> to append the new element to
813         * @param styleList
814         *            the <code>StyleList</code> to be appended as new element
815         * 
816         */
817        protected static void appendStyleList( Node toNode, StyleList styleList ) {
818    
819            if ( styleList != null ) {
820    
821                Style[] styles = styleList.getStyles();
822                if ( styles != null ) {
823                    Element e = createElement( OGC_CONTEXT_NS, "StyleList" );
824    
825                    for ( int i = 0; i < styles.length; i++ ) {
826                        if ( styles[i] != null ) {
827                            Element s = createElement( OGC_CONTEXT_NS, "Style" );
828                            s.setAttribute( "current", stringValue01( styles[i].isCurrent() ) );
829    
830                            if ( styles[i].getName() != null ) {
831                                Element ne = createElement( OGC_CONTEXT_NS, "Name" );
832                                ne.appendChild( createTextNode( styles[i].getName() ) );
833                                s.appendChild( ne );
834                            }
835                            if ( styles[i].getTitle() != null ) {
836                                Element ne = createElement( OGC_CONTEXT_NS, "Title" );
837                                ne.appendChild( createTextNode( styles[i].getTitle() ) );
838                                s.appendChild( ne );
839                            }
840                            if ( styles[i].getAbstract() != null ) {
841                                Element ne = createElement( OGC_CONTEXT_NS, "Abstract" );
842                                ne.appendChild( createTextNode( styles[i].getAbstract() ) );
843                                s.appendChild( ne );
844                            }
845                            if ( styles[i].getLegendURL() != null
846                                 && styles[i].getLegendURL().getOnlineResource() != null ) {
847                                Element ne = createElement( OGC_CONTEXT_NS, "LegendURL" );
848                                ne.setAttribute( "width",
849                                                 String.valueOf( styles[i].getLegendURL().getWidth() ) );
850                                ne.setAttribute( "height",
851                                                 String.valueOf( styles[i].getLegendURL().getHeight() ) );
852                                ne.setAttribute( "width",
853                                                 String.valueOf( styles[i].getLegendURL().getWidth() ) );
854                                appendOnlineResource( ne, styles[i].getLegendURL().getOnlineResource() );
855                                s.appendChild( ne );
856                            }
857                            e.appendChild( s );
858    
859                        }
860                    }
861                    toNode.appendChild( e );
862                }
863            }
864    
865        }
866    
867        /**
868         * Appends the XML representation of a list of a <code>ViewContextCollection</code> to a
869         * <code>Node</code> using the <code>namespace</code>.
870         * 
871         * @param toNode
872         *            the <code>Node</code> to append the new element to
873         * @param vcc
874         *            the <code>ViewContextCollection</code> to be appended as new element
875         * 
876         */
877        protected static void appendViewContextCollection( Node toNode, ViewContextCollection vcc ) {
878    
879            if ( vcc != null ) {
880                Element e = createElement( OGC_CONTEXT_NS, "ViewContextCollection" );
881                e.setAttributeNode( createAttribute( "xmlns", OGC_CONTEXT_NS.toString() ) );
882                e.setAttributeNode( createAttribute( "xmlns:sld", SLD_NS.toString() ) );
883                e.setAttributeNode( createAttribute( "xmlns:xlink", XLINK_NS.toString() ) );
884                e.setAttributeNode( createAttribute( "xmlns:deegree", D_CONTEXT_NS.toString() ) );
885                e.setAttributeNode( createAttribute( "xmlns:xsi", XSI_NS.toString() ) );
886                e.setAttributeNode( createAttribute( "version", "1.0.0" ) );
887    
888                ViewContextReference[] vcrs = vcc.getViewContextReferences();
889                if ( vcrs != null && vcrs.length > 0 ) {
890                    for ( int i = 0; i < vcrs.length; i++ ) {
891                        if ( vcrs[i] != null ) {
892                            appendContextReference( e, vcrs[i] );
893                        }
894                    }
895                }
896                toNode.appendChild( e );
897            }
898    
899        }
900    
901        /**
902         * Appends the XML representation of a list of a <code>ViewContextReference</code> to a
903         * <code>Node</code> using the <code>namespace</code>. <p/> // TODO implement ID in VCR
904         * 
905         * @param toNode
906         *            the <code>Node</code> to append the new element to
907         * @param vcr
908         *            the <code>ViewContextReference</code> to be appended as new element
909         */
910        protected static void appendContextReference( Node toNode, ViewContextReference vcr ) {
911    
912            if ( vcr != null ) {
913                Element e = createElement( OGC_CONTEXT_NS, "ViewContextReference" );
914    
915                e.setAttributeNode( createAttribute( "version", "1.0.0" ) );
916    
917                String id = vcr.getTitle().replace( ' ', '_' ).toLowerCase();
918                e.setAttributeNode( createAttribute( "id", id ) );
919    
920                Element t = createElement( OGC_CONTEXT_NS, "Title" );
921                t.appendChild( createTextNode( vcr.getTitle() ) );
922                e.appendChild( t );
923    
924                if ( vcr.getContextURL() != null ) {
925                    Element c = createElement( OGC_CONTEXT_NS, "ViewContextURL" );
926                    appendOnlineResource( c, vcr.getContextURL() );
927                    e.appendChild( c );
928                }
929                toNode.appendChild( e );
930            }
931    
932        }
933    
934        /**
935         * Creates a String representation ("0" or "1") of a boolean value.
936         * 
937         * @param value
938         *            the input value
939         * @return "0" or "1" if value is true or false, respectively
940         */
941        private static final String stringValue01( boolean value ) {
942            return value ? "1" : "0";
943        }
944    
945        // ***********************************************************************
946        // BEGIN Deegree specific methods
947        // ***********************************************************************
948    
949        /**
950         * Appends the XML representation of a list of a <code>GeneralExtension</code> to a
951         * <code>Node</code> using the <code>namespace</code>.
952         * 
953         * @param toNode
954         *            the <code>Node</code> to append the new element to
955         * @param genExt
956         *            the <code>GeneralExtension</code> to be appended as new element
957         * @param namespace
958         *            the namespace of the new element
959         * 
960         */
961        protected static void appendGeneralExtension( Node toNode, GeneralExtension genExt ) {
962    
963            if ( genExt != null ) {
964                Element e = createElement( OGC_CONTEXT_NS, "Extension" );
965                Element a = createElement( D_CONTEXT_NS, "deegree:Mode" );
966                a.appendChild( createTextNode( genExt.getMode() ) );
967                e.appendChild( a );
968    
969                appendAuthentificationSettings( e, genExt.getAuthentificationSettings() );
970                appendIOSettings( e, genExt.getIOSettings() );
971                appendFrontend( e, genExt.getFrontend() );
972                appendMapParameter( e, genExt.getMapParameter() );
973                
974                appendLayerTree( e, genExt.getLayerTreeRoot() );
975    
976                toNode.appendChild( e );
977            }
978    
979        }
980        
981        protected static void appendLayerTree( Element e, org.deegree.portal.context.Node layerTreeRoot ) {
982            Element layerTree = createElement( D_CONTEXT_NS, "deegree:LayerTree" );
983    
984            //TODO FIXME 
985            Element sTree = createElement( D_CONTEXT_NS, "deegree:Node" );
986            sTree.setAttributeNode( createAttribute( "id", "1" ) );
987            sTree.setAttributeNode( createAttribute( "title", "Root" ) );
988            layerTree.appendChild( sTree );
989            
990            org.deegree.portal.context.Node[] nodes = layerTreeRoot.getNodes();
991            for ( int i = 0; i < nodes.length; i++ ) {
992                Element n = createElement( D_CONTEXT_NS, "deegree:Node" );
993                n.setAttributeNode( createAttribute( "id", String.valueOf( nodes[i].getId() ) ) );
994                n.setAttributeNode( createAttribute( "title", nodes[i].getTitle() ) );
995    
996                sTree.appendChild( n );
997            }
998            e.appendChild( layerTree );
999        }
1000    
1001        /**
1002         * @param toNode
1003         * @param settings
1004         */
1005        protected static void appendAuthentificationSettings( Node toNode,
1006                                                              AuthentificationSettings settings ) {
1007    
1008            if ( settings != null ) {
1009                Element e = createElement( D_CONTEXT_NS, "deegree:AuthentificationSettings" );
1010                Element ee = createElement( D_CONTEXT_NS, "deegree:AuthentificationService" );
1011                appendOnlineResource( ee, settings.getAuthentificationURL().getOnlineResource() );
1012                e.appendChild( ee );
1013                toNode.appendChild( e );
1014            }
1015    
1016        }
1017    
1018        /**
1019         * Appends the XML representation of a list of a <code>IOSettings</code> to a
1020         * <code>Node</code> using the <code>namespace</code>.
1021         * 
1022         * @param toNode
1023         *            the <code>Node</code> to append the new element to
1024         * @param ioSetts
1025         *            the <code>IOSettings</code> to be appended as new element
1026         * @param namespace
1027         *            the namespace of the new element
1028         * 
1029         */
1030        protected static void appendIOSettings( Node toNode, IOSettings ioSetts ) {
1031    
1032            if ( ioSetts != null ) {
1033                Element e = createElement( D_CONTEXT_NS, "deegree:IOSettings" );
1034    
1035                // TODO: ioSetts.getTempDirectory() , inexistent till now
1036                /*
1037                 * if(ioSetts.getRootDirectory() != null ){ Element rd =
1038                 * createElement(namespace,"deegree:TempDirectory"); rd.appendChild( createTextNode(
1039                 * ioSetts.getRootDirectory() + "temp")); e.appendChild(rd); }
1040                 */
1041    
1042                appendDirectoryAccess( e, ioSetts.getTempDirectory(), "deegree:TempDirectory" );
1043                // appendDirectoryAccess( e, ioSetts.getDownloadDirectory(), "deegree:TempDirectory" );
1044                appendDirectoryAccess( e, ioSetts.getDownloadDirectory(), "deegree:DownloadDirectory" );
1045                appendDirectoryAccess( e, ioSetts.getSLDDirectory(), "deegree:SLDDirectory" );
1046                appendDirectoryAccess( e, ioSetts.getPrintDirectory(), "deegree:PrintDirectory" );
1047    
1048                toNode.appendChild( e );
1049            }
1050    
1051        }
1052    
1053        /**
1054         * Appends the XML representation of a list of a <code>DirectoryAccess</code> to a
1055         * <code>Node</code> using the <code>namespace</code>.
1056         * 
1057         * @param toNode
1058         *            the <code>Node</code> to append the new element to
1059         * @param dirAcc
1060         *            the <code>DirectoryAccess</code> to be appended as new element
1061         * @param dirName
1062         * @param namespace
1063         *            the namespace of the new element
1064         * 
1065         */
1066        protected static void appendDirectoryAccess( Node toNode, DirectoryAccess dirAcc, String dirName ) {
1067    
1068            if ( dirAcc != null ) {
1069                Element d = createElement( D_CONTEXT_NS, dirName );
1070                if ( dirAcc.getDirectoryName() != null ) {
1071                    Element a = createElement( D_CONTEXT_NS, "deegree:Name" );
1072                    a.appendChild( createTextNode( dirAcc.getDirectoryName() ) );
1073                    d.appendChild( a );
1074    
1075                }
1076                if ( dirAcc.getOnlineResource() != null ) {
1077                    Element a = createElement( D_CONTEXT_NS, "deegree:Access" );
1078                    appendOnlineResource( a, dirAcc.getOnlineResource() );
1079                    d.appendChild( a );
1080                }
1081                toNode.appendChild( d );
1082            }
1083    
1084        }
1085    
1086        /**
1087         * Appends the XML representation of a list of a <code>Frontend</code> to a <code>Node</code>
1088         * using the <code>namespace</code>.
1089         * 
1090         * @param toNode
1091         *            the <code>Node</code> to append the new element to
1092         * @param fEnd
1093         *            the <code>Frontend</code> to be appended as new element
1094         * @param namespace
1095         *            the namespace of the new element
1096         * 
1097         */
1098        protected static void appendFrontend( Node toNode, Frontend fEnd ) {
1099    
1100            if ( fEnd != null ) {
1101                Element e = createElement( D_CONTEXT_NS, "deegree:Frontend" );
1102    
1103                e.setAttribute( "scope", "JSP" );
1104                if ( fEnd.getController() != null ) {
1105                    Element c = createElement( D_CONTEXT_NS, "deegree:Controller" );
1106                    c.appendChild( createTextNode( fEnd.getController() ) );
1107                    e.appendChild( c );
1108                }
1109                if ( ( (JSPFrontend) fEnd ).getStyle() != null ) {
1110                    Element c = createElement( D_CONTEXT_NS, "deegree:Style" );
1111                    c.appendChild( createTextNode( ( (JSPFrontend) fEnd ).getStyle() ) );
1112                    e.appendChild( c );
1113                }
1114                if ( ( (JSPFrontend) fEnd ).getHeader() != null ) {
1115                    Element c = createElement( D_CONTEXT_NS, "deegree:Header" );
1116                    c.appendChild( createTextNode( ( (JSPFrontend) fEnd ).getHeader() ) );
1117                    e.appendChild( c );
1118                }
1119                if ( ( (JSPFrontend) fEnd ).getFooter() != null ) {
1120                    Element c = createElement( D_CONTEXT_NS, "deegree:Footer" );
1121                    c.appendChild( createTextNode( ( (JSPFrontend) fEnd ).getFooter() ) );
1122                    e.appendChild( c );
1123                }
1124    
1125                appendCommonJS( e, ( (JSPFrontend) fEnd ).getCommonJS() );
1126    
1127                appendButtons( e, ( (JSPFrontend) fEnd ).getButtons() );
1128    
1129                appendGUIArea( e, fEnd.getNorth(), "deegree:North" );
1130                appendGUIArea( e, fEnd.getWest(), "deegree:West" );
1131                appendGUIArea( e, fEnd.getCenter(), "deegree:Center" );
1132                appendGUIArea( e, fEnd.getEast(), "deegree:East" );
1133                appendGUIArea( e, fEnd.getSouth(), "deegree:South" );
1134    
1135                toNode.appendChild( e );
1136            }
1137    
1138        }
1139    
1140        /**
1141         * Appends the XML representation of a list of a <code>String[]</code> to a <code>Node</code>
1142         * using the <code>namespace</code>.
1143         * 
1144         * @param toNode
1145         *            the <code>Node</code> to append the new element to
1146         * @param commonJS
1147         *            the <code>String[]</code> to be appended as new element
1148         * @param namespace
1149         *            the namespace of the new element
1150         * 
1151         */
1152        protected static void appendCommonJS( Node toNode, String[] commonJS ) {
1153    
1154            if ( commonJS != null ) {
1155                Element c = createElement( D_CONTEXT_NS, "deegree:CommonJS" );
1156    
1157                for ( int i = 0; i < commonJS.length; i++ ) {
1158                    if ( commonJS[i] != null ) {
1159                        Element n = createElement( D_CONTEXT_NS, "deegree:Name" );
1160                        n.appendChild( createTextNode( commonJS[i] ) );
1161                        c.appendChild( n );
1162                    }
1163                }
1164                toNode.appendChild( c );
1165            }
1166    
1167        }
1168    
1169        /**
1170         * Appends the XML representation of a list of a <code>String</code> to a <code>Node</code>
1171         * using the <code>namespace</code>. // TODO
1172         * 
1173         * @param toNode
1174         *            the <code>Node</code> to append the new element to
1175         * @param buttons
1176         *            the <code>String</code> to be appended as new element
1177         * @param namespace
1178         *            the namespace of the new element
1179         * 
1180         */
1181        protected static void appendButtons( Node toNode, String buttons ) {
1182    
1183            if ( buttons != null ) {
1184                Element b = createElement( D_CONTEXT_NS, "deegree:Buttons" );
1185                b.appendChild( createTextNode( buttons ) );
1186    
1187                toNode.appendChild( b );
1188            }
1189    
1190        }
1191    
1192        /**
1193         * Appends the XML representation of a list of a <code>GUIArea</code> to a <code>Node</code>
1194         * using the <code>namespace</code>.
1195         * 
1196         * @param toNode
1197         *            the <code>Node</code> to append the new element to
1198         * @param guiArea
1199         *            the <code>GUIArea</code> to be appended as new element
1200         * @param name
1201         * @param namespace
1202         *            the namespace of the new element
1203         * 
1204         */
1205        protected static void appendGUIArea( Node toNode, GUIArea guiArea, String name ) {
1206    
1207            if ( guiArea != null ) {
1208                Element e = createElement( D_CONTEXT_NS, name );
1209                e.setAttribute( "hidden", String.valueOf( guiArea.isHidden() ) );
1210    
1211                Module[] mods = guiArea.getModules();
1212                if ( mods != null ) {
1213                    for ( int i = 0; i < mods.length; i++ ) {
1214                        if ( mods[i] != null ) {
1215                            appendModule( e, mods[i] );
1216                        }
1217                    }
1218                }
1219    
1220                toNode.appendChild( e );
1221            }
1222    
1223        }
1224    
1225        /**
1226         * Appends the XML representation of a list of a <code>GUIArea</code> to a <code>Node</code>
1227         * using the <code>namespace</code>.
1228         * 
1229         * @param toNode
1230         *            the <code>Node</code> to append the new element to
1231         * @param mod
1232         * @param namespace
1233         *            the namespace of the new element
1234         * 
1235         */
1236        protected static void appendModule( Node toNode, Module mod ) {
1237    
1238            if ( mod != null ) {
1239                Element m = createElement( D_CONTEXT_NS, "deegree:Module" );
1240                m.setAttribute( "hidden", String.valueOf( mod.isHidden() ) );
1241    
1242                m.setAttribute( "type", mod.getType() );
1243                m.setAttribute( "width", String.valueOf( mod.getWidth() ) );
1244                m.setAttribute( "height", String.valueOf( mod.getHeight() ) );
1245                m.setAttribute( "scrolling", String.valueOf( mod.getScrolling() ) );
1246    
1247                Element n = createElement( D_CONTEXT_NS, "deegree:Name" );
1248                n.appendChild( createTextNode( mod.getName() ) );
1249                m.appendChild( n );
1250    
1251                n = createElement( D_CONTEXT_NS, "deegree:Content" );
1252                n.appendChild( createTextNode( mod.getContent() ) );
1253                m.appendChild( n );
1254    
1255                appendModuleJSList( m, mod.getModuleJSList() );
1256                appendModuleConfiguration( m, mod.getModuleConfiguration() );
1257                appendParameterList( m, mod.getParameter() );
1258    
1259                toNode.appendChild( m );
1260            }
1261    
1262        }
1263    
1264        /**
1265         * Appends the XML representation of a list of a <code>ModuleConfiguration</code> to a
1266         * <code>Node</code> using the <code>namespace</code>.
1267         * 
1268         * @param toNode
1269         *            the <code>Node</code> to append the new element to
1270         * @param modConf
1271         *            the <code>ModuleConfiguration</code> to be appended as new element
1272         * @param namespace
1273         *            the namespace of the new element
1274         * 
1275         */
1276        protected static void appendModuleConfiguration( Node toNode, ModuleConfiguration modConf ) {
1277    
1278            if ( modConf != null && modConf.getOnlineResource() != null ) {
1279                Element e = createElement( D_CONTEXT_NS, "deegree:ModuleConfiguration" );
1280                appendOnlineResource( e, modConf.getOnlineResource() );
1281                toNode.appendChild( e );
1282            }
1283    
1284        }
1285    
1286        /**
1287         * Appends the XML representation of a list of a <code>ParameterList</code> to a
1288         * <code>Node</code> using the <code>namespace</code>.
1289         * 
1290         * @param toNode
1291         *            the <code>Node</code> to append the new element to
1292         * @param parList
1293         *            the <code>ParameterList</code> to be appended as new element
1294         * 
1295         */
1296        protected static void appendParameterList( Node toNode, ParameterList parList ) {
1297    
1298            if ( parList != null && parList.getParameters().length > 0 ) {
1299    
1300                Element e = createElement( D_CONTEXT_NS, "deegree:ParameterList" );
1301    
1302                Parameter[] pars = parList.getParameters();
1303                for ( int i = 0; i < pars.length; i++ ) {
1304                    if ( pars[i] != null ) {
1305                        Element p = createElement( D_CONTEXT_NS, "deegree:Parameter" );
1306    
1307                        Element n = createElement( D_CONTEXT_NS, "deegree:Name" );
1308                        String name = pars[i].getName();
1309                        // name = name.substring(0,name.indexOf(':'));
1310                        n.appendChild( createTextNode( name ) );
1311                        p.appendChild( n );
1312    
1313                        n = createElement( D_CONTEXT_NS, "deegree:Value" );
1314                        n.appendChild( createTextNode( pars[i].getValue().toString() ) );
1315                        p.appendChild( n );
1316    
1317                        e.appendChild( p );
1318                    }
1319                }
1320                toNode.appendChild( e );
1321            }
1322    
1323        }
1324    
1325        /**
1326         * Appends the XML representation of a list of a <code>MapParameter</code> to a
1327         * <code>Node</code> using the <code>namespace</code>.
1328         * 
1329         * @param toNode
1330         *            the <code>Node</code> to append the new element to
1331         * @param mapPar
1332         *            the <code>MapParameter</code> to be appended as new element
1333         * @param namespace
1334         *            the namespace of the new element
1335         * 
1336         */
1337        protected static void appendMapParameter( Node toNode, MapParameter mapPar ) {
1338    
1339            if ( mapPar != null ) {
1340                Element e = createElement( D_CONTEXT_NS, "deegree:MapParameter" );
1341    
1342                Element f = createElement( D_CONTEXT_NS, "deegree:OfferedInfoFormats" );
1343                appendFormats( f, mapPar.getOfferedInfoFormats() );
1344                e.appendChild( f );
1345    
1346                appendMapOperationFactors( e, mapPar.getOfferedZoomFactors(),
1347                                           "deegree:OfferedZoomFactor" );
1348                appendMapOperationFactors( e, mapPar.getOfferedPanFactors(), "deegree:OfferedPanFactor" );
1349    
1350                Element minScale = createElement( D_CONTEXT_NS, "deegree:MinScale" );
1351                minScale.appendChild( createTextNode( String.valueOf( mapPar.getMinScale() ) ) );
1352                e.appendChild( minScale );
1353    
1354                Element maxScale = createElement( D_CONTEXT_NS, "deegree:MaxScale" );
1355                maxScale.appendChild( createTextNode( String.valueOf( mapPar.getMaxScale() ) ) );
1356                e.appendChild( maxScale );
1357    
1358                toNode.appendChild( e );
1359            }
1360    
1361        }
1362    
1363        /**
1364         * Appends the XML representation of a list of a <code>Format[]</code> to a <code>Node</code>
1365         * using the <code>namespace</code>.
1366         * 
1367         * @param toNode
1368         *            the <code>Node</code> to append the new element to
1369         * @param formats
1370         *            the <code>Format[]</code> to be appended as new element
1371         * @param namespace
1372         *            the namespace of the new element
1373         * 
1374         */
1375        protected static void appendFormats( Node toNode, Format[] formats ) {
1376    
1377            if ( formats != null ) {
1378                for ( int i = 0; i < formats.length; i++ ) {
1379                    if ( formats[i] != null ) {
1380                        Element f = createElement( D_CONTEXT_NS, "deegree:Format" );
1381    
1382                        // TODO is current or selected?
1383                        if ( formats[i].isCurrent() ) {
1384                            f.setAttribute( "selected", String.valueOf( formats[i].isCurrent() ) );
1385                        }
1386    
1387                        f.appendChild( createTextNode( formats[i].getName() ) );
1388                        toNode.appendChild( f );
1389                    }
1390                }
1391            }
1392    
1393        }
1394    
1395        /**
1396         * Appends the XML representation of a list of a <code>MapOperationFactor</code> to a
1397         * <code>Node</code> using the <code>namespace</code>.
1398         * 
1399         * @param toNode
1400         *            the <code>Node</code> to append the new element to
1401         * @param mapOpFac
1402         *            the <code>MapOperationFactor</code> to be appended as new element
1403         * @param opName
1404         * @param namespace
1405         *            the namespace of the new element
1406         * 
1407         */
1408        protected static void appendMapOperationFactors( Node toNode, MapOperationFactor[] mapOpFac,
1409                                                         String opName ) {
1410    
1411            if ( mapOpFac != null ) {
1412                for ( int i = 0; i < mapOpFac.length; i++ ) {
1413                    if ( mapOpFac[i] != null ) {
1414    
1415                        Element mof = createElement( D_CONTEXT_NS, opName );
1416                        Element f = createElement( D_CONTEXT_NS, "deegree:Factor" );
1417                        f.appendChild( createTextNode( String.valueOf( mapOpFac[i].getFactor() ) ) );
1418    
1419                        if ( mapOpFac[i].isSelected() ) {
1420                            f.setAttribute( "selected", String.valueOf( mapOpFac[i].isSelected() ) );
1421                        }
1422    
1423                        // TODO isFree ???
1424    
1425                        mof.appendChild( f );
1426                        toNode.appendChild( mof );
1427                    }
1428                }
1429            }
1430    
1431        }
1432    
1433        /**
1434         * Appends the XML representation of a list of a <code>LayerExtension</code> to a
1435         * <code>Node</code> using the <code>namespace</code>.
1436         * 
1437         * @param toNode
1438         *            the <code>Node</code> to append the new element to
1439         * @param layExt
1440         *            the <code>LayerExtension</code> to be appended as new element
1441         * 
1442         */
1443        protected static void appendLayerExtension( Node toNode, LayerExtension layExt ) {
1444    
1445            if ( layExt != null ) {
1446                Element e = createElement( OGC_CONTEXT_NS, "Extension" );
1447    
1448                appendDataService( e, layExt.getDataService() );
1449    
1450                Element g = createElement( D_CONTEXT_NS, "deegree:MasterLayer" );
1451                g.appendChild( createTextNode( String.valueOf( layExt.isMasterLayer() ) ) );
1452                e.appendChild( g );
1453    
1454                g = createElement( D_CONTEXT_NS, "deegree:ScaleHint" );
1455                g.setAttribute( "min", "" + layExt.getMinScaleHint() );
1456                g.setAttribute( "max", "" + layExt.getMaxScaleHint() );
1457                e.appendChild( g );
1458                
1459                g = createElement( D_CONTEXT_NS, "deegree:parentNodeId" );
1460                g.appendChild( createTextNode( String.valueOf( layExt.getParentNodeId() ) ) );
1461                e.appendChild( g );
1462    
1463                g = createElement( D_CONTEXT_NS, "deegree:SelectedForQuery" );
1464                g.appendChild( createTextNode( String.valueOf( layExt.isSelectedForQuery() ) ) );
1465                e.appendChild( g );
1466    
1467                g = createElement( D_CONTEXT_NS, "deegree:UseAuthentication" );
1468                // System.out.println(layExt.getAuthentication());
1469                if ( layExt.getAuthentication() == LayerExtension.SESSIONID ) {
1470                    g.appendChild( createTextNode( "sessionID" ) );
1471                } else if ( layExt.getAuthentication() == LayerExtension.USERPASSWORD ) {
1472                    g.appendChild( createTextNode( "user/password" ) );
1473                } else if ( layExt.getAuthentication() == LayerExtension.NONE ) {
1474                    g.appendChild( createTextNode( "none" ) );
1475                }
1476                e.appendChild( g );
1477    
1478                toNode.appendChild( e );
1479            }
1480    
1481        }
1482    
1483        /**
1484         * Appends the XML representation of a list of a <code>DataService</code> to a
1485         * <code>Node</code> using the <code>namespace</code>.
1486         * 
1487         * @param toNode
1488         *            the <code>Node</code> to append the new element to
1489         * @param dataServ
1490         *            the <code>DataService</code> to be appended as new element
1491         * 
1492         */
1493        protected static void appendDataService( Node toNode, DataService dataServ ) {
1494    
1495            if ( dataServ != null ) {
1496                Element e = createElement( D_CONTEXT_NS, "deegree:DataService" );
1497    
1498                if ( dataServ.getServer() != null ) {
1499                    appendServer( e, dataServ.getServer() );
1500                }
1501                String geoType = dataServ.getGeometryType();
1502                if ( geoType != null ) {
1503                    Element g = createElement( D_CONTEXT_NS, "deegree:GeometryType" );
1504                    g.appendChild( createTextNode( dataServ.getGeometryType() ) );
1505                    e.appendChild( g );
1506                }
1507                String featType = dataServ.getFeatureType();
1508                if ( featType != null ) {
1509                    Element g = createElement( D_CONTEXT_NS, "deegree:FeatureType" );
1510                    g.appendChild( createTextNode( featType ) );
1511                    e.appendChild( g );
1512                }
1513    
1514                toNode.appendChild( e );
1515            }
1516    
1517        }
1518    
1519        /**
1520         * Appends the XML representation of a list of a <code>ParameterList</code> to a
1521         * <code>Node</code> using the <code>namespace</code>.
1522         * 
1523         * @param toNode
1524         *            the <code>Node</code> to append the new element to
1525         * @param modJSList
1526         *            the <code>modJSList</code> to be appended as new element
1527         * @param namespace
1528         *            the namespace of the new element
1529         * 
1530         */
1531        protected static void appendModuleJSList( Node toNode, String[] modJSList ) {
1532    
1533            if ( modJSList != null && modJSList.length > 0 ) {
1534    
1535                for ( int i = 0; i < modJSList.length; i++ ) {
1536                    if ( modJSList[i] != null ) {
1537                        Element p = createElement( D_CONTEXT_NS, "deegree:ModuleJS" );
1538                        p.appendChild( createTextNode( modJSList[i] ) );
1539    
1540                        toNode.appendChild( p );
1541                    }
1542                }
1543            }
1544    
1545        }
1546    
1547    }