Debian Codegraph @

XCode/XCWorkspaceParser.m

raw
1 /*
2 Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc.
3
4 Written by: Gregory John Casamento <[email protected]>
5 Date: 2022
6
7 This file is part of the GNUstep XCode Library
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110 USA.
23 */
24
25 #import "XCWorkspaceParser.h"
26 #import "XCWorkspace.h"
27 #import "XCFileRef.h"
28
29 #import <Foundation/NSString.h>
30 #import <Foundation/NSArray.h>
31 #import <Foundation/NSXMLParser.h>
32 #import <Foundation/NSDictionary.h>
33 #import <Foundation/NSData.h>
34
35 @implementation XCWorkspaceParser
36
37 - (instancetype) initWithContentsOfFile: (NSString *)file
38 {
39 ASSIGN(_filename, file);
40 if ((self = [super init]) != nil)
41 {
42 NSData *data = [NSData dataWithContentsOfFile: file];
43 if (data != nil)
44 {
45 NSError *error = nil;
46 NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data];
47
48 [parser setDelegate: self];
49 [parser parse];
50
51 error = [parser parserError];
52 if (error != nil)
53 {
54 NSLog(@"Error: %@", error);
55 return nil;
56 }
57
58 RELEASE(parser);
59 }
60 else
61 {
62 NSLog(@"Unable to read data");
63 }
64 }
65
66 return self;
67 }
68
69 + (instancetype) parseWorkspaceFile: (NSString *)file
70 {
71 return AUTORELEASE([[self alloc] initWithContentsOfFile: file]);
72 }
73
74 + (instancetype) parseWorkspaceDirectory: (NSString *)dir
75 {
76 NSString *datafile = [dir stringByAppendingPathComponent: @"contents.xcworkspacedata"];
77 return [self parseWorkspaceFile: datafile];
78 }
79
80 - (XCWorkspace *) workspace
81 {
82 [_workspace setFilename: _filename];
83 return _workspace;
84 }
85
86 - (void) dealloc
87 {
88 RELEASE(_filename);
89 RELEASE(_workspace);
90 [super dealloc];
91 }
92
93 /** Parser delegate **/
94
95 - (void) parserDidStartDocument: (NSXMLParser *)parser
96 {
97 // not needed for this type of file...
98 }
99
100 - (void) parser: (NSXMLParser *)parser
101 didStartElement: (NSString *)elementName
102 namespaceURI: (NSString *)namespaceURI
103 qualifiedName: (NSString *)qName
104 attributes: (NSDictionary *)attributeDict
105 {
106 if ([elementName isEqualToString: @"Workspace"])
107 {
108 NSString *v = [attributeDict objectForKey: @"version"];
109 ASSIGN(_workspace, [XCWorkspace workspace]);
110 [_workspace setVersion: v];
111 }
112 else if ([elementName isEqualToString: @"FileRef"])
113 {
114 XCFileRef *fr = [XCFileRef fileRef];
115 NSString *l = [attributeDict objectForKey: @"location"];
116 NSArray *a = [_workspace fileRefs];
117
118 [fr setLocation: l];
119 a = [a arrayByAddingObject: fr];
120 [_workspace setFileRefs: a];
121 }
122 }
123
124 -(void) parser: (NSXMLParser *)parser
125 foundCharacters: (NSString *)string
126 {
127 // not needed for this type of file...
128 }
129
130 - (void) parser: (NSXMLParser *)parser
131 didEndElement: (NSString *)elementName
132 namespaceURI: (NSString *)namespaceURI
133 qualifiedName: (NSString *)qName
134 {
135 }
136
137 - (void) parserDidEndDocument: (NSXMLParser *)parser
138 {
139 // not needed for this type of file...
140 }
141
142 @end