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: return WmcParser::checkWmcDocument($doc, $validate);
99: }
100:
101: /**
102: * Checks the wmc xml
103: *
104: * @param \DOMDocument $doc the wmc xml to check
105: * @param boolean $validate to validate of xml
106: * @return \DOMDocument checked wmc document
107: * @throws WmcException if a xml is not a wmc document.
108: * @throws NotSupportedVersionException if a wmc version is not supported
109: */
110: private static function checkWmcDocument(\DOMDocument $doc, $validate = false)
111: {
112: if($doc->documentElement->tagName !== "ViewContext")
113: {
114: throw new WmcException("Not supported Wmc Document");
115: }
116:
117: if($validate && !@$this->doc->validate())
118: {
119: // TODO logging
120: }
121:
122: $version = $doc->documentElement->getAttribute("version");
123: if($version !== "1.1.0")
124: {
125: throw new NotSupportedVersionException('The WMC version "'
126: . $version . '" is not supported.');
127: }
128: return $doc;
129: }
130:
131: /**
132: * Loads a wmc document from a file
133: *
134: * @param string $file path to wmc document
135: * @param boolean $validate to validate of xml
136: * @return \DOMDocument a WMC document
137: * @throws XmlParseException if a file is not a wmc document.
138: */
139: public static function loadDocument($file, $validate = false)
140: {
141: $doc = new \DOMDocument();
142: if(!@$doc->load($file))
143: {
144: throw new XmlParseException("Could not parse Wmc Document.");
145: }
146: return WmcParser::checkWmcDocument($doc, $validate);
147: }
148:
149: /**
150: * Returns a wmc parser
151: *
152: * @param \DOMDocument $doc the WMC document
153: * @return \Mapbender\WmsBundle\Component\WmcParser110
154: * @throws NotSupportedVersionException if a version is not supported
155: */
156: public static function getParser(\DOMDocument $doc)
157: {
158: $version = $doc->documentElement->getAttribute("version");
159: switch($version)
160: {
161: case "1.1.0":
162: return new WmcParser110($doc);
163: default:
164: throw new NotSupportedVersionException("Could not determine WMC Version");
165: break;
166: }
167: }
168:
169: }
170: