1: <?php
2:
3: namespace Mapbender\WmsBundle\Component;
4:
5: use Mapbender\CoreBundle\Component\Exception\XmlParseException;
6: use Mapbender\CoreBundle\Component\Exception\NotSupportedVersionException;
7: use Mapbender\WmsBundle\Component\Exception\WmsException;
8:
9: /**
10: * Class that Parses WMS GetCapabilies Document
11: * Parses WMS GetCapabilities documents
12: *
13: * @author Karim Malhas
14: * @author Paul Schmidt
15: */
16: abstract class WmsCapabilitiesParser
17: {
18:
19: /**
20: * The XML representation of the Capabilites Document
21: * @var DOMDocument
22: */
23: protected $doc;
24:
25: /**
26: * An Xpath-instance
27: */
28: protected $xpath;
29:
30: /**
31: * The resolution
32: *
33: * @var integer
34: */
35: protected $resolution = 72;
36:
37: /**
38: * Creates an instance
39: *
40: * @param \DOMDocument $doc
41: */
42: public function __construct(\DOMDocument $doc)
43: {
44: $this->doc = $doc;
45: $this->xpath = new \DOMXPath($doc);
46: $this->xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");
47: }
48:
49: /**
50: * Sets the resolution
51: *
52: * @param integer $resolution
53: */
54: protected function setReslolution($resolution){
55: $this->resolution = $resolution;
56: }
57:
58: /**
59: * Finds the value
60: * @param string $xpath xpath expression
61: * @param \DOMNode $contextElm the node to use as context for evaluating the
62: * XPath expression.
63: * @return string the value of item or the selected item or null
64: */
65: protected function getValue($xpath, $contextElm = null)
66: {
67: if(!$contextElm)
68: {
69: $contextElm = $this->doc;
70: }
71: try
72: {
73: $elm = $this->xpath->query($xpath, $contextElm)->item(0);
74: if($elm->nodeType == XML_ATTRIBUTE_NODE)
75: {
76: return $elm->value;
77: } else if($elm->nodeType == XML_TEXT_NODE)
78: {
79: return $elm->wholeText;
80: } else if($elm->nodeType == XML_ELEMENT_NODE)
81: {
82: return $elm;
83: } else
84: {
85: return null;
86: }
87: } catch(\Exception $E)
88: {
89: return null;
90: }
91: }
92:
93: /**
94: * Parses the capabilities document
95: */
96: abstract public function parse();
97:
98: /**
99: * Creates a document
100: *
101: * @param string $data the string containing the XML
102: * @param boolean $validate to validate of xml
103: * @return \DOMDocument a GetCapabilites document
104: * @throws XmlParseException if a GetCapabilities xml is not valid
105: * @throws WmsException if an service exception
106: * @throws NotSupportedVersionException if a service version is not supported
107: */
108: public static function createDocument($data, $validate = false)
109: {
110: $doc = new \DOMDocument();
111: if(!@$doc->loadXML($data))
112: {
113: throw new XmlParseException("Could not parse CapabilitiesDocument.");
114: }
115:
116: if($doc->documentElement->tagName == "ServiceExceptionReport")
117: {
118: $message = $doc->documentElement->nodeValue;
119: throw new WmsException($message);
120: }
121:
122: if($doc->documentElement->tagName !== "WMS_Capabilities"
123: && $doc->documentElement->tagName !== "WMT_MS_Capabilities")
124: {
125: throw new NotSupportedVersionException("No supported CapabilitiesDocument");
126: }
127:
128: if($validate && !@$this->doc->validate())
129: {
130: // TODO logging
131: }
132:
133: $version = $doc->documentElement->getAttribute("version");
134: if($version !== "1.1.1" && $version !== "1.3.0")
135: {
136: throw new NotSupportedVersionException('The WMS version "'
137: . $version . '" is not supported.');
138: }
139: return $doc;
140: }
141:
142: /**
143: * Gets a capabilities parser
144: *
145: * @param \DOMDocument $doc the GetCapabilities document
146: * @return \Mapbender\WmsBundle\Component\WmsCapabilitiesParser111
147: * |\Mapbender\WmsBundle\Component\WmsCapabilitiesParser130 a capabilities parser
148: * @throws NotSupportedVersionException if a service version is not supported
149: */
150: public static function getParser(\DOMDocument $doc)
151: {
152: $version = $doc->documentElement->getAttribute("version");
153: switch($version)
154: {
155: case "1.1.1":
156: return new WmsCapabilitiesParser111($doc);
157: case "1.3.0":
158: return new WmsCapabilitiesParser130($doc);
159: default:
160: throw new NotSupportedVersionException("Could not determine WMS Version");
161: break;
162: }
163: }
164:
165: }
166: