001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/metadata/iso19115/ISO19115Document.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.model.metadata.iso19115;
037    
038    import java.net.MalformedURLException;
039    import java.net.URI;
040    import java.net.URL;
041    import java.util.ArrayList;
042    import java.util.List;
043    
044    import org.deegree.datatypes.Code;
045    import org.deegree.framework.xml.XMLFragment;
046    import org.deegree.framework.xml.XMLParsingException;
047    import org.deegree.framework.xml.XMLTools;
048    import org.deegree.ogcbase.CommonNamespaces;
049    import org.w3c.dom.Element;
050    
051    /**
052     * Parser class that can parse various elements defined in the OWS subset of the ISO 19115
053     * specification.
054     *
055     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
059     *
060     * @since 2.0
061     */
062    
063    public class ISO19115Document extends XMLFragment {
064    
065        private static final long serialVersionUID = -5536802360612196021L;
066    
067        private static final String POWS = CommonNamespaces.OWS_PREFIX + ":";
068    
069        private static final String PXLINK = CommonNamespaces.XLINK_PREFIX + ":";
070    
071        /**
072         * @param root
073         *            an element of type ows:ResponsiblePartySubsetType
074         * @return the data object
075         * @throws XMLParsingException
076         */
077        public CitedResponsibleParty parseCitedResponsibleParty( Element root )
078                                throws XMLParsingException {
079            String individualName = XMLTools.getNodeAsString( root, POWS + "IndividualName", nsContext, null );
080    
081            String positionName = XMLTools.getNodeAsString( root, POWS + "PositionName", nsContext, null );
082    
083            Element roleElement = (Element) XMLTools.getNode( root, POWS + "Role", nsContext );
084            RoleCode role = null;
085            if ( roleElement != null )
086                role = parseRoleCode( roleElement );
087    
088            Element contactElement = (Element) XMLTools.getNode( root, POWS + "ContactInfo", nsContext );
089            ContactInfo contactInfo = null;
090            if ( contactElement != null )
091                contactInfo = parseContactInfo( contactElement );
092    
093            // why Lists/Arrays are necessary here is beyond my knowledge
094            List<String> name = new ArrayList<String>();
095            name.add( individualName );
096            List<String> pName = new ArrayList<String>();
097            pName.add( positionName );
098            List<RoleCode> roles = new ArrayList<RoleCode>();
099            roles.add( role );
100            List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
101            contactInfos.add( contactInfo );
102    
103            CitedResponsibleParty result = new CitedResponsibleParty( contactInfos, name, null, pName, roles );
104            return result;
105        }
106    
107        /**
108         * @param root
109         *            the ContactInfo element
110         * @return the <code>ContactInfo</code> data object
111         * @throws XMLParsingException
112         */
113        public ContactInfo parseContactInfo( Element root )
114                                throws XMLParsingException {
115            Element phoneElement = (Element) XMLTools.getNode( root, POWS + "Phone", nsContext );
116            Phone phone = null;
117            if ( phoneElement != null )
118                phone = parsePhone( phoneElement );
119    
120            Address address = null;
121            Element addressElement = (Element) XMLTools.getNode( root, POWS + "Address", nsContext );
122            if ( addressElement != null )
123                address = parseAddress( addressElement );
124    
125            OnlineResource onlineResource = null;
126            Element onlineResourceElement = (Element) XMLTools.getNode( root, POWS + "OnlineResource", nsContext );
127            if ( onlineResourceElement != null )
128                onlineResource = parseOnlineResource( onlineResourceElement );
129    
130            String hoursOfService = XMLTools.getNodeAsString( root, POWS + "HoursOfService", nsContext, null );
131            String contactInstructions = XMLTools.getNodeAsString( root, POWS + "ContactInstructions", nsContext, null );
132    
133            ContactInfo result = new ContactInfo( address, contactInstructions, hoursOfService, onlineResource, phone );
134            return result;
135        }
136    
137        /**
138         * @param root
139         *            the Address element
140         * @return the <code>Address</code> data object
141         * @throws XMLParsingException
142         */
143        public Address parseAddress( Element root )
144                                throws XMLParsingException {
145            String[] deliveryPoint = XMLTools.getNodesAsStrings( root, POWS + "DeliveryPoint", nsContext );
146            String city = XMLTools.getNodeAsString( root, POWS + "City", nsContext, null );
147            String administrativeArea = XMLTools.getNodeAsString( root, POWS + "AdministrativeArea", nsContext, null );
148            String postalCode = XMLTools.getNodeAsString( root, POWS + "PostalCode", nsContext, null );
149            String country = XMLTools.getNodeAsString( root, POWS + "Country", nsContext, null );
150            String[] emails = XMLTools.getNodesAsStrings( root, POWS + "ElectronicMailAddress", nsContext );
151    
152            Address result = new Address( administrativeArea, city, country, deliveryPoint, emails, postalCode );
153            return result;
154        }
155    
156        /**
157         * @param root
158         *            the Phone element
159         * @return a <code>Phone</code> data object
160         * @throws XMLParsingException
161         */
162        public Phone parsePhone( Element root )
163                                throws XMLParsingException {
164            String[] voice = XMLTools.getNodesAsStrings( root, POWS + "Voice", nsContext );
165    
166            String[] facsimile = XMLTools.getNodesAsStrings( root, POWS + "Facsimile", nsContext );
167    
168            Phone result = new Phone( facsimile, voice );
169            return result;
170        }
171    
172        /**
173         * @param root
174         *            the element containing the xlink attributes
175         * @return the <code>OnlineResource</data> data object
176         * @throws XMLParsingException
177         */
178        public OnlineResource parseOnlineResource( Element root )
179                                throws XMLParsingException {
180            // This is just a preview version, not sure how to handle all the xlink attributes
181            // correctly.
182    
183            URL href = null;
184            String url = null;
185    
186            try {
187                url = XMLTools.getNodeAsString( root, "@" + PXLINK + "href", nsContext, null );
188                if ( url != null )
189                    href = new URL( url );
190            } catch ( MalformedURLException e ) {
191                throw new XMLParsingException( "'" + url + "' is not a valid URL." );
192            }
193    
194            Linkage linkage = new Linkage( href );
195            OnlineResource result = new OnlineResource( linkage );
196            return result;
197        }
198    
199        /**
200         * @param root
201         *            the Code element
202         * @return a <code>Code</code> data object
203         */
204        public Code parseCode( Element root ) {
205            URI codeSpace = null;
206            try {
207                codeSpace = new URI( XMLTools.getAttrValue( root, null, "codeSpace", null ) );
208            } catch ( Exception e ) {
209                // ignore codeSpace
210            }
211    
212            String code = XMLTools.getStringValue( root );
213    
214            if ( codeSpace != null )
215                return new Code( code, codeSpace );
216            return new Code( code );
217        }
218    
219        /**
220         * @param root
221         *            the Type element
222         * @return the <code>TypeCode</code> data object
223         */
224        public TypeCode parseTypeCode( Element root ) {
225            Code code = parseCode( root );
226            // since the TypeCode class already existed, it is used. Deleting the TypeCode class and
227            // just using the Code class would be the better solution, though.
228            return new TypeCode( code.getCode(), code.getCodeSpace() );
229        }
230    
231        /**
232         * @param root
233         *            the Role element
234         * @return the <code>RoleCode</code> data object
235         */
236        public RoleCode parseRoleCode( Element root ) {
237            Code code = parseCode( root );
238            // since the RoleCode class already existed, it is used. Deleting the RoleCode class and
239            // just using the Code class would be the better solution, though.
240            return new RoleCode( code.getCode() );
241        }
242    
243        /**
244         * @param root
245         *            the AccessConstraints element
246         * @param fee
247         * @return the <code>Constraints</code> object containing the parsed data
248         * @throws XMLParsingException
249         */
250        public Constraints parseConstraint( Element root, String fee )
251                                throws XMLParsingException {
252            // please note that the same fee is used for all constraints
253    
254            List<String> constr = new ArrayList<String>();
255            String str = XMLTools.getRequiredNodeAsString( root, ".", nsContext );
256            constr.add( str );
257    
258            Constraints result = new Constraints( fee, null, null, null, constr, null, null, null );
259            return result;
260        }
261    
262        /**
263         * @param root
264         *            the Keywords element
265         * @return the <code>Keywords</code> object
266         * @throws XMLParsingException
267         */
268        public Keywords parseKeywords( Element root )
269                                throws XMLParsingException {
270            String[] keywords = XMLTools.getRequiredNodesAsStrings( root, POWS + "Keyword", nsContext );
271            Element codeElem = (Element) XMLTools.getNode( root, POWS + "Type", nsContext );
272            TypeCode type = null;
273            if ( codeElem != null )
274                type = parseTypeCode( codeElem );
275    
276            Keywords result = null;
277    
278            // the thesaurus name is ignored at the moment, as it is omitted by the OWS specification as
279            // well
280            if ( type != null )
281                result = new Keywords( keywords );
282            else
283                result = new Keywords( keywords, "", type );
284    
285            return result;
286        }
287    
288    }