-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSource.m
150 lines (121 loc) · 4.83 KB
/
Source.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
% Copyright (c) 2012 Howard Hughes Medical Institute.
% All rights reserved.
% Use is subject to Janelia Farm Research Campus Software Copyright 1.1 license terms.
% http://license.janelia.org/license/jfrc_copyright_1_1.html
classdef Source < handle
properties
name
identifier
parentSource
childSources
end
methods
function obj = Source(name, varargin)
if nargin == 2
parent = varargin{1};
% Check if the parent has a source with this name already.
if isempty(parent)
existingSource = [];
else
existingSource = parent.childWithName(name);
end
if isempty(existingSource)
% Keep this new source and link it to the parent.
obj.parentSource = parent;
obj.parentSource.childSources(end + 1) = obj;
else
% Use the existing source instead.
obj = existingSource;
return;
end
else
obj.parentSource = [];
end
obj.name = name;
obj.identifier = System.Guid.NewGuid();
obj.childSources = Source.empty(1, 0);
end
function path = path(obj)
if isempty(obj.parentSource)
path = obj.name;
else
path = [obj.parentSource.path() ':' obj.name];
end
end
function a = ancestors(obj)
if isempty(obj.parentSource)
a = [];
else
parentAncestors = obj.parentSource.ancestors();
a = [parentAncestors obj.parentSource];
end
end
function c = childWithName(obj, name)
c = [];
for i = 1:length(obj.childSources)
if strcmp(obj.childSources(i).name, name)
c = obj.childSources(i);
break;
end
end
end
function d = descendantAtPath(obj, path)
if isempty(path)
d = obj;
else
index = find(path == ':', 1, 'first');
if isempty(index)
childName = path;
childPath = '';
else
childName = path(1:index - 1);
childPath = path(index + 1:end);
end
d = obj.childWithName(childName);
if ~isempty(d)
d = d.descendantAtPath(childPath);
end
end
end
function persistToMetadata(obj, parentNode)
docNode = parentNode.getOwnerDocument();
% Persist all ancestor sources.
ancestors = obj.ancestors();
for i = 2:length(ancestors)
sourceNode = parentNode.appendChild(docNode.createElement('source'));
sourceNode.setAttribute('label', ancestors(i).name);
sourceNode.setAttribute('identifier', char(ancestors(i).identifier.ToString()));
parentNode = sourceNode;
end
% Persist this source.
sourceNode = parentNode.appendChild(docNode.createElement('source'));
sourceNode.setAttribute('label', obj.name);
sourceNode.setAttribute('identifier', char(obj.identifier.ToString()));
end
function syncWithMetadata(obj, sourceNode)
% Update the UUID of this source.
attr = sourceNode.getAttributes();
obj.identifier = System.Guid(char(attr.getNamedItem('identifier').getNodeValue()));
% Sync any child sources.
children = sourceNode.getChildNodes();
for i = 1:children.getLength()
childNode = children.item(i-1);
if childNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && ...
strcmp(char(childNode.getNodeName()), 'source')
attr = childNode.getAttributes();
childName = char(attr.getNamedItem('label').getNodeValue());
child = obj.childWithName(childName);
if isempty(child)
child = Source(label, obj);
end
child.syncWithMetadata(childNode);
end
end
end
function delete(obj)
for i = 1:length(obj.childSources)
delete(obj.childSources(i));
end
end
end
end