|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +/// TO-DO: Rename the class to `GMUKMLParser` once the linking is done and remove the objective c class. |
| 14 | +/// This extension provides the delegate(`XMLParserDelegate`) implementations, of the `GMUKMLParser` class. |
| 15 | +/// |
| 16 | +extension GMUKMLParser1 { |
| 17 | + |
| 18 | + // MARK: - `didStartElement` |
| 19 | + /// Called when the parser encounters the start of an XML element. |
| 20 | + /// |
| 21 | + /// - Parameters: |
| 22 | + /// - parser: The XML parser object providing context for the parse operation. |
| 23 | + /// - elementName: The name of the element that was found in the XML. |
| 24 | + /// - namespaceURI: The URI of the namespace to which the element belongs (if any). |
| 25 | + /// - qName: The qualified name of the element (optional). |
| 26 | + /// - attributeDict: A dictionary containing the attributes of the element (if any). |
| 27 | + func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { |
| 28 | + characters = nil |
| 29 | + if let styleRegex, let _ = styleRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) { |
| 30 | + parseBeginStyle(with: elementName, styleID: attributeDict[GMUKMLParserConstants.idAttributeName]) |
| 31 | + } else if elementName == GMUKMLParserConstants.placemarkElementName || elementName == GMUKMLParserConstants.groundOverlayElementName { |
| 32 | + parseBeginPlacemark() |
| 33 | + } else if elementName == GMUKMLParserConstants.hotspotElementName { |
| 34 | + parseBeginHotspot(with: attributeDict) |
| 35 | + } else if let geometryRegex, |
| 36 | + geometryRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil || |
| 37 | + elementName == GMUKMLParserConstants.groundOverlayElementName { |
| 38 | + parseBeginGeometry(withElementName: elementName) |
| 39 | + } else if let boundaryRegex, |
| 40 | + boundaryRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 41 | + parseBeginBoundary(with: elementName) |
| 42 | + } else if let styleAttributeRegex, |
| 43 | + let geometryAttributeRegex, |
| 44 | + let compassRegex, |
| 45 | + let pairAttributeRegex, |
| 46 | + styleAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil || |
| 47 | + geometryAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil || |
| 48 | + compassRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil || |
| 49 | + pairAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 50 | + parseBeginLeafNode() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - `didEndElement` |
| 55 | + /// Called when the parser encounters the end of an XML element. |
| 56 | + /// |
| 57 | + /// - Parameters: |
| 58 | + /// - parser: The XML parser object providing context for the parse operation. |
| 59 | + /// - elementName: The name of the element that has just ended in the XML. |
| 60 | + /// - namespaceURI: The URI of the namespace to which the element belongs (if any). |
| 61 | + /// - qName: The qualified name of the element (optional). |
| 62 | + func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { |
| 63 | + if let styleRegex, |
| 64 | + styleRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 65 | + parseEndStyle() |
| 66 | + } else if let styleAttributeRegex, |
| 67 | + styleAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 68 | + parseEndStyleAttribute(elementName) |
| 69 | + } else if elementName == GMUKMLParserConstants.placemarkElementName { |
| 70 | + parseEndPlacemark() |
| 71 | + } else if let pairAttributeRegex, |
| 72 | + pairAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 73 | + parseEndPairAttribute(elementName) |
| 74 | + } else if elementName == GMUKMLParserConstants.groundOverlayElementName { |
| 75 | + parseEndGroundOverlay() |
| 76 | + } else if let geometryRegex, |
| 77 | + geometryRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 78 | + parseEndGeometry(withElementName: elementName) |
| 79 | + } else if let geometryAttributeRegex, |
| 80 | + geometryAttributeRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 81 | + parseEndGeometryAttribute(elementName) |
| 82 | + } else if let compassRegex, |
| 83 | + compassRegex.firstMatch(in: elementName, options: [], range: NSRange(location: 0, length: elementName.count)) != nil { |
| 84 | + parseEndCompassAttribute(elementName) |
| 85 | + } |
| 86 | + parserState.remove(.leafNode) |
| 87 | + characters = nil |
| 88 | + } |
| 89 | + |
| 90 | + // MARK: - `foundCharacters` |
| 91 | + /// Called when the parser finds character data within an XML element. |
| 92 | + /// |
| 93 | + /// - Parameters: |
| 94 | + /// - parser: The XML parser object providing context for the parse operation. |
| 95 | + /// - string: The string containing the characters found between XML elements. |
| 96 | + func parser(_ parser: XMLParser, foundCharacters string: String) { |
| 97 | + if isParsing(.leafNode) { |
| 98 | + guard var characters, !characters.isEmpty else { |
| 99 | + characters = string |
| 100 | + return |
| 101 | + } |
| 102 | + characters.append(string) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments