001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/metadata/iso19115/XMLFactory.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.model.metadata.iso19115;
037    
038    import java.net.URI;
039    import java.net.URL;
040    import java.util.List;
041    
042    import org.deegree.datatypes.Code;
043    import org.deegree.framework.xml.XMLTools;
044    import org.deegree.ogcbase.CommonNamespaces;
045    import org.w3c.dom.Element;
046    
047    /**
048     * <code>XMLFactory</code> with append methods for the various ISO 19115 elements as specified in
049     * the OWS common specification 1.0.0.
050     *
051     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
052     * @author last edited by: $Author: mschneider $
053     *
054     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
055     *
056     * @since 2.0
057     */
058    
059    public class XMLFactory extends org.deegree.ogcbase.XMLFactory {
060    
061        private static URI OWS = CommonNamespaces.OWSNS;
062    
063        private static String POWS = CommonNamespaces.OWS_PREFIX + ':';
064    
065        /**
066         * Appends the given <code>CitedResponsibleParty</code> object as XML.
067         *
068         * @param root
069         * @param party
070         */
071        public static void appendCitedResponsibleParty( Element root, CitedResponsibleParty party ) {
072            Element elem = XMLTools.appendElement( root, OWS, POWS + "ResponsiblePartySubsetType" );
073    
074            String[] individualNames = party.getIndividualName();
075            if ( ( individualNames != null ) && ( individualNames.length != 0 ) && ( individualNames[0] != null ) )
076                XMLTools.appendElement( elem, OWS, POWS + "IndividualName", individualNames[0] );
077    
078            String[] positionNames = party.getPositionName();
079            if ( ( positionNames != null ) && ( positionNames.length != 0 ) && ( positionNames[0] != null ) )
080                XMLTools.appendElement( elem, OWS, POWS + "PositionName", positionNames[0] );
081    
082            RoleCode[] roles = party.getRoleCode();
083            if ( ( roles != null ) && ( roles.length != 0 ) && ( roles[0] != null ) )
084                XMLTools.appendElement( elem, OWS, POWS + "Role", roles[0].getValue() );
085    
086            ContactInfo[] contactInfos = party.getContactInfo();
087            if ( ( contactInfos != null ) && ( contactInfos.length != 0 ) && ( contactInfos[0] != null ) )
088                appendContactInfo( elem, contactInfos[0] );
089        }
090    
091        /**
092         * Appends the contact info.
093         *
094         * @param root
095         * @param contactInfo
096         */
097        public static void appendContactInfo( Element root, ContactInfo contactInfo ) {
098            Element elem = XMLTools.appendElement( root, OWS, POWS + "ContactInfo" );
099    
100            appendPhone( elem, contactInfo.getPhone() );
101            appendAddress( elem, contactInfo.getAddress() );
102            appendOnlineResource( elem, contactInfo.getOnLineResource() );
103    
104            String hours = contactInfo.getHoursOfService();
105            String instructions = contactInfo.getContactInstructions();
106    
107            if ( hours != null )
108                XMLTools.appendElement( elem, OWS, POWS + "HoursOfService", hours );
109            if ( instructions != null )
110                XMLTools.appendElement( elem, OWS, POWS + "ContactInstructions", instructions );
111        }
112    
113        /**
114         * Appends the phone data.
115         *
116         * @param root
117         * @param phone
118         */
119        public static void appendPhone( Element root, Phone phone ) {
120            if ( phone == null )
121                return;
122    
123            Element elem = XMLTools.appendElement( root, OWS, POWS + "Phone" );
124    
125            String[] voice = phone.getVoice();
126            for ( String number : voice )
127                XMLTools.appendElement( elem, OWS, POWS + "Voice", number );
128    
129            String[] facsimile = phone.getFacsimile();
130            for ( String number : facsimile )
131                XMLTools.appendElement( elem, OWS, POWS + "Facsimile", number );
132        }
133    
134        /**
135         * Appends the address data.
136         *
137         * @param root
138         * @param address
139         */
140        public static void appendAddress( Element root, Address address ) {
141            if ( address == null )
142                return;
143    
144            Element elem = XMLTools.appendElement( root, OWS, POWS + "Address" );
145    
146            String[] deliveryPoint = address.getDeliveryPoint();
147            for ( String point : deliveryPoint )
148                XMLTools.appendElement( elem, OWS, POWS + "DeliveryPoint", point );
149    
150            String city = address.getCity();
151            if ( city != null )
152                XMLTools.appendElement( elem, OWS, POWS + "City", city );
153    
154            String adminArea = address.getAdministrativeArea();
155            if ( adminArea != null )
156                XMLTools.appendElement( elem, OWS, POWS + "AdministrativeArea", adminArea );
157    
158            String postalCode = address.getPostalCode();
159            if ( postalCode != null )
160                XMLTools.appendElement( elem, OWS, POWS + "PostalCode", postalCode );
161    
162            String country = address.getCountry();
163            if ( country != null )
164                XMLTools.appendElement( elem, OWS, POWS + "Country", country );
165    
166            String[] email = address.getElectronicMailAddress();
167            for ( String mail : email )
168                XMLTools.appendElement( elem, OWS, POWS + "ElectronicMailAddress", mail );
169        }
170    
171        /**
172         * Appends the link.
173         *
174         * @param root
175         * @param link
176         */
177        public static void appendOnlineResource( Element root, OnlineResource link ) {
178            if ( link == null )
179                return;
180            appendOnlineResource( root, link.getLinkage().getHref() );
181        }
182    
183        /**
184         * Appends the link.
185         *
186         * @param root
187         * @param link
188         */
189        public static void appendOnlineResource( Element root, URL link ) {
190            if ( link == null )
191                return;
192    
193            // fix up URL to standard form
194            String url = link.toExternalForm();
195            if ( !url.toString().endsWith( "?" ) ) {
196                if ( !url.endsWith( "&" ) ) {
197                    if ( url.indexOf( "?" ) == -1 )
198                        url = url + "?";
199                    else
200                        url = url + "&";
201                }
202            }
203    
204            root.setAttributeNS( "http://www.w3.org/1999/xlink", "xlink:type", "simple" );
205            root.setAttributeNS( "http://www.w3.org/1999/xlink", "xlink:href", url );
206    
207        }
208    
209        /**
210         * Appends an online resource in a newly created element in the OWS namespace. The new element
211         * will be named according to the tagName parameter.
212         *
213         * @param root
214         * @param link
215         * @param tagName
216         */
217        public static void appendOnlineResource( Element root, OnlineResource link, String tagName ) {
218            Element newElem = XMLTools.appendElement( root, OWS, POWS + tagName );
219            appendOnlineResource( newElem, link );
220        }
221    
222        /**
223         * Appends an online resource in a newly created element in the OWS namespace. The new element
224         * will be named according to the tagName parameter.
225         *
226         * @param root
227         * @param link
228         * @param tagName
229         */
230        public static void appendOnlineResource( Element root, URL link, String tagName ) {
231            Element newElem = XMLTools.appendElement( root, OWS, POWS + tagName );
232            appendOnlineResource( newElem, link );
233        }
234    
235        /**
236         * Appends the access constraint element. Please note that a lot of the information contained
237         * within will not be included in the output due to restrictions of the OWS specification.
238         *
239         * @param root
240         * @param constraints
241         */
242        public static void appendAccessConstraint( Element root, Constraints constraints ) {
243            List<String> constrList = constraints.getUseLimitations();
244            String constr = "";
245            if ( constrList.size() != 0 )
246                constr = constrList.get( 0 );
247    
248            XMLTools.appendElement( root, OWS, POWS + "AccessConstraint", constr );
249        }
250    
251        /**
252         * Appends an element of type CodeType with the given name and content.
253         *
254         * @param root
255         * @param tagName
256         * @param code
257         */
258        public static void appendCode( Element root, String tagName, Code code ) {
259            Element elem = XMLTools.appendElement( root, OWS, POWS + tagName, code.getCode() );
260            URI codeSpace = code.getCodeSpace();
261            if ( codeSpace != null )
262                elem.setAttribute( "codeSpace", codeSpace.toString() );
263        }
264    
265        /**
266         * Appends the contents of a <code>Keywords</code> object.
267         *
268         * @param root
269         * @param keywords
270         */
271        public static void appendKeywords( Element root, Keywords keywords ) {
272            Element elem = XMLTools.appendElement( root, OWS, POWS + "Keywords" );
273    
274            String[] words = keywords.getKeywords();
275            for ( String word : words )
276                XMLTools.appendElement( elem, OWS, POWS + "Keyword", word );
277    
278            TypeCode code = keywords.getTypeCode();
279            if ( code != null )
280                appendCode( elem, "Type", code );
281        }
282    
283    }