001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/context/MapModelDocument.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.portal.context;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    
041    import org.deegree.framework.xml.NamespaceContext;
042    import org.deegree.framework.xml.XMLFragment;
043    import org.deegree.framework.xml.XMLParsingException;
044    import org.deegree.framework.xml.XMLTools;
045    import org.deegree.ogcbase.CommonNamespaces;
046    import org.w3c.dom.Element;
047    
048    /**
049     * TODO add class documentation here
050     * 
051     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
052     * @author last edited by: $Author: apoth $
053     * 
054     * @version $Revision: 20771 $, $Date: 2009-11-12 18:37:45 +0100 (Do, 12 Nov 2009) $
055     */
056    class MapModelDocument extends XMLFragment {
057    
058        private static final long serialVersionUID = -248476317451177157L;
059    
060        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
061    
062        /**
063         * 
064         * @param root
065         */
066        MapModelDocument( Element root ) {
067            super( root );
068        }
069    
070        /**
071         * 
072         * @return MapModel including hierarchyly arranged layers and layer groups
073         * @throws XMLParsingException
074         */
075        MapModel parseMapModel()
076                                throws XMLParsingException {
077            MapModel mapModel = new MapModel();
078            List<LayerGroup> layerGroups = new ArrayList<LayerGroup>();
079            List<Element> elements = XMLTools.getElements( getRootElement(), "./dgcntxt:LayerGroup", nsContext );
080            for ( Element element : elements ) {
081                String identifier = XMLTools.getRequiredNodeAsString( element, "./@identifier", nsContext );
082                String title = XMLTools.getRequiredNodeAsString( element, "./@title", nsContext );
083                String tmp = XMLTools.getNodeAsString( element, "./@hidden", nsContext, "false" );
084                boolean hidden = "true".equals( tmp ) || "1".equals( tmp );
085                tmp = XMLTools.getNodeAsString( element, "./@expanded", nsContext, "false" );
086                boolean expanded = "true".equals( tmp ) || "1".equals( tmp );
087                LayerGroup layerGroup = new LayerGroup( identifier, title, hidden, expanded, null, mapModel );
088                layerGroups.add( layerGroup );
089                appendMapModelEntries( element, layerGroup, mapModel );
090            }
091            mapModel.setLayerGroups( layerGroups );
092    
093            return mapModel;
094        }
095    
096        /**
097         * @param lgElement
098         * @param layerGroup
099         * @param mapModel
100         * @throws XMLParsingException
101         */
102        private void appendMapModelEntries( Element lgElement, LayerGroup layerGroup, MapModel mapModel )
103                                throws XMLParsingException {
104            List<Element> elements = XMLTools.getElements( lgElement, "./dgcntxt:LayerGroup | ./dgcntxt:Layer", nsContext );
105            for ( Element element : elements ) {
106                if ( element.getLocalName().equals( "Layer" ) ) {
107                    String layerId = XMLTools.getRequiredNodeAsString( element, "./@layerId", nsContext );
108                    layerGroup.addLayer(  new MMLayer( layerId, layerGroup, mapModel, null ) );
109                } else {
110                    String tmp = XMLTools.getNodeAsString( element, "./@hidden", nsContext, "false" );                
111                    boolean hidden = "true".equals( tmp ) || "1".equals( tmp );
112                    tmp = XMLTools.getNodeAsString( element, "./@expanded", nsContext, "false" );
113                    boolean expanded = "true".equals( tmp ) || "1".equals( tmp );
114                    String title = XMLTools.getRequiredNodeAsString( element, "./@title", nsContext );
115                    String identifier = XMLTools.getRequiredNodeAsString( element, "./@identifier", nsContext );
116                    LayerGroup lg = new LayerGroup( identifier, title, hidden, expanded, layerGroup, mapModel );
117                    layerGroup.addLayerGroup( lg );
118                    appendMapModelEntries( element, lg, mapModel );
119                }
120            }
121            
122        }
123    }