CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Vocabulary.php
Go to the documentation of this file.
1 <?PHP
2
3 #
4 # FILE: SPT--Vocabulary.php
5 #
6 # METHODS PROVIDED:
7 # Vocabulary()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR: Edward Almasy
13 #
14 # Part of the Collection Workflow Integration System
15 # Copyright 2007 Edward Almasy and Internet Scout
16 # http://scout.wisc.edu
17 #
18
22 class Vocabulary {
23
24 # ---- PUBLIC INTERFACE --------------------------------------------------
25
31 function Vocabulary($FileName)
32 {
33 # save file name
34 $this->FileName = $FileName;
35
36 # attempt to load vocabulary from file
37 $this->Xml = simplexml_load_file($FileName);
38
39 # set error code if load failed
40 $this->StatusString = ($this->Xml === FALSE) ? "XML Load Failed" : "OK";
41 $this->Xml = isset($this->Xml->vocabulary) ? $this->Xml->vocabulary : $this->Xml;
42 }
43
47 function Status() { return $this->StatusString; }
48
53 function Hash()
54 {
55 return self::HashForFile($this->FileName);
56 }
57
63 static function HashForFile($FileName = NULL)
64 {
65 return strtoupper(md5($FileName));
66 }
67
71 function Name() { return $this->XmlVal("name"); }
72
76 function Description() { return $this->XmlVal("description"); }
77
81 function Url() { return $this->XmlVal("url"); }
82
86 function Version() { return $this->XmlVal("version"); }
87
91 function HasQualifier()
92 {
93 return strlen($this->QualifierName())
94 && (strlen($this->QualifierNamespace())
95 || strlen($this->QualifierUrl()));
96 }
97
101 function QualifierName()
102 {
103 return isset($this->Xml->qualifier->name)
104 ? (string)$this->Xml->qualifier->name : "";
105 }
106
110 function QualifierNamespace()
111 {
112 return isset($this->Xml->qualifier->namespace)
113 ? (string)$this->Xml->qualifier->namespace : "";
114 }
115
119 function QualifierUrl()
120 {
121 return isset($this->Xml->qualifier->url)
122 ? (string)$this->Xml->qualifier->url : "";
123 }
124
129 function OwnerName()
130 {
131 return isset($this->Xml->owner->name)
132 ? (string)$this->Xml->owner->name : "";
133 }
134
139 function OwnerUrl()
140 {
141 return isset($this->Xml->owner->url)
142 ? (string)$this->Xml->owner->url : "";
143 }
144
148 function TermArray()
149 {
150 $Terms = $this->ExtractTermSet($this->Xml);
151
152 # return array of terms to caller
153 return $Terms;
154 }
155
159 function TermList()
160 {
161 $TermTree = $this->TermArray();
162 $Terms = $this->BuildTermList("", $TermTree);
163 return $Terms;
164 }
165
166 # ---- PRIVATE INTERFACE -------------------------------------------------
167
168 var $FileName;
169 var $StatusString;
170 var $Xml;
171
172 function XmlVal($ValueName)
173 {
174 return isset($this->Xml->{$ValueName})
175 ? (string)$this->Xml->{$ValueName} : "";
176 }
177
178 function ExtractTermSet($Tree)
179 {
180 $Terms = array();
181 foreach ($Tree->term as $Term)
182 {
183 if (isset($Term->value))
184 {
185 $Terms[(string)$Term->value] = $this->ExtractTermSet($Term);
186 }
187 else
188 {
189 $Terms[(string)$Term] = array();
190 }
191 }
192 return $Terms;
193 }
194
195 # build double-dash separated term list from hierarchical array
196 function BuildTermList($Prefix, $TermTree)
197 {
198 $Terms = array();
199 foreach ($TermTree as $Term => $Children)
200 {
201 $Term = trim($Term);
202 $NewTerm = strlen($Prefix) ? $Prefix." -- ".$Term : $Term;
203 $Terms[] = $NewTerm;
204 $Terms = array_merge($Terms, $this->BuildTermList($NewTerm, $Children));
205 }
206 return $Terms;
207 }
208 }
209
210
211 ?>