001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/metadata/iso19115/ISO19115Document.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.model.metadata.iso19115;
045    
046    import java.net.MalformedURLException;
047    import java.net.URI;
048    import java.net.URL;
049    import java.util.ArrayList;
050    import java.util.List;
051    
052    import org.deegree.datatypes.Code;
053    import org.deegree.framework.xml.XMLFragment;
054    import org.deegree.framework.xml.XMLParsingException;
055    import org.deegree.framework.xml.XMLTools;
056    import org.deegree.ogcbase.CommonNamespaces;
057    import org.w3c.dom.Element;
058    
059    /**
060     * Parser class that can parse various elements defined in the OWS subset of the ISO 19115
061     * specification.
062     * 
063     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
064     * @author last edited by: $Author: apoth $
065     * 
066     * @version 2.0, $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $
067     * 
068     * @since 2.0
069     */
070    
071    public class ISO19115Document extends XMLFragment {
072    
073        private static final long serialVersionUID = -5536802360612196021L;
074    
075        private static final String POWS = CommonNamespaces.OWS_PREFIX + ":";
076    
077        private static final String PXLINK = CommonNamespaces.XLINK_PREFIX + ":";
078    
079        /**
080         * @param root
081         *            an element of type ows:ResponsiblePartySubsetType
082         * @return the data object
083         * @throws XMLParsingException
084         */
085        public CitedResponsibleParty parseCitedResponsibleParty( Element root )
086                                throws XMLParsingException {
087            String individualName = XMLTools.getNodeAsString( root, POWS + "IndividualName", nsContext, null );
088    
089            String positionName = XMLTools.getNodeAsString( root, POWS + "PositionName", nsContext, null );
090    
091            Element roleElement = (Element) XMLTools.getNode( root, POWS + "Role", nsContext );
092            RoleCode role = null;
093            if ( roleElement != null )
094                role = parseRoleCode( roleElement );
095    
096            Element contactElement = (Element) XMLTools.getNode( root, POWS + "ContactInfo", nsContext );
097            ContactInfo contactInfo = null;
098            if ( contactElement != null )
099                contactInfo = parseContactInfo( contactElement );
100    
101            // why Lists/Arrays are necessary here is beyond my knowledge
102            List<String> name = new ArrayList<String>();
103            name.add( individualName );
104            List<String> pName = new ArrayList<String>();
105            pName.add( positionName );
106            List<RoleCode> roles = new ArrayList<RoleCode>();
107            roles.add( role );
108            List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
109            contactInfos.add( contactInfo );
110    
111            CitedResponsibleParty result = new CitedResponsibleParty( contactInfos, name, null, pName, roles );
112            return result;
113        }
114    
115        /**
116         * @param root
117         *            the ContactInfo element
118         * @return the <code>ContactInfo</code> data object
119         * @throws XMLParsingException
120         */
121        public ContactInfo parseContactInfo( Element root )
122                                throws XMLParsingException {
123            Element phoneElement = (Element) XMLTools.getNode( root, POWS + "Phone", nsContext );
124            Phone phone = null;
125            if ( phoneElement != null )
126                phone = parsePhone( phoneElement );
127    
128            Address address = null;
129            Element addressElement = (Element) XMLTools.getNode( root, POWS + "Address", nsContext );
130            if ( addressElement != null )
131                address = parseAddress( addressElement );
132    
133            OnlineResource onlineResource = null;
134            Element onlineResourceElement = (Element) XMLTools.getNode( root, POWS + "OnlineResource", nsContext );
135            if ( onlineResourceElement != null )
136                onlineResource = parseOnlineResource( onlineResourceElement );
137    
138            String hoursOfService = XMLTools.getNodeAsString( root, POWS + "HoursOfService", nsContext, null );
139            String contactInstructions = XMLTools.getNodeAsString( root, POWS + "ContactInstructions", nsContext, null );
140    
141            ContactInfo result = new ContactInfo( address, contactInstructions, hoursOfService, onlineResource, phone );
142            return result;
143        }
144    
145        /**
146         * @param root
147         *            the Address element
148         * @return the <code>Address</code> data object
149         * @throws XMLParsingException
150         */
151        public Address parseAddress( Element root )
152                                throws XMLParsingException {
153            String[] deliveryPoint = XMLTools.getNodesAsStrings( root, POWS + "DeliveryPoint", nsContext );
154            String city = XMLTools.getNodeAsString( root, POWS + "City", nsContext, null );
155            String administrativeArea = XMLTools.getNodeAsString( root, POWS + "AdministrativeArea", nsContext, null );
156            String postalCode = XMLTools.getNodeAsString( root, POWS + "PostalCode", nsContext, null );
157            String country = XMLTools.getNodeAsString( root, POWS + "Country", nsContext, null );
158            String[] emails = XMLTools.getNodesAsStrings( root, POWS + "ElectronicMailAddress", nsContext );
159    
160            Address result = new Address( administrativeArea, city, country, deliveryPoint, emails, postalCode );
161            return result;
162        }
163    
164        /**
165         * @param root
166         *            the Phone element
167         * @return a <code>Phone</code> data object
168         * @throws XMLParsingException
169         */
170        public Phone parsePhone( Element root )
171                                throws XMLParsingException {
172            String[] voice = XMLTools.getNodesAsStrings( root, POWS + "Voice", nsContext );
173    
174            String[] facsimile = XMLTools.getNodesAsStrings( root, POWS + "Facsimile", nsContext );
175    
176            Phone result = new Phone( facsimile, voice );
177            return result;
178        }
179    
180        /**
181         * @param root
182         *            the element containing the xlink attributes
183         * @return the <code>OnlineResource</data> data object
184         * @throws XMLParsingException
185         */
186        public OnlineResource parseOnlineResource( Element root )
187                                throws XMLParsingException {
188            // This is just a preview version, not sure how to handle all the xlink attributes
189            // correctly.
190    
191            URL href = null;
192            String url = null;
193    
194            try {
195                url = XMLTools.getNodeAsString( root, "@" + PXLINK + "href", nsContext, null );
196                if ( url != null )
197                    href = new URL( url );
198            } catch ( MalformedURLException e ) {
199                throw new XMLParsingException( "'" + url + "' is not a valid URL." );
200            }
201    
202            Linkage linkage = new Linkage( href );
203            OnlineResource result = new OnlineResource( linkage );
204            return result;
205        }
206    
207        /**
208         * @param root
209         *            the Code element
210         * @return a <code>Code</code> data object
211         */
212        public Code parseCode( Element root ) {
213            URI codeSpace = null;
214            try {
215                codeSpace = new URI( XMLTools.getAttrValue( root, null, "codeSpace", null ) );
216            } catch ( Exception e ) {
217                // ignore codeSpace
218            }
219    
220            String code = XMLTools.getStringValue( root );
221    
222            if ( codeSpace != null )
223                return new Code( code, codeSpace );
224            return new Code( code );
225        }
226    
227        /**
228         * @param root
229         *            the Type element
230         * @return the <code>TypeCode</code> data object
231         */
232        public TypeCode parseTypeCode( Element root ) {
233            Code code = parseCode( root );
234            // since the TypeCode class already existed, it is used. Deleting the TypeCode class and
235            // just using the Code class would be the better solution, though.
236            return new TypeCode( code.getCode(), code.getCodeSpace() );
237        }
238    
239        /**
240         * @param root
241         *            the Role element
242         * @return the <code>RoleCode</code> data object
243         */
244        public RoleCode parseRoleCode( Element root ) {
245            Code code = parseCode( root );
246            // since the RoleCode class already existed, it is used. Deleting the RoleCode class and
247            // just using the Code class would be the better solution, though.
248            return new RoleCode( code.getCode() );
249        }
250    
251        /**
252         * @param root
253         *            the AccessConstraints element
254         * @param fee
255         * @return the <code>Constraints</code> object containing the parsed data
256         * @throws XMLParsingException
257         */
258        public Constraints parseConstraint( Element root, String fee )
259                                throws XMLParsingException {
260            // please note that the same fee is used for all constraints
261    
262            List<String> constr = new ArrayList<String>();
263            String str = XMLTools.getRequiredNodeAsString( root, ".", nsContext );
264            constr.add( str );
265    
266            Constraints result = new Constraints( fee, null, null, null, constr, null, null, null );
267            return result;
268        }
269    
270        /**
271         * @param root
272         *            the Keywords element
273         * @return the <code>Keywords</code> object
274         * @throws XMLParsingException
275         */
276        public Keywords parseKeywords( Element root )
277                                throws XMLParsingException {
278            String[] keywords = XMLTools.getRequiredNodesAsStrings( root, POWS + "Keyword", nsContext );
279            Element codeElem = (Element) XMLTools.getNode( root, POWS + "Type", nsContext );
280            TypeCode type = null;
281            if ( codeElem != null )
282                type = parseTypeCode( codeElem );
283    
284            Keywords result = null;
285    
286            // the thesaurus name is ignored at the moment, as it is omitted by the OWS specification as
287            // well
288            if ( type != null )
289                result = new Keywords( keywords );
290            else
291                result = new Keywords( keywords, "", type );
292    
293            return result;
294        }
295    
296    }