0001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/context/WebMapContextFactory.java $
0002: /*---------------- FILE HEADER ------------------------------------------
0003:
0004: This file is part of deegree.
0005: Copyright (C) 2001-2008 by:
0006: EXSE, Department of Geography, University of Bonn
0007: http://www.giub.uni-bonn.de/deegree/
0008: lat/lon GmbH
0009: http://www.lat-lon.de
0010:
0011: This library is free software; you can redistribute it and/or
0012: modify it under the terms of the GNU Lesser General Public
0013: License as published by the Free Software Foundation; either
0014: version 2.1 of the License, or (at your option) any later version.
0015:
0016: This library is distributed in the hope that it will be useful,
0017: but WITHOUT ANY WARRANTY; without even the implied warranty of
0018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0019: Lesser General Public License for more details.
0020:
0021: You should have received a copy of the GNU Lesser General Public
0022: License along with this library; if not, write to the Free Software
0023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0024:
0025: Contact:
0026:
0027: Andreas Poth
0028: lat/lon GmbH
0029: Aennchenstr. 19
0030: 53177 Bonn
0031: Germany
0032: E-Mail: poth@lat-lon.de
0033:
0034: Prof. Dr. Klaus Greve
0035: Department of Geography
0036: University of Bonn
0037: Meckenheimer Allee 166
0038: 53115 Bonn
0039: Germany
0040: E-Mail: greve@giub.uni-bonn.de
0041:
0042:
0043: ---------------------------------------------------------------------------*/
0044: package org.deegree.portal.context;
0045:
0046: import java.awt.Rectangle;
0047: import java.io.IOException;
0048: import java.io.InputStreamReader;
0049: import java.io.Reader;
0050: import java.net.MalformedURLException;
0051: import java.net.URL;
0052: import java.util.HashMap;
0053: import java.util.Map;
0054:
0055: import org.apache.commons.httpclient.HttpClient;
0056: import org.apache.commons.httpclient.methods.GetMethod;
0057: import org.deegree.datatypes.QualifiedName;
0058: import org.deegree.enterprise.WebUtils;
0059: import org.deegree.framework.log.ILogger;
0060: import org.deegree.framework.log.LoggerFactory;
0061: import org.deegree.framework.util.Parameter;
0062: import org.deegree.framework.util.ParameterList;
0063: import org.deegree.framework.util.StringTools;
0064: import org.deegree.framework.xml.ElementList;
0065: import org.deegree.framework.xml.XMLFragment;
0066: import org.deegree.framework.xml.XMLParsingException;
0067: import org.deegree.framework.xml.XMLTools;
0068: import org.deegree.graphics.sld.FeatureTypeStyle;
0069: import org.deegree.graphics.sld.SLDFactory;
0070: import org.deegree.graphics.sld.StyledLayerDescriptor;
0071: import org.deegree.model.crs.CRSFactory;
0072: import org.deegree.model.crs.CoordinateSystem;
0073: import org.deegree.model.crs.UnknownCRSException;
0074: import org.deegree.model.metadata.iso19115.Address;
0075: import org.deegree.model.metadata.iso19115.CitedResponsibleParty;
0076: import org.deegree.model.metadata.iso19115.ContactInfo;
0077: import org.deegree.model.metadata.iso19115.Phone;
0078: import org.deegree.model.metadata.iso19115.RoleCode;
0079: import org.deegree.model.spatialschema.GeometryFactory;
0080: import org.deegree.model.spatialschema.Point;
0081: import org.deegree.ogcbase.BaseURL;
0082: import org.deegree.ogcbase.CommonNamespaces;
0083: import org.deegree.ogcbase.ImageURL;
0084: import org.deegree.ogcwebservices.OWSUtils;
0085: import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
0086: import org.deegree.ogcwebservices.getcapabilities.OGCCapabilitiesDocument;
0087: import org.deegree.ogcwebservices.wcs.getcapabilities.WCSCapabilities;
0088: import org.deegree.ogcwebservices.wcs.getcapabilities.WCSCapabilitiesDocument;
0089: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
0090: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument;
0091: import org.deegree.ogcwebservices.wms.capabilities.LegendURL;
0092: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
0093: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
0094: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
0095: import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic;
0096: import org.deegree.owscommon_new.HTTP;
0097: import org.deegree.owscommon_new.Operation;
0098: import org.deegree.owscommon_new.OperationsMetadata;
0099: import org.deegree.security.drm.model.User;
0100: import org.w3c.dom.Element;
0101: import org.w3c.dom.Text;
0102: import org.xml.sax.SAXException;
0103:
0104: /**
0105: * Factory class for creating an instance of a web map Context (<tt>ViewContext</tt>). The
0106: * factory is able to parse deegree specific extensions (General and Layer) as well as standard web
0107: * map context documents.
0108: *
0109: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
0110: * @author last edited by: $Author: apoth $
0111: *
0112: * @version $Revision: 10572 $, $Date: 2008-03-13 02:36:08 -0700 (Thu, 13 Mar 2008) $
0113: */
0114: public class WebMapContextFactory {
0115:
0116: private static final ILogger LOG = LoggerFactory
0117: .getLogger(WebMapContextFactory.class);
0118:
0119: private static Map<URL, WMSCapabilities> wmsCache = new HashMap<URL, WMSCapabilities>();
0120:
0121: private static Map<URL, WFSCapabilities> wfsCache = new HashMap<URL, WFSCapabilities>();
0122:
0123: private static Map<URL, WCSCapabilities> wcsCache = new HashMap<URL, WCSCapabilities>();
0124:
0125: /**
0126: * creates an instance of a ViewContext from the web map context document read from the passed
0127: * URL
0128: *
0129: * @param url
0130: * @param user
0131: * @param sessionID
0132: * @return
0133: * @throws IOException
0134: * @throws XMLParsingException
0135: * @throws ContextException
0136: * @throws SAXException
0137: * @throws UnknownCRSException
0138: */
0139: public synchronized static ViewContext createViewContext(URL url,
0140: User user, String sessionID) throws IOException,
0141: XMLParsingException, ContextException, SAXException,
0142: UnknownCRSException {
0143: // cache have to be cleared because contained capabilities may has been
0144: // requested with other user identification
0145: wmsCache.clear();
0146:
0147: XMLFragment xml = new XMLFragment(url);
0148:
0149: return createViewContext(xml, user, sessionID);
0150: }
0151:
0152: /**
0153: * @param xml
0154: * @param user
0155: * @param sessionID
0156: * @return
0157: * @throws IOException
0158: * @throws XMLParsingException
0159: * @throws ContextException
0160: * @throws SAXException
0161: * @throws UnknownCRSException
0162: */
0163: public synchronized static ViewContext createViewContext(
0164: XMLFragment xml, User user, String sessionID)
0165: throws IOException, XMLParsingException, ContextException,
0166: UnknownCRSException {
0167: // general section
0168: Element element = XMLTools.getRequiredChildElement("General",
0169: CommonNamespaces.CNTXTNS, xml.getRootElement());
0170: General general = createGeneral(element, xml);
0171:
0172: // Layer (List) section
0173: element = XMLTools.getRequiredChildElement("LayerList",
0174: CommonNamespaces.CNTXTNS, xml.getRootElement());
0175:
0176: LayerList layerList = createLayerList(element, user, sessionID);
0177:
0178: ViewContext vc = new ViewContext(general, layerList);
0179:
0180: return vc;
0181: }
0182:
0183: /**
0184: * creates an instance of a class encapsulating the general context informations
0185: *
0186: * @param element
0187: * <General>
0188: * @param xml
0189: *
0190: * @return instance of <tt>General</tt>
0191: *
0192: * @throws XMLParsingException
0193: * @throws UnknownCRSException
0194: */
0195: private static General createGeneral(Element element,
0196: XMLFragment xml) throws XMLParsingException,
0197: MalformedURLException, UnknownCRSException {
0198:
0199: // <Window>
0200: Element elem = XMLTools.getChildElement("Window",
0201: CommonNamespaces.CNTXTNS, element);
0202: Rectangle rect = createWindow(elem);
0203:
0204: // <BoundingBox>
0205: elem = XMLTools.getRequiredChildElement("BoundingBox",
0206: CommonNamespaces.CNTXTNS, element);
0207: Point[] bbox = createBoundingBox(elem);
0208:
0209: // <Title>
0210: String title = XMLTools.getRequiredStringValue("Title",
0211: CommonNamespaces.CNTXTNS, element);
0212:
0213: // <KeywordList>
0214: elem = XMLTools.getChildElement("KeywordList",
0215: CommonNamespaces.CNTXTNS, element);
0216: String[] keywords = createKeywords(elem);
0217:
0218: // <Abstract>
0219: String abstract_ = XMLTools.getStringValue("Abstract",
0220: CommonNamespaces.CNTXTNS, element, null);
0221:
0222: // <LogoURL>
0223: elem = XMLTools.getChildElement("LogoURL",
0224: CommonNamespaces.CNTXTNS, element);
0225: ImageURL logoURL = createImageURL(elem);
0226:
0227: // <DescriptionURL>
0228:
0229: // elem = XMLTools.getChildElement( "DescriptionURL", CommonNamespaces.CNTXTNS, element );
0230: elem = XMLTools.getRequiredElement(element,
0231: CommonNamespaces.CNTXT_PREFIX + ":DescriptionURL",
0232: CommonNamespaces.getNamespaceContext());
0233:
0234: BaseURL descriptionURL = createBaseURL(elem);
0235:
0236: // <ContactInformation>
0237: elem = XMLTools.getChildElement("ContactInformation",
0238: CommonNamespaces.CNTXTNS, element);
0239: CitedResponsibleParty contact = createContactInformation(elem);
0240:
0241: // <Extension>
0242: elem = XMLTools.getChildElement("Extension",
0243: CommonNamespaces.CNTXTNS, element);
0244: GeneralExtension extension = createGeneralExtension(elem, xml);
0245:
0246: General general = null;
0247: try {
0248: general = new General(title, abstract_, rect, contact,
0249: bbox, descriptionURL, logoURL, keywords, extension);
0250: } catch (Exception e) {
0251: throw new XMLParsingException(e.getMessage(), e);
0252: }
0253:
0254: return general;
0255: }
0256:
0257: /**
0258: * creates a <tt>Rectangle<tt> (Window) instance from the passed Element.
0259: *
0260: * @param element <Window>
0261: *
0262: * @return instance of <tt>Rectangle</tt>
0263: *
0264: * @throws XMLParsingException
0265: */
0266: private static Rectangle createWindow(Element element)
0267: throws XMLParsingException {
0268:
0269: Rectangle rect = null;
0270:
0271: if (element != null) {
0272: String tmp = XMLTools.getRequiredAttrValue("width", null,
0273: element);
0274: int width = Integer.parseInt(tmp);
0275: tmp = XMLTools
0276: .getRequiredAttrValue("height", null, element);
0277:
0278: int height = Integer.parseInt(tmp);
0279: rect = new Rectangle(width, height);
0280: }
0281:
0282: return rect;
0283: }
0284:
0285: /**
0286: * creates a <tt>Envelope </tt> from the passed Element
0287: *
0288: * @param element
0289: * <BoundingBox>
0290: *
0291: * @return instance of <tt>Envelope</tt>
0292: *
0293: * @throws XMLParsingException
0294: * @throws UnknownCRSException
0295: */
0296: private static Point[] createBoundingBox(Element element)
0297: throws XMLParsingException, UnknownCRSException {
0298:
0299: String srs = XMLTools
0300: .getRequiredAttrValue("SRS", null, element);
0301: CoordinateSystem crs = CRSFactory.create(srs);
0302: String tmp = XMLTools.getRequiredAttrValue("minx", null,
0303: element);
0304: double minx = Double.parseDouble(tmp);
0305: tmp = XMLTools.getRequiredAttrValue("miny", null, element);
0306:
0307: double miny = Double.parseDouble(tmp);
0308: tmp = XMLTools.getRequiredAttrValue("maxx", null, element);
0309:
0310: double maxx = Double.parseDouble(tmp);
0311: tmp = XMLTools.getRequiredAttrValue("maxy", null, element);
0312:
0313: double maxy = Double.parseDouble(tmp);
0314:
0315: Point[] points = new Point[2];
0316: points[0] = GeometryFactory.createPoint(minx, miny, crs);
0317: points[1] = GeometryFactory.createPoint(maxx, maxy, crs);
0318:
0319: return points;
0320: }
0321:
0322: /**
0323: * creates an array of keywords (String) from the passed Keyword list
0324: *
0325: * @param element
0326: * <KeywordList>
0327: *
0328: * @return array of Strings
0329: *
0330: * @throws XMLParsingException
0331: */
0332: private static String[] createKeywords(Element element) {
0333:
0334: ElementList el = XMLTools.getChildElements("Keyword",
0335: CommonNamespaces.CNTXTNS, element);
0336: String[] keywords = new String[el.getLength()];
0337:
0338: for (int i = 0; i < keywords.length; i++) {
0339: keywords[i] = XMLTools.getStringValue(el.item(i));
0340: }
0341:
0342: return keywords;
0343: }
0344:
0345: /**
0346: * creates an instance of an ImageURL that is used for <LogoURL> and LegendURL
0347: *
0348: * @param element
0349: * <LogoURL> or <LegendURL>
0350: *
0351: * @return instance of <tt>ImageURL</tt>
0352: *
0353: * @throws XMLParsingException
0354: */
0355: private static ImageURL createImageURL(Element element)
0356: throws XMLParsingException {
0357:
0358: ImageURL imageURL = null;
0359:
0360: if (element != null) {
0361: String tmp = XMLTools.getAttrValue(element, null, "width",
0362: null);
0363: int width = -1;
0364: if (tmp != null) {
0365: width = Integer.parseInt(tmp);
0366: }
0367: tmp = XMLTools.getAttrValue(element, null, "height", null);
0368: int height = -1;
0369: if (tmp != null) {
0370: height = Integer.parseInt(tmp);
0371: }
0372: String format = XMLTools.getAttrValue(element, null,
0373: "format", null);
0374:
0375: Element elem = XMLTools
0376: .getRequiredChildElement("OnlineResource",
0377: CommonNamespaces.CNTXTNS, element);
0378: URL onlineResource = createOnlineResource(elem);
0379:
0380: imageURL = new ImageURL(width, height, format,
0381: onlineResource);
0382: }
0383:
0384: return imageURL;
0385: }
0386:
0387: /**
0388: * creates an instance of an URL described by a <OnlineResource> element
0389: *
0390: * @param element
0391: * <OnlineResource>
0392: *
0393: * @return instance of <tt>URL</tt>
0394: *
0395: * @throws XMLParsingException
0396: */
0397: private static URL createOnlineResource(Element element)
0398: throws XMLParsingException {
0399:
0400: URL onlineResource = null;
0401:
0402: if (element != null) {
0403: // String type = XMLTools.getAttrValue( element, null, "type", null );
0404: String type = element.getAttributeNS(CommonNamespaces.XLNNS
0405: .toASCIIString(), "type");
0406:
0407: // if ( type == null || "".equals( type.trim() ) ) {
0408: // type = XMLTools.getAttrValue( element, CommonNamespaces.XLNNS, "type", null );
0409: // }
0410: // System.out.println( "document: " + new XMLFragment( element ).getAsPrettyString() );
0411: // System.out.println("Type is: " + type);
0412: if ((type != null) && !"".equals(type)
0413: && !type.equals("simple")) {
0414: throw new XMLParsingException(
0415: "unknown type of online resource: " + type);
0416: }
0417:
0418: // String tmp = XMLTools.getAttrValue( element, null, "href", null );
0419: String tmp = element.getAttributeNS(CommonNamespaces.XLNNS
0420: .toASCIIString(), "href");
0421:
0422: // if ( tmp == null ) {
0423: // tmp = XMLTools.getAttrValue( element, CommonNamespaces.XLNNS, "href", null );
0424: // }
0425:
0426: // System.out.println("href is: " + tmp);
0427: try {
0428: onlineResource = new URL(tmp);
0429: } catch (Exception e) {
0430: throw new XMLParsingException(
0431: "couldn't create online resource", e);
0432: }
0433: }
0434:
0435: return onlineResource;
0436: }
0437:
0438: /**
0439: * creates an instance of BaseURL that is used for <DescriptionURL>
0440: *
0441: * @param element
0442: * <DescriptionURL>
0443: *
0444: * @return instance of <tt>BaseURL</tt>
0445: *
0446: * @throws XMLParsingException
0447: */
0448: private static BaseURL createBaseURL(Element element)
0449: throws XMLParsingException {
0450:
0451: BaseURL baseURL = null;
0452:
0453: if (element != null) {
0454: String format = XMLTools.getAttrValue(element, null,
0455: "format", null);
0456:
0457: Element elem = XMLTools.getElement(element,
0458: CommonNamespaces.CNTXT_PREFIX + ":OnlineResource",
0459: CommonNamespaces.getNamespaceContext());
0460: URL onlineResource = createOnlineResource(elem);
0461:
0462: baseURL = new BaseURL(format, onlineResource);
0463: }
0464:
0465: return baseURL;
0466: }
0467:
0468: /**
0469: * Creates a <tt>ContactInformation</tt>-instance according to the contents of the
0470: * DOM-subtree starting at the given 'ContactInformation'-<tt>Element</tt>.
0471: * <p>
0472: *
0473: * @param element
0474: * the 'ContactInformation'-<tt>Element</tt>
0475: * @throws XMLParsingException
0476: * if a syntactic or semantic error in the DOM-subtree is encountered
0477: * @return the constructed <tt>ContactInformation</tt>-instance
0478: */
0479: private static CitedResponsibleParty createContactInformation(
0480: Element element) throws XMLParsingException {
0481:
0482: CitedResponsibleParty contact = null;
0483:
0484: if (element != null) {
0485: // optional: <ContactPersonPrimary>
0486: Element contactPersonPrimaryElement = XMLTools
0487: .getChildElement("ContactPersonPrimary",
0488: CommonNamespaces.CNTXTNS, element);
0489:
0490: String contactPerson = null;
0491: String contactOrganization = null;
0492: if (contactPersonPrimaryElement != null) {
0493: // required: <ContactPerson>
0494: contactPerson = XMLTools.getRequiredStringValue(
0495: "ContactPerson", CommonNamespaces.CNTXTNS,
0496: contactPersonPrimaryElement);
0497:
0498: // required: <ContactOrganization>
0499: contactOrganization = XMLTools.getRequiredStringValue(
0500: "ContactOrganization",
0501: CommonNamespaces.CNTXTNS,
0502: contactPersonPrimaryElement);
0503: }
0504:
0505: // optional: <ContactPosition>
0506: String contactPosition = XMLTools.getStringValue(
0507: "ContactPosition", CommonNamespaces.CNTXTNS,
0508: element, null);
0509:
0510: // optional: <ContactAddress>
0511: Address contactAddress = null;
0512: Element contactAddressElement = XMLTools
0513: .getChildElement("ContactAddress",
0514: CommonNamespaces.CNTXTNS, element);
0515:
0516: if (contactAddressElement != null) {
0517: // optional: <ContactElectronicMailAddress>
0518: String eMailAddress = XMLTools.getStringValue(
0519: "ContactElectronicMailAddress",
0520: CommonNamespaces.CNTXTNS, element, null);
0521: contactAddress = createContactAddress(eMailAddress,
0522: contactAddressElement);
0523: }
0524:
0525: // optional: <ContactVoiceTelephone>
0526: String voice = XMLTools.getStringValue(
0527: "ContactVoiceTelephone", CommonNamespaces.CNTXTNS,
0528: element, null);
0529:
0530: // optional: <ContactFacsimileTelephone>
0531: String fax = XMLTools.getStringValue(
0532: "ContactFacsimileTelephone",
0533: CommonNamespaces.CNTXTNS, element, null);
0534:
0535: Phone phone = new Phone(new String[] { fax },
0536: new String[] { voice });
0537: ContactInfo ci = new ContactInfo(contactAddress, null,
0538: null, null, phone);
0539: contact = new CitedResponsibleParty(
0540: new ContactInfo[] { ci },
0541: new String[] { contactPerson },
0542: new String[] { contactOrganization },
0543: new String[] { contactPosition }, new RoleCode[0]);
0544: }
0545:
0546: return contact;
0547: }
0548:
0549: /**
0550: * Creates a <tt>ContactAddress</tt>-instance according to the contents of the DOM-subtree
0551: * starting at the given 'ContactAddress'-<tt>Element</tt>.
0552: * <p>
0553: *
0554: * @param element
0555: * the 'ContactAddress'-<tt>Element</tt>
0556: * @throws XMLParsingException
0557: * if a syntactic or semantic error in the DOM-subtree is encountered
0558: * @return the constructed <tt>ContactAddress</tt>-instance
0559: */
0560: private static Address createContactAddress(String eMail,
0561: Element element) throws XMLParsingException {
0562:
0563: // required: <AddressType>
0564: /*
0565: * String addressType = XMLTools.getRequiredStringValue( "AddressType",
0566: * CommonNamespaces.CNTXTNS, element );
0567: */
0568: // required: <Address>
0569: String address = XMLTools.getRequiredStringValue("Address",
0570: CommonNamespaces.CNTXTNS, element);
0571:
0572: // required: <City>
0573: String city = XMLTools.getRequiredStringValue("City",
0574: CommonNamespaces.CNTXTNS, element);
0575:
0576: // required: <StateOrProvince>
0577: String stateOrProvince = XMLTools.getRequiredStringValue(
0578: "StateOrProvince", CommonNamespaces.CNTXTNS, element);
0579:
0580: // required: <PostCode>
0581: String postCode = XMLTools.getRequiredStringValue("PostCode",
0582: CommonNamespaces.CNTXTNS, element);
0583:
0584: // required: <Country>
0585: String country = XMLTools.getRequiredStringValue("Country",
0586: CommonNamespaces.CNTXTNS, element);
0587:
0588: return new Address(stateOrProvince, city, country,
0589: new String[] { address }, new String[] { eMail },
0590: postCode);
0591: }
0592:
0593: /**
0594: * creates an instance of a class encapsulating the deegree specific extensions of the general
0595: * section of a web map context document
0596: *
0597: * @param element
0598: * <Extension>
0599: * @param xml
0600: *
0601: * @return instance of <tt>GeneralExtension</tt>
0602: *
0603: * @throws XMLParsingException
0604: */
0605: private static GeneralExtension createGeneralExtension(
0606: Element element, XMLFragment xml)
0607: throws XMLParsingException, MalformedURLException {
0608:
0609: GeneralExtension ge = null;
0610:
0611: if (element != null) {
0612:
0613: // retunrs the current mode of a client using a WMC
0614: String mode = XMLTools.getStringValue("Mode",
0615: CommonNamespaces.DGCNTXTNS, element, "ZOOMIN");
0616:
0617: // <AuthentificationSettings>
0618: Element elem = XMLTools.getChildElement(
0619: "AuthentificationSettings",
0620: CommonNamespaces.DGCNTXTNS, element);
0621: AuthentificationSettings authSettings = null;
0622: if (elem != null) {
0623: authSettings = createAuthentificationSettings(elem);
0624: }
0625: // <IOSetiings>
0626: elem = XMLTools.getChildElement("IOSettings",
0627: CommonNamespaces.DGCNTXTNS, element);
0628: IOSettings ioSettings = null;
0629: if (elem != null) {
0630: ioSettings = createIOSettings(elem, xml);
0631: }
0632: // <Frontend>
0633: elem = XMLTools.getChildElement("Frontend",
0634: CommonNamespaces.DGCNTXTNS, element);
0635: Frontend frontend = null;
0636: if (elem != null) {
0637: frontend = createFrontend(elem);
0638: }
0639: // <MapParameter>
0640: elem = XMLTools.getRequiredChildElement("MapParameter",
0641: CommonNamespaces.DGCNTXTNS, element);
0642: MapParameter mapParameter = createMapParameter(elem);
0643:
0644: // <LayerTree>
0645: elem = XMLTools.getChildElement("LayerTree",
0646: CommonNamespaces.DGCNTXTNS, element);
0647:
0648: Node layerTreeRoot = null;
0649: if (elem != null) {
0650: Element nodeElem = XMLTools.getRequiredChildElement(
0651: "Node", CommonNamespaces.DGCNTXTNS, elem);
0652: layerTreeRoot = createNode(nodeElem, null);
0653: } else {
0654: try {
0655: layerTreeRoot = new Node(0, null, "root", false,
0656: false);
0657: Node[] nodes = new Node[] { new Node(1,
0658: layerTreeRoot, "deegree", false, false) };
0659: layerTreeRoot.setNodes(nodes);
0660: } catch (ContextException e) {
0661: throw new XMLParsingException(
0662: "couldn't create layertree node", e);
0663: }
0664: }
0665: ge = new GeneralExtension(ioSettings, frontend,
0666: mapParameter, authSettings, mode, layerTreeRoot);
0667: }
0668:
0669: return ge;
0670: }
0671:
0672: /**
0673: * creates a node in the layerTree
0674: */
0675: private static Node createNode(Element nodeElem, Node parent)
0676: throws XMLParsingException {
0677:
0678: int id = Integer.parseInt(XMLTools.getRequiredAttrValue("id",
0679: null, nodeElem));
0680: String title = XMLTools.getRequiredAttrValue("title", null,
0681: nodeElem);
0682: String s = XMLTools.getAttrValue(nodeElem, null, "selectable",
0683: null);
0684: boolean selectable = "true".equals(s) || "1".equals(s);
0685: s = XMLTools.getAttrValue(nodeElem, null, "collapsed", null);
0686: boolean collapsed = "true".equals(s) || "1".equals(s);
0687: Node node = null;
0688: try {
0689: node = new Node(id, parent, title, selectable, collapsed);
0690: } catch (Exception e) {
0691: throw new XMLParsingException(
0692: "couldn't create layertree node", e);
0693: }
0694:
0695: // get Child nodes
0696: ElementList nl = XMLTools.getChildElements("Node",
0697: CommonNamespaces.DGCNTXTNS, nodeElem);
0698: Node[] nodes = new Node[nl.getLength()];
0699: for (int i = 0; i < nodes.length; i++) {
0700: nodes[i] = createNode(nl.item(i), node);
0701: }
0702: // set child nodes
0703: node.setNodes(nodes);
0704:
0705: return node;
0706: }
0707:
0708: /**
0709: * creates an instance of a class encapsulating access descrition to an authentification service
0710: * for a deegree map client
0711: *
0712: * @param element
0713: * <AuthentificationSettings>
0714: * @return instance of <tt>AuthentificationSettings</tt>
0715: * @throws XMLParsingException
0716: */
0717: private static AuthentificationSettings createAuthentificationSettings(
0718: Element element) throws XMLParsingException {
0719:
0720: Element elem = XMLTools.getRequiredChildElement(
0721: "AuthentificationService", CommonNamespaces.DGCNTXTNS,
0722: element);
0723: elem = XMLTools.getRequiredChildElement("OnlineResource",
0724: CommonNamespaces.CNTXTNS, elem);
0725:
0726: URL url = createOnlineResource(elem);
0727: BaseURL bu = new BaseURL("text/xml", url);
0728:
0729: return new AuthentificationSettings(bu);
0730: }
0731:
0732: /**
0733: * creates an instance of a class encapsulating the frontend (GUI) description of a deegree map
0734: * client
0735: *
0736: * @param element
0737: * <Frontend>
0738: *
0739: * @return instance of <tt>Frontend</tt>
0740: *
0741: * @throws XMLParsingException
0742: */
0743: private static Frontend createFrontend(Element element)
0744: throws XMLParsingException {
0745:
0746: // <Controller>
0747: String controller = XMLTools.getRequiredStringValue(
0748: "Controller", CommonNamespaces.DGCNTXTNS, element);
0749: // <Style>
0750: String style = XMLTools.getStringValue("Style",
0751: CommonNamespaces.DGCNTXTNS, element, null);
0752: // <Buttons>
0753: String buttons = XMLTools.getStringValue("Buttons",
0754: CommonNamespaces.DGCNTXTNS, element, null);
0755: // <CommonJS>
0756: Element elem = XMLTools.getChildElement("CommonJS",
0757: CommonNamespaces.DGCNTXTNS, element);
0758: String[] commonJS = createCommonJS(elem);
0759: // <West>
0760: elem = XMLTools.getChildElement("West",
0761: CommonNamespaces.DGCNTXTNS, element);
0762: GUIArea west = createGUIArea(elem);
0763: // <East>
0764: elem = XMLTools.getChildElement("East",
0765: CommonNamespaces.DGCNTXTNS, element);
0766: GUIArea east = createGUIArea(elem);
0767: // <North>
0768: elem = XMLTools.getChildElement("North",
0769: CommonNamespaces.DGCNTXTNS, element);
0770: GUIArea north = createGUIArea(elem);
0771: // <South>
0772: elem = XMLTools.getChildElement("South",
0773: CommonNamespaces.DGCNTXTNS, element);
0774: GUIArea south = createGUIArea(elem);
0775: // <Center>
0776: elem = XMLTools.getChildElement("Center",
0777: CommonNamespaces.DGCNTXTNS, element);
0778: GUIArea center = createGUIArea(elem);
0779: // <Header>
0780: String header = XMLTools.getStringValue("Header",
0781: CommonNamespaces.DGCNTXTNS, element, null);
0782: // <Footer>
0783: String footer = XMLTools.getStringValue("Footer",
0784: CommonNamespaces.DGCNTXTNS, element, null);
0785:
0786: Frontend frontend = new JSPFrontend(controller, west, east,
0787: south, north, center, style, buttons, commonJS, header,
0788: footer);
0789:
0790: return frontend;
0791: }
0792:
0793: /**
0794: * creates a list of javascript pages (names) that contains javascript objects and methods that
0795: * are used by more than one module
0796: *
0797: * @param element
0798: * <CommonJS>
0799: *
0800: * @return instance of <tt>String[]</tt>
0801: *
0802: * @throws XMLParsingException
0803: */
0804: private static String[] createCommonJS(Element element) {
0805:
0806: String[] commonJS = null;
0807: if (element != null) {
0808: ElementList el = XMLTools.getChildElements("Name",
0809: CommonNamespaces.DGCNTXTNS, element);
0810: commonJS = new String[el.getLength()];
0811: for (int i = 0; i < commonJS.length; i++) {
0812: commonJS[i] = XMLTools.getStringValue(el.item(i));
0813: }
0814: }
0815:
0816: return commonJS;
0817: }
0818:
0819: /**
0820: * creates an instance of a class encapsulating the GUI description of one region of the GUI
0821: *
0822: * @param element
0823: * <West>; <East>; <South>; <North> or <Center>
0824: *
0825: * @return instance of <tt>GUIArea</tt>
0826: *
0827: * @throws XMLParsingException
0828: */
0829: private static GUIArea createGUIArea(Element element)
0830: throws XMLParsingException {
0831:
0832: GUIArea gui = null;
0833: if (element != null) {
0834: String tmp = element.getLocalName();
0835: int area = 0;
0836: if (tmp.equals("West")) {
0837: area = GUIArea.WEST;
0838: } else if (tmp.equals("East")) {
0839: area = GUIArea.EAST;
0840: } else if (tmp.equals("South")) {
0841: area = GUIArea.SOUTH;
0842: } else if (tmp.equals("North")) {
0843: area = GUIArea.NORTH;
0844: } else if (tmp.equals("Center")) {
0845: area = GUIArea.CENTER;
0846: }
0847:
0848: // hidden
0849: tmp = XMLTools.getAttrValue(element, null, "hidden", null);
0850: boolean hidden = "1".equals(tmp) || "true".equals(tmp);
0851: // <Module>
0852: ElementList el = XMLTools.getChildElements("Module",
0853: CommonNamespaces.DGCNTXTNS, element);
0854: Module[] modules = new Module[el.getLength()];
0855: for (int i = 0; i < modules.length; i++) {
0856: modules[i] = createModule(el.item(i));
0857: }
0858: gui = new GUIArea(area, hidden, modules);
0859: }
0860:
0861: return gui;
0862: }
0863:
0864: /**
0865: * creates an instance of a class encapsulating module informations
0866: *
0867: * @param element
0868: * <Module>
0869: *
0870: * @return instance of <tt>Module</tt>
0871: *
0872: * @throws XMLParsingException
0873: */
0874: private static Module createModule(Element element)
0875: throws XMLParsingException {
0876:
0877: // hidden
0878: String tmp = XMLTools.getAttrValue(element, null, "hidden",
0879: null);
0880: boolean hidden = tmp.equals("1") || tmp.equals("true");
0881: // <Name>
0882: String name = XMLTools.getRequiredStringValue("Name",
0883: CommonNamespaces.DGCNTXTNS, element);
0884: // <Content>
0885: String content = XMLTools.getRequiredStringValue("Content",
0886: CommonNamespaces.DGCNTXTNS, element);
0887: // <ModuleConfiguration>
0888: Element elem = XMLTools.getChildElement("ModuleConfiguration",
0889: CommonNamespaces.DGCNTXTNS, element);
0890: ModuleConfiguration mc = createModuleConfiguration(elem);
0891: // <ParameterList>
0892: elem = XMLTools.getChildElement("ParameterList",
0893: CommonNamespaces.DGCNTXTNS, element);
0894: ParameterList paramList = createParameterList(elem);
0895:
0896: String type = XMLTools
0897: .getAttrValue(element, null, "type", null);
0898:
0899: // width and height of a Module are optional
0900: // if not set '0' will be used instead
0901: tmp = XMLTools.getAttrValue(element, null, "width", null);
0902: int w = 0;
0903: try {
0904: w = Integer.parseInt(tmp);
0905: } catch (Exception e) {
0906: }
0907: tmp = XMLTools.getAttrValue(element, null, "height", null);
0908: int h = 0;
0909: try {
0910: h = Integer.parseInt(tmp);
0911: } catch (Exception e) {
0912: }
0913:
0914: String scrollable = XMLTools.getAttrValue(element, null,
0915: "scrolling", null);
0916:
0917: String[] moduleJS = createModuleJSList(element);
0918: Module module = new Module(name, content, hidden, type, w, h,
0919: scrollable, moduleJS, mc, paramList);
0920:
0921: return module;
0922: }
0923:
0924: /**
0925: * creates an instance of a class encapsulating the access the configuration of Module
0926: *
0927: * @param element
0928: * <ModuleConfiguration>
0929: *
0930: * @return instance of <tt>ModuleConfiguration</tt>
0931: *
0932: * @throws XMLParsingException
0933: */
0934: private static ModuleConfiguration createModuleConfiguration(
0935: Element element) throws XMLParsingException {
0936:
0937: ModuleConfiguration mc = null;
0938: if (element != null) {
0939: Element elem = XMLTools.getRequiredChildElement(
0940: "OnlineResource", CommonNamespaces.DGCNTXTNS,
0941: element);
0942: URL onlineResource = createOnlineResource(elem);
0943: mc = new ModuleConfiguration(onlineResource);
0944: }
0945:
0946: return mc;
0947: }
0948:
0949: /**
0950: * creates an instance of a class encapsulating the layer list informations
0951: *
0952: * @param element
0953: * <LayerList>
0954: *
0955: * @return instance of <tt>LayerList</tt>
0956: *
0957: * @throws XMLParsingException
0958: */
0959: private static ParameterList createParameterList(Element element)
0960: throws XMLParsingException {
0961:
0962: ParameterList parameterList = new ParameterList();
0963: if (element != null) {
0964: ElementList el = XMLTools.getChildElements("Parameter",
0965: CommonNamespaces.DGCNTXTNS, element);
0966: for (int i = 0; i < el.getLength(); i++) {
0967: Parameter parameter = createParameter(el.item(i));
0968: parameterList.addParameter(parameter);
0969: }
0970: }
0971:
0972: return parameterList;
0973: }
0974:
0975: /**
0976: * creates an instance of a class encapsulating a parameter that shall be passed to a module
0977: *
0978: * @param element
0979: * <Parameter>
0980: *
0981: * @return instance of <tt>Parameter</tt>
0982: *
0983: * @throws XMLParsingException
0984: */
0985: private static Parameter createParameter(Element element)
0986: throws XMLParsingException {
0987:
0988: String name = XMLTools.getRequiredStringValue("Name",
0989: CommonNamespaces.DGCNTXTNS, element);
0990: String value = XMLTools.getRequiredStringValue("Value",
0991: CommonNamespaces.DGCNTXTNS, element);
0992: // Parameter param = new Parameter_Impl( name+":"+value, value );
0993: Parameter param = new Parameter(name, value);
0994:
0995: return param;
0996: }
0997:
0998: /**
0999: * creates an instance of a class encapsulating informations about controlling options for a map
1000: * presented to the user
1001: *
1002: * @param element
1003: * <MapParameter>
1004: *
1005: * @return instance of <tt>MapParameter</tt>
1006: *
1007: * @throws XMLParsingException
1008: */
1009: private static MapParameter createMapParameter(Element element)
1010: throws XMLParsingException {
1011:
1012: // <OfferedInfoFormats>
1013: Element elem = XMLTools.getChildElement("OfferedInfoFormats",
1014: CommonNamespaces.DGCNTXTNS, element);
1015: Format[] infoFormats = null;
1016: if (elem != null) {
1017: infoFormats = createOfferedInfoFormats(elem);
1018: }
1019: // <OfferedZoomFactor>
1020: elem = XMLTools.getChildElement("OfferedZoomFactor",
1021: CommonNamespaces.DGCNTXTNS, element);
1022: MapOperationFactor[] zoomFactors = null;
1023: if (elem != null) {
1024: zoomFactors = createOfferedMapOperationFactors(elem);
1025: }
1026: // <OfferedPanFactor>
1027: elem = XMLTools.getChildElement("OfferedPanFactor",
1028: CommonNamespaces.DGCNTXTNS, element);
1029: MapOperationFactor[] panFactors = null;
1030: if (elem != null) {
1031: panFactors = createOfferedMapOperationFactors(elem);
1032: }
1033: // <MinScale>
1034: String tmp = XMLTools.getStringValue("MinScale",
1035: CommonNamespaces.DGCNTXTNS, element, "0");
1036: double minScale = Double.parseDouble(tmp);
1037: // <MaxScale>
1038: tmp = XMLTools.getStringValue("MaxScale",
1039: CommonNamespaces.DGCNTXTNS, element, "9999999999");
1040: double maxScale = Double.parseDouble(tmp);
1041:
1042: MapParameter mp = new MapParameter(infoFormats, panFactors,
1043: zoomFactors, minScale, maxScale);
1044:
1045: return mp;
1046: }
1047:
1048: /**
1049: * Creates a list of the feature info formats offered by the client.
1050: *
1051: * @param element
1052: * <OfferedInfoFormats> element of the configuration
1053: *
1054: * @return list of offered feature info formats
1055: *
1056: * @throws XMLParsingException
1057: */
1058: private static Format[] createOfferedInfoFormats(Element element)
1059: throws XMLParsingException {
1060:
1061: Format[] format = null;
1062:
1063: // get list of offered feature info formats
1064: ElementList el = XMLTools.getChildElements("Format",
1065: CommonNamespaces.DGCNTXTNS, element);
1066:
1067: format = new Format[el.getLength()];
1068:
1069: for (int i = 0; i < el.getLength(); i++) {
1070: String name = XMLTools.getStringValue(el.item(i));
1071: String sel = XMLTools.getAttrValue(el.item(i), null,
1072: "selected", null);
1073:
1074: boolean selected = "1".equals(sel) || "true".equals(sel);
1075: try {
1076: format[i] = new Format(name, selected);
1077: } catch (ContextException e) {
1078: throw new XMLParsingException("", e);
1079: }
1080: }
1081:
1082: return format;
1083: }
1084:
1085: /**
1086: * returns a list of offered numerical map operation factors that can be used to determine zoom
1087: * or pan levels
1088: *
1089: * @param element
1090: * a <tt>Element</tt> that contains <Factor> elements as children
1091: *
1092: * @return list of <tt>MapOperationFactor</tt>s
1093: *
1094: * @throws XMLParsingException
1095: */
1096: private static MapOperationFactor[] createOfferedMapOperationFactors(
1097: Element element) {
1098:
1099: // get list of offered factors
1100: ElementList el = XMLTools.getChildElements("Factor",
1101: CommonNamespaces.DGCNTXTNS, element);
1102:
1103: MapOperationFactor[] mof = new MapOperationFactor[el
1104: .getLength()];
1105:
1106: for (int i = 0; i < el.getLength(); i++) {
1107: boolean free = true;
1108: String tmp = XMLTools.getStringValue(el.item(i));
1109: double fac = -99;
1110:
1111: if (!tmp.equals("*")) {
1112: free = false;
1113: fac = Double.parseDouble(tmp);
1114: }
1115:
1116: String sel = XMLTools.getAttrValue(el.item(i), null,
1117: "selected", null);
1118: boolean selected = "1".equals(sel) || "true".equals(sel);
1119: mof[i] = new MapOperationFactor(fac, selected, free);
1120: }
1121:
1122: return mof;
1123: }
1124:
1125: /**
1126: * creates an instance of a class encapsulating the IO setting informations
1127: *
1128: * @param element
1129: * @param xml
1130: * @return
1131: * @throws XMLParsingException
1132: */
1133: private static IOSettings createIOSettings(Element element,
1134: XMLFragment xml) throws XMLParsingException,
1135: MalformedURLException {
1136:
1137: // temp directory
1138: Element elem = XMLTools.getChildElement("TempDirectory",
1139: CommonNamespaces.DGCNTXTNS, element);
1140: DirectoryAccess temp = null;
1141: if (elem != null) {
1142: temp = createDirectoryAccess(elem, null, xml);
1143: }
1144: // download directory
1145: elem = XMLTools.getChildElement("DownloadDirectory",
1146: CommonNamespaces.DGCNTXTNS, element);
1147: DirectoryAccess download = null;
1148: if (elem != null) {
1149: download = createDirectoryAccess(elem, temp, xml);
1150: }
1151: if (temp == null && elem == null) {
1152: throw new XMLParsingException(
1153: "If <TempDirectory> isn't set, "
1154: + "downloaddirectory must be set!");
1155: }
1156: // SLD directory
1157: elem = XMLTools.getChildElement("SLDDirectory",
1158: CommonNamespaces.DGCNTXTNS, element);
1159: DirectoryAccess sld = null;
1160: if (elem != null) {
1161: sld = createDirectoryAccess(elem, temp, xml);
1162: }
1163: if (temp == null && elem == null) {
1164: throw new XMLParsingException(
1165: "If <TempDirectory> isn't set, "
1166: + "slddirectory must be set!");
1167: }
1168: // Print directory
1169: elem = XMLTools.getChildElement("PrintDirectory",
1170: CommonNamespaces.DGCNTXTNS, element);
1171: DirectoryAccess print = null;
1172: if (elem != null) {
1173: print = createDirectoryAccess(elem, temp, xml);
1174: }
1175: if (temp == null && elem == null) {
1176: throw new XMLParsingException(
1177: "If <TempDirectory> isn't set, "
1178: + "printdirectory must be set!");
1179: }
1180:
1181: IOSettings ioSettings = new IOSettings(download, sld, print,
1182: temp);
1183:
1184: return ioSettings;
1185: }
1186:
1187: /**
1188: * @param element
1189: * @param tempDir
1190: * @return
1191: * @throws XMLParsingException
1192: */
1193: private static DirectoryAccess createDirectoryAccess(
1194: Element element, DirectoryAccess tempDir, XMLFragment xml)
1195: throws XMLParsingException, MalformedURLException {
1196:
1197: // directory name
1198: String name = XMLTools.getStringValue("Name",
1199: CommonNamespaces.DGCNTXTNS, element, null);
1200: name = xml.resolve(name).toExternalForm();
1201:
1202: URL url = null;
1203: Element elem = XMLTools.getChildElement("Access",
1204: CommonNamespaces.DGCNTXTNS, element);
1205: if (elem != null) {
1206: elem = XMLTools.getRequiredChildElement("OnlineResource",
1207: CommonNamespaces.CNTXTNS, elem);
1208: url = createOnlineResource(elem);
1209: }
1210:
1211: DirectoryAccess da = null;
1212: if (name == null || url == null) {
1213: da = tempDir;
1214: } else {
1215: da = new DirectoryAccess(
1216: xml.resolve(name).toExternalForm(), url);
1217: }
1218:
1219: return da;
1220:
1221: }
1222:
1223: /**
1224: * creates an instance of a class encapsulating the layer list informations
1225: *
1226: * @param element
1227: * <LayerList>
1228: * @param user
1229: * @param sessionID
1230: *
1231: * @return instance of <tt>LayerList</tt>
1232: *
1233: * @throws XMLParsingException
1234: */
1235: private static LayerList createLayerList(Element element,
1236: User user, String sessionID) throws XMLParsingException {
1237:
1238: ElementList el = XMLTools.getChildElements("Layer",
1239: CommonNamespaces.CNTXTNS, element);
1240: Layer[] layers = new Layer[el.getLength()];
1241: for (int i = 0; i < layers.length; i++) {
1242: layers[i] = createLayer(el.item(i), user, sessionID);
1243: }
1244: LayerList list = new LayerList(layers);
1245:
1246: return list;
1247: }
1248:
1249: /**
1250: * creates an instance of a class encapsulating a web map context layer's attributes
1251: *
1252: * @param element
1253: * <Layer>
1254: * @param user
1255: * @param sessionID
1256: *
1257: * @return instance of <tt>Layer</tt>
1258: *
1259: * @throws XMLParsingException
1260: */
1261: private static Layer createLayer(Element element, User user,
1262: String sessionID) throws XMLParsingException {
1263:
1264: String tmp = XMLTools.getRequiredAttrValue("queryable", null,
1265: element);
1266: boolean queryable = "1".equals(tmp) || "true".equals(tmp);
1267: tmp = XMLTools.getRequiredAttrValue("hidden", null, element);
1268: boolean hidden = "1".equals(tmp) || "true".equals(tmp);
1269:
1270: // <Name>
1271: String name = XMLTools.getRequiredStringValue("Name",
1272: CommonNamespaces.CNTXTNS, element);
1273: // <Title>
1274: String title = XMLTools.getRequiredStringValue("Title",
1275: CommonNamespaces.CNTXTNS, element);
1276: // <Abstract>
1277: String abstract_ = XMLTools.getStringValue("Abstract",
1278: CommonNamespaces.CNTXTNS, element, null);
1279: // <DataURL>
1280: Element elem = XMLTools.getChildElement("DataURL",
1281: CommonNamespaces.CNTXTNS, element);
1282: BaseURL dataURL = createBaseURL(elem);
1283: // <MetaDataURL>
1284: elem = XMLTools.getChildElement("MetaDataURL",
1285: CommonNamespaces.CNTXTNS, element);
1286: BaseURL metadataURL = createBaseURL(elem);
1287: // <SRS>
1288: tmp = XMLTools.getStringValue("SRS", CommonNamespaces.CNTXTNS,
1289: element, null);
1290: String[] srs = StringTools.toArray(tmp, ",; ", true);
1291: // <FormatList>
1292: elem = XMLTools.getChildElement("FormatList",
1293: CommonNamespaces.CNTXTNS, element);
1294: FormatList formatList = createFormatList(elem);
1295: // <Extension>
1296: elem = XMLTools.getChildElement("Extension",
1297: CommonNamespaces.CNTXTNS, element);
1298: LayerExtension extension = createLayerExtension(elem, user,
1299: sessionID);
1300: // <Server>
1301: elem = XMLTools.getRequiredChildElement("Server",
1302: CommonNamespaces.CNTXTNS, element);
1303: Server server = createServer(elem, user, sessionID, extension
1304: .getAuthentication());
1305: // <StyleList>
1306: elem = XMLTools.getChildElement("StyleList",
1307: CommonNamespaces.CNTXTNS, element);
1308: StyleList styleList = createStyleList(elem, name, server);
1309:
1310: Layer layer = null;
1311: try {
1312: layer = new Layer(server, name, title, abstract_, srs,
1313: dataURL, metadataURL, formatList, styleList,
1314: queryable, hidden, extension);
1315: } catch (Exception e) {
1316: throw new XMLParsingException(
1317: "couldn't create map context layer", e);
1318: }
1319:
1320: return layer;
1321: }
1322:
1323: /**
1324: * creates an instance of a class encapsulating informations about the server (service) a layer
1325: * based on
1326: *
1327: * @param element
1328: * <Server>
1329: * @param user
1330: * @param sessionID
1331: * @param useAuthentication
1332: *
1333: * @return instance of <tt>Server</tt>
1334: *
1335: * @throws XMLParsingException
1336: */
1337: private static Server createServer(Element element, User user,
1338: String sessionID, int useAuthentication)
1339: throws XMLParsingException {
1340:
1341: String service = XMLTools.getRequiredAttrValue("service", null,
1342: element);
1343: String version = XMLTools.getRequiredAttrValue("version", null,
1344: element);
1345: String title = XMLTools.getRequiredAttrValue("title", null,
1346: element);
1347: // <OnlineResource>
1348: Element elem = XMLTools.getRequiredChildElement(
1349: "OnlineResource", CommonNamespaces.CNTXTNS, element);
1350: URL onlineResource = createOnlineResource(elem);
1351:
1352: OGCCapabilities capabilities = getCapabilities(onlineResource,
1353: service, version, user, sessionID, useAuthentication);
1354:
1355: Server server = null;
1356: try {
1357: server = new Server(title, version, service,
1358: onlineResource, capabilities);
1359: } catch (Exception e) {
1360: LOG.logError(e.getMessage(), e);
1361: throw new XMLParsingException(
1362: "could not create context server", e);
1363: }
1364:
1365: return server;
1366: }
1367:
1368: /**
1369: * creates an instance of a class encapsulating a list of image formats a layer offers
1370: *
1371: * @param element
1372: * <FormatList>
1373: *
1374: * @return instance of <tt>FormatList</tt>
1375: *
1376: * @throws XMLParsingException
1377: */
1378: private static FormatList createFormatList(Element element)
1379: throws XMLParsingException {
1380:
1381: ElementList el = XMLTools.getChildElements("Format",
1382: CommonNamespaces.CNTXTNS, element);
1383: Format[] formats = new Format[el.getLength()];
1384: for (int i = 0; i < formats.length; i++) {
1385: String name = XMLTools.getStringValue(el.item(i));
1386: String tmp = XMLTools.getAttrValue(el.item(i), null,
1387: "current", null);
1388: boolean current = "1".equals(tmp) || "true".equals(tmp)
1389: || formats.length == 1;
1390: try {
1391: formats[i] = new Format(name, current);
1392: } catch (Exception e) {
1393: throw new XMLParsingException(
1394: "could not create context format", e);
1395: }
1396: }
1397:
1398: FormatList formatList = null;
1399: try {
1400: formatList = new FormatList(formats);
1401: } catch (Exception e) {
1402: LOG.logDebug(e.getMessage(), e);
1403: throw new XMLParsingException(
1404: "could not create context formatList", e);
1405: }
1406:
1407: return formatList;
1408: }
1409:
1410: /**
1411: * creates an instance of a class encapsulating a list of styles available for a layer
1412: *
1413: * @param element
1414: * <StyleList>
1415: * @param layerName
1416: * @param server
1417: *
1418: * @return instance of <tt>StyleList</tt>
1419: *
1420: * @throws XMLParsingException
1421: */
1422: private static StyleList createStyleList(Element element,
1423: String layerName, Server server) throws XMLParsingException {
1424:
1425: ElementList el = XMLTools.getChildElements("Style",
1426: CommonNamespaces.CNTXTNS, element);
1427: Style[] styles = new Style[el.getLength()];
1428: for (int i = 0; i < styles.length; i++) {
1429: styles[i] = createStyle(el.item(i), layerName, server);
1430: }
1431: StyleList styleList = null;
1432: try {
1433: styleList = new StyleList(styles);
1434: } catch (Exception e) {
1435: LOG.logDebug(e.getMessage(), e);
1436: throw new XMLParsingException(
1437: "could not create context stylelist", e);
1438: }
1439:
1440: return styleList;
1441: }
1442:
1443: /**
1444: * creates an instance of a class encapsulating a description of a Style
1445: *
1446: * @param element
1447: * <Style>
1448: *
1449: * @return instance of <tt>Style</tt>
1450: *
1451: * @throws XMLParsingException
1452: */
1453: private static Style createStyle(Element element, String layerName,
1454: Server server) throws XMLParsingException {
1455:
1456: Style style = null;
1457:
1458: String tmp = XMLTools.getAttrValue(element, null, "current",
1459: null);
1460: boolean current = "1".equals(tmp) || "true".equals(tmp);
1461:
1462: Element elem = XMLTools.getChildElement("SLD",
1463: CommonNamespaces.CNTXTNS, element);
1464: if (elem != null) {
1465: SLD sld = createSLD(elem);
1466: try {
1467: style = new Style(sld, current);
1468: } catch (Exception e) {
1469: throw new XMLParsingException(
1470: "could not create context style", e);
1471: }
1472: } else {
1473: String name = XMLTools.getRequiredStringValue("Name",
1474: CommonNamespaces.CNTXTNS, element);
1475: String title = XMLTools.getRequiredStringValue("Title",
1476: CommonNamespaces.CNTXTNS, element);
1477: String abstract_ = XMLTools.getStringValue("Abstract",
1478: CommonNamespaces.CNTXTNS, element, null);
1479: // <LegendURL>
1480: elem = XMLTools.getChildElement("LegendURL",
1481: CommonNamespaces.CNTXTNS, element);
1482: ImageURL legendURL = null;
1483: if (elem != null) {
1484: legendURL = createImageURL(elem);
1485: } else {
1486: try {
1487: legendURL = createLegendURL(name, layerName, server);
1488: } catch (Exception e) {
1489: throw new XMLParsingException(
1490: "could not create context style", e);
1491: }
1492: }
1493: try {
1494: style = new Style(name, title, abstract_, legendURL,
1495: current);
1496: } catch (Exception e) {
1497: LOG.logDebug(e.getMessage(), e);
1498: throw new XMLParsingException(
1499: "could not create context style", e);
1500: }
1501: }
1502:
1503: return style;
1504: }
1505:
1506: /**
1507: * creates a legendURL for a style/layer by evaluating the servers capabilities the layer is
1508: * servered by.
1509: *
1510: * @param layer
1511: * @return
1512: * @throws XMLParsingException
1513: */
1514: private static ImageURL createLegendURL(String styleName,
1515: String layerName, Server server) throws XMLParsingException {
1516:
1517: WMSCapabilities capa = (WMSCapabilities) server
1518: .getCapabilities();
1519: ImageURL legendURL = null;
1520: if (capa != null) {
1521: org.deegree.ogcwebservices.wms.capabilities.Layer layer = capa
1522: .getLayer(layerName);
1523: // FIXME null layer will produce NullPointerException@layer.getStyles()
1524: // is this exception correct ?
1525: if (layer == null) {
1526: throw new XMLParsingException("LayerName: " + layerName
1527: + " does not exist in the WMSCapabilities");
1528: }
1529:
1530: org.deegree.ogcwebservices.wms.capabilities.Style[] styles = layer
1531: .getStyles();
1532: org.deegree.ogcwebservices.wms.capabilities.Style style = null;
1533: for (int i = 0; i < styles.length; i++) {
1534: // find responsible style definition
1535: style = styles[i];
1536: if (style.getName().equals(styleName)) {
1537: break;
1538: }
1539: }
1540:
1541: LegendURL[] urls = null;
1542:
1543: if (style != null) {
1544: urls = style.getLegendURL();
1545: }
1546:
1547: if (urls != null && urls.length > 0 && urls[0] != null) {
1548: // if style has defined LegendURL(s) take the first
1549: legendURL = new ImageURL(urls[0].getWidth(), urls[0]
1550: .getHeight(), urls[0].getFormat(), urls[0]
1551: .getOnlineResource());
1552: } else {
1553: // create a GetLegendGraphic request as style URL if the server
1554: // supports GetLegendGraphic operation
1555: OperationsMetadata om = capa.getOperationMetadata();
1556: Operation operation = om
1557: .getOperation(new QualifiedName(
1558: "GetLegendGraphic"));
1559: if (operation != null) {
1560: HTTP http = (HTTP) operation.getDCP().get(0);
1561: URL url = http.getGetOnlineResources().get(0);
1562: StringBuffer sb = new StringBuffer(500);
1563: sb.append(OWSUtils.validateHTTPGetBaseURL(url
1564: .toExternalForm()));
1565: GetLegendGraphic glg = GetLegendGraphic.create(
1566: "12", capa.getVersion(), layerName,
1567: styleName, null, null, 1, null, null,
1568: "image/jpeg", 20, 20, null, null);
1569: try {
1570: sb.append(glg.getRequestParameter());
1571: url = new URL(sb.toString());
1572: legendURL = new ImageURL(20, 20, "image/jpeg",
1573: url);
1574: } catch (Exception shouldNeverHappen) {
1575: shouldNeverHappen.printStackTrace();
1576: }
1577: }
1578: }
1579: }
1580:
1581: return legendURL;
1582: }
1583:
1584: /**
1585: * creates an instance of a class encapsulating a description of a Style based on a SLD
1586: *
1587: * @param element
1588: * <SLD>
1589: *
1590: * @return instance of <tt>SLD</tt>
1591: *
1592: * @throws XMLParsingException
1593: */
1594: private static SLD createSLD(Element element)
1595: throws XMLParsingException {
1596:
1597: SLD sld = null;
1598:
1599: String name = XMLTools.getRequiredStringValue("Name",
1600: CommonNamespaces.CNTXTNS, element);
1601: String title = XMLTools.getStringValue("Title",
1602: CommonNamespaces.CNTXTNS, element, null);
1603:
1604: Element elem = XMLTools.getChildElement("OnlineResource",
1605: CommonNamespaces.CNTXTNS, element);
1606: try {
1607: if (elem != null) {
1608: URL onlineResource = createOnlineResource(elem);
1609: sld = new SLD(name, title, onlineResource);
1610: } else {
1611: elem = XMLTools.getChildElement(
1612: "StyledLayerDescriptor",
1613: CommonNamespaces.SLDNS, element);
1614: if (elem != null) {
1615: XMLFragment xml = new XMLFragment();
1616: xml.setRootElement(elem);
1617: StyledLayerDescriptor styledLayerDescriptor = SLDFactory
1618: .createSLD(xml);
1619: sld = new SLD(name, title, styledLayerDescriptor);
1620: } else {
1621: FeatureTypeStyle fts = SLDFactory
1622: .createFeatureTypeStyle(elem);
1623: sld = new SLD(name, title, fts);
1624: }
1625: }
1626: } catch (Exception e) {
1627: LOG.logDebug(e.getMessage(), e);
1628: throw new XMLParsingException(
1629: "couldn't create map context SLD", e);
1630: }
1631:
1632: return sld;
1633: }
1634:
1635: /**
1636: * creates an instance of a class encapsulating the deegree specific extensions of a Layer
1637: *
1638: * @param element
1639: * Extension
1640: * @param user
1641: * @param sessionID
1642: *
1643: * @return instance of <tt>LayerExtension</tt>
1644: *
1645: * @throws XMLParsingException
1646: */
1647: private static LayerExtension createLayerExtension(Element element,
1648: User user, String sessionID) throws XMLParsingException {
1649:
1650: LayerExtension le = null;
1651: if (element != null) {
1652:
1653: String tmp = XMLTools.getNodeAsString(element,
1654: "./dgcntxt:UseAuthentication/text()",
1655: CommonNamespaces.getNamespaceContext(), "NONE");
1656: int ua = LayerExtension.NONE;
1657: if ("sessionID".equalsIgnoreCase(tmp)) {
1658: ua = LayerExtension.SESSIONID;
1659: } else if ("user/password".equalsIgnoreCase(tmp)) {
1660: ua = LayerExtension.USERPASSWORD;
1661: }
1662:
1663: DataService dataService = null;
1664: Element elem = XMLTools.getChildElement("DataService",
1665: CommonNamespaces.DGCNTXTNS, element);
1666: if (elem != null) {
1667: Element el = XMLTools.getRequiredChildElement("Server",
1668: CommonNamespaces.CNTXTNS, elem);
1669: Server server = createServer(el, user, sessionID, ua);
1670: String geoType = XMLTools.getStringValue(
1671: "GeometryType", CommonNamespaces.DGCNTXTNS,
1672: elem, null);
1673: String featureType = XMLTools.getStringValue(
1674: "FeatureType", CommonNamespaces.DGCNTXTNS,
1675: elem, null);
1676: dataService = new DataService(server, featureType,
1677: geoType);
1678: }
1679: boolean masterLayer = false;
1680: elem = XMLTools.getChildElement("MasterLayer",
1681: CommonNamespaces.DGCNTXTNS, element);
1682: if (elem != null) {
1683: String s = XMLTools.getStringValue(elem);
1684: masterLayer = "true".equals(s) || "1".equals(s);
1685: }
1686: elem = XMLTools.getChildElement("ScaleHint",
1687: CommonNamespaces.DGCNTXTNS, element);
1688: double min = 0;
1689: double max = 9E99;
1690: if (elem != null) {
1691: String s = XMLTools.getRequiredAttrValue("min", null,
1692: elem);
1693: min = Double.parseDouble(s);
1694: s = XMLTools.getRequiredAttrValue("max", null, elem);
1695: max = Double.parseDouble(s);
1696: } else {
1697: /*
1698: * double[] sc = getScaleHintFromCapabilities(mapServer, name); min = sc[0]; max =
1699: * sc[1];
1700: */
1701: }
1702: boolean selectedForFI = false;
1703: elem = XMLTools.getChildElement("SelectedForQuery",
1704: CommonNamespaces.DGCNTXTNS, element);
1705: if (elem != null) {
1706: String s = XMLTools.getStringValue(elem);
1707: selectedForFI = "true".equals(s) || "1".equals(s);
1708: }
1709:
1710: int parentNodeId = 1;
1711: elem = XMLTools.getChildElement("parentNodeId",
1712: CommonNamespaces.DGCNTXTNS, element);
1713: if (elem != null) {
1714: parentNodeId = Integer.parseInt(XMLTools
1715: .getStringValue(elem));
1716: }
1717: boolean showLegendGraphic = false;
1718: elem = XMLTools.getChildElement("showLegendGraphic",
1719: CommonNamespaces.DGCNTXTNS, element);
1720: if (elem != null) {
1721: String s = XMLTools.getStringValue(elem);
1722: showLegendGraphic = "true".equals(s) || "1".equals(s);
1723: }
1724: le = new LayerExtension(dataService, masterLayer, min, max,
1725: selectedForFI, ua, parentNodeId, showLegendGraphic);
1726:
1727: } else {
1728: le = new LayerExtension();
1729: }
1730:
1731: return le;
1732: }
1733:
1734: /**
1735: * returns the capabilities for the passed server. The capabilities are read directly from the
1736: * server or from the loacal cache.
1737: *
1738: * @param url
1739: * @param service
1740: * @param version
1741: * @param user
1742: * @param sessionID
1743: * @param useAuthentication
1744: * @return he capabilities for the passed server. The capabilities are read directly from the
1745: * server or from the loacal cache.
1746: */
1747: private static OGCCapabilities getCapabilities(URL url,
1748: String service, String version, User user,
1749: String sessionID, int useAuthentication) {
1750:
1751: OGCCapabilities capa = null;
1752:
1753: String href = OWSUtils.validateHTTPGetBaseURL(url
1754: .toExternalForm());
1755: if (href.toLowerCase().startsWith("http://")
1756: || href.toLowerCase().startsWith("https://")) {
1757: if ("OGC:WMS".equals(service)) {
1758: // is a HTTP URL so GetCapabilities request must be constructed
1759: if ("1.0.0".equals(version)) {
1760: href = StringTools.concat(1000, href,
1761: "request=capabilities&service=WMS",
1762: "&WMTVER=", version);
1763: } else {
1764: href = StringTools.concat(1000, href,
1765: "request=GetCapabilities&service=WMS",
1766: "&version=", version);
1767: }
1768:
1769: } else if ("OGC:WFS".equals(service)) {
1770: // is a HTTP URL so GetCapabilities request must be constructed
1771: href = StringTools.concat(1000, href,
1772: "request=GetCapabilities&service=WFS",
1773: "&version=", version);
1774:
1775: } else if ("OGC:WCS".equals(service)) {
1776: // is a HTTP URL so GetCapabilities request must be constructed
1777: href = StringTools.concat(1000, href,
1778: "request=GetCapabilities&service=WCS",
1779: "&version=", version);
1780:
1781: }
1782:
1783: if (useAuthentication == LayerExtension.SESSIONID) {
1784: href = StringTools.concat(1000, href, "&sessionID=",
1785: sessionID);
1786: } else if (useAuthentication == LayerExtension.USERPASSWORD) {
1787: href = StringTools.concat(1000, href, "&user=", user
1788: .getName(), "&password=", user.getPassword());
1789: }
1790: }
1791:
1792: if (wmsCache.get(url) == null && "OGC:WMS".equals(service)) {
1793: LOG.logDebug("get " + service
1794: + " capabilities from GetCapabilities request");
1795: capa = parseCapabilities(href, service);
1796: if (capa != null) {
1797: // write capabilities into local cache
1798: wmsCache.put(url, (WMSCapabilities) capa);
1799: }
1800: } else if ("OGC:WMS".equals(service)) {
1801: LOG.logDebug("get WMS capabilities from cache");
1802: capa = wmsCache.get(url);
1803: } else if (wfsCache.get(url) == null
1804: && "OGC:WFS".equals(service)) {
1805: LOG.logDebug("get " + service
1806: + " capabilities from GetCapabilities request");
1807: capa = parseCapabilities(href, service);
1808: if (capa != null) {
1809: // write capabilities into local cache
1810: wfsCache.put(url, (WFSCapabilities) capa);
1811: }
1812: } else if ("OGC:WFS".equals(service)) {
1813: LOG.logDebug("get WFS capabilities from cache");
1814: capa = wfsCache.get(url);
1815: } else if (wcsCache.get(url) == null
1816: && "OGC:WCS".equals(service)) {
1817: LOG.logDebug("get " + service
1818: + " capabilities from GetCapabilities request");
1819: capa = parseCapabilities(href, service);
1820: if (capa != null) {
1821: // write capabilities into local cache
1822: wcsCache.put(url, (WCSCapabilities) capa);
1823: }
1824: } else if ("OGC:WFS".equals(service)) {
1825: LOG.logDebug("get WCS capabilities from cache");
1826: capa = wcsCache.get(url);
1827: }
1828:
1829: return capa;
1830: }
1831:
1832: /**
1833: *
1834: * @param href
1835: * @param service
1836: * @return
1837: */
1838: private static OGCCapabilities parseCapabilities(String href,
1839: String service) {
1840:
1841: OGCCapabilities capa = null;
1842: try {
1843: URL url = null;
1844: Reader reader = null;
1845:
1846: // consider that the reference to the capabilities may has been
1847: // made by a file URL to a local copy
1848: if (href.toLowerCase().startsWith("http://")
1849: || href.toLowerCase().startsWith("https://")) {
1850: HttpClient httpclient = new HttpClient();
1851: httpclient = WebUtils.enableProxyUsage(httpclient,
1852: new URL(href));
1853: int timeout = Integer.parseInt(Messages
1854: .getString("WebMapContextFactory.timeout"));
1855: httpclient.getHttpConnectionManager().getParams()
1856: .setSoTimeout(timeout);
1857:
1858: GetMethod httpget = new GetMethod(href);
1859: LOG.logDebug("GetCapabilities: ", href);
1860:
1861: httpclient.executeMethod(httpget);
1862: reader = new InputStreamReader(httpget
1863: .getResponseBodyAsStream());
1864: } else {
1865: if (href.endsWith("?")) {
1866: url = new URL(href.substring(0, href.length() - 1));
1867: }
1868: reader = new InputStreamReader(url.openStream());
1869: }
1870:
1871: OGCCapabilitiesDocument doc = null;
1872: if ("OGC:WMS".equals(service)) {
1873: doc = new WMSCapabilitiesDocument();
1874: doc.load(reader, XMLFragment.DEFAULT_URL);
1875: doc = WMSCapabilitiesDocumentFactory
1876: .getWMSCapabilitiesDocument(doc
1877: .getRootElement());
1878: } else if ("OGC:WFS".equals(service)) {
1879: doc = new WFSCapabilitiesDocument();
1880: doc.load(reader, XMLFragment.DEFAULT_URL);
1881: } else if ("OGC:WCS".equals(service)) {
1882: doc = new WCSCapabilitiesDocument();
1883: doc.load(reader, XMLFragment.DEFAULT_URL);
1884: } else {
1885: throw new XMLParsingException(
1886: "not supported service type: " + service);
1887: }
1888:
1889: capa = doc.parseCapabilities();
1890: } catch (Exception e) {
1891: System.out.println("could not read capabilities: ");
1892: System.out.println(href);
1893: LOG.logError(e.getMessage(), e);
1894: return null;
1895: }
1896: return capa;
1897: }
1898:
1899: /**
1900: * @param mapServer
1901: * @param layer
1902: * @return ScaleHint
1903: */
1904: private static double[] getScaleHintFromCapabilities(
1905: Server mapServer, String layer) {
1906:
1907: double[] sc = new double[] { 0, 9999999 };
1908: WMSCapabilities capa = (WMSCapabilities) mapServer
1909: .getCapabilities();
1910: if (capa != null) {
1911: org.deegree.ogcwebservices.wms.capabilities.Layer lay = capa
1912: .getLayer(layer);
1913: if (lay != null) {
1914: sc[0] = lay.getScaleHint().getMin();
1915: sc[1] = lay.getScaleHint().getMax();
1916: }
1917: }
1918: return sc;
1919: }
1920:
1921: /**
1922: * creates a list (String[]) containing the name of the JavaScript files used by the moudle
1923: *
1924: * @param element
1925: * <Module>
1926: *
1927: * @return instance of <tt>String[]</tt>
1928: *
1929: * @throws XMLParsingException
1930: */
1931: private static String[] createModuleJSList(Element element) {
1932:
1933: String[] moduleJS = null;
1934: if (element != null) {
1935: ElementList el = XMLTools.getChildElements("ModuleJS",
1936: CommonNamespaces.DGCNTXTNS, element);
1937: moduleJS = new String[el.getLength()];
1938: for (int i = 0; i < el.getLength(); i++) {
1939: moduleJS[i] = ((Text) el.item(i).getFirstChild())
1940: .getData();
1941: }
1942: }
1943:
1944: return moduleJS;
1945: }
1946: }
|