1: <?php
2:
3: namespace Mapbender\WmcBundle\Component;
4:
5: use Mapbender\CoreBundle\Component\Exception\XmlParseException;
6: use Mapbender\CoreBundle\Component\Exception\NotSupportedVersionException;
7: use Mapbender\WmcBundle\Component\Exception\WmcException;
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 WmcParser
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: * Creates an instance
32: *
33: * @param \DOMDocument $doc
34: */
35: public function __construct(\DOMDocument $doc)
36: {
37: $this->doc = $doc;
38: $this->xpath = new \DOMXPath($doc);
39: $this->xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");
40: }
41:
42: /**
43: * Finds the value
44: * @param string $xpath xpath expression
45: * @param \DOMNode $contextElm the node to use as context for evaluating the
46: * XPath expression.
47: * @return string the value of item or the selected item or null
48: */
49: protected function getValue($xpath, $contextElm = null)
50: {
51: if(!$contextElm)
52: {
53: $contextElm = $this->doc;
54: }
55: try
56: {
57: $elm = $this->xpath->query($xpath, $contextElm)->item(0);
58: if($elm->nodeType == XML_ATTRIBUTE_NODE)
59: {
60: return $elm->value;
61: } else if($elm->nodeType == XML_TEXT_NODE)
62: {
63: return $elm->wholeText;
64: } else if($elm->nodeType == XML_ELEMENT_NODE)
65: {
66: return $elm;
67: } else
68: {
69: return null;
70: }
71: } catch(\Exception $E)
72: {
73: return null;
74: }
75: }
76:
77: /**
78: * Parses the capabilities document
79: */
80: abstract public function parse();
81:
82: /**
83: * Creates a document
84: *
85: * @param string $data the string containing the XML
86: * @param boolean $validate to validate of xml
87: * @return \DOMDocument a WMC document
88: * @throws XmlParseException if a GetCapabilities xml is not valid
89: * @throws NotSupportedVersionException if a wmc version is not supported
90: */
91: public static function createDocument($data, $validate = false)
92: {
93: $doc = new \DOMDocument();
94: if(!@$doc->loadXML($data))
95: {
96: throw new XmlParseException("Could not parse Wmc Document.");
97: }
98:
99: if($doc->documentElement->tagName !== "ViewContext")
100: {
101: throw new WmcException("Not supported Wmc Document");
102: }
103:
104: if($validate && !@$this->doc->validate())
105: {
106: // TODO logging
107: }
108:
109: $version = $doc->documentElement->getAttribute("version");
110: if($version !== "1.1.0")
111: {
112: throw new NotSupportedVersionException('The WMC version "'
113: . $version . '" is not supported.');
114: }
115: return $doc;
116: }
117:
118: /**
119: * Returns a wmc parser
120: *
121: * @param \DOMDocument $doc the WMC document
122: * @return \Mapbender\WmsBundle\Component\WmcParser110
123: * @throws NotSupportedVersionException if a version is not supported
124: */
125: public static function getParser(\DOMDocument $doc)
126: {
127: $version = $doc->documentElement->getAttribute("version");
128: switch($version)
129: {
130: case "1.1.0":
131: return new WmcParser110($doc);
132: default:
133: throw new NotSupportedVersionException("Could not determine WMC Version");
134: break;
135: }
136: }
137:
138: }
139: