My favorites | Sign in
Project Logo
                
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

/*
* (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* http://code.google.com/p/gagawa/
*
* AUTHORS:
* Mark Kolich
* Chris Friedrich
*
*/

class Node {

protected $tag_;
protected $attributes_;
protected $parent_;

/**
* This Node constructor can only be called from
* classes that extend Node.
*/
protected function __construct ( $tag = NULL ) {

if(GagawaUtil::gagawaIsEmpty($tag)){
throw new Exception( "Node's must have a tag " .
"type!" );
}

$this->parent_ = NULL;
$this->tag_ = $tag;
$this->attributes_ = array();

}

/**
* Returns the parent node of this node, if
* a parent exists. If no parent exists,
* this function returns NULL.
*/
public function getParent ( ) {
return $this->parent_;
}

/**
* Sets the parent of this Node. Note that this
* function is protected and can only be called by
* classes that extend Node. The parent cannot
* be NULL; this function will throw an Exception
* if the parent node is empty.
*/
protected function setParent ( $parent = NULL ) {

if(GagawaUtil::gagawaIsEmpty($parent)){
throw new Exception( "Parent cannot be NULL!" );
}

$this->parent_ = $parent;

}

/**
* Given a name and value pair, sets an attribute on this Node.
* The name and value cannot be empty; if so, this function
* will throw an Exception. Note if the attribute already exists
* and the caller wants to set an attribute of the same name,
* this function will not create a new Attribute, but rather
* update the value of the existing named attribute.
*/
public function setAttribute ( $name = NULL, $value = NULL ) {

if(GagawaUtil::gagawaIsEmpty($name) ||
GagawaUtil::gagawaIsEmpty($value)){
throw new Exception("Attributes must have " .
"a name and a value!");

}

foreach ( $this->attributes_ as $attribute ) {
if($attribute->getName()===$name){
$attribute->setValue( $value );
return;
}
}

$this->attributes_[] = new Attribute( $name, $value );
return $this;

}

/**
* Fetch and attribute by name from this Node. The attribute
* name cannot be NULL; if so, this function will throw an
* Exception.
*/
public function getAttribute ( $name = NULL ) {

$returnAttr = NULL;

if(GagawaUtil::gagawaIsEmpty($name)){
throw new Exception("Attribute name cannot " .
"be empty!");
}

foreach ( $this->attributes_ as $attribute ) {
if($attribute->getName()===$name){
$returnAttr = $attribute->getValue();
break;
}
}

return $returnAttr;

}

/**
* Removes an attribute from this Node, by name. The name
* of the attribute to remove cannot be NULL; if so, this
* function will throw an Exception.
*/
public function removeAttribute ( $name = NULL ) {

if(GagawaUtil::gagawaIsEmpty($name)){
throw new Exception("Attribute name cannot " .
"be empty!");
}

for ( $i = 0; $i < count($this->attributes_); $i++ ) {
$attribute = $this->attributes_[$i];
if($attribute->getName()===$name){
unset( $this->attributes_[$i] );
return true;
}
}

// Couldn't find the attribute, so
// it wasn't removed.
return false;

}

public function write ( ) {
$buffer = $this->writeOpen();
$buffer .= $this->writeClose();
return $buffer;
}

protected function writeOpen ( ) {
$buffer = "<";
$buffer .= $this->tag_;
foreach ( $this->attributes_ as $attribute ) {
$buffer .= $attribute->write();
}
$buffer .= ">";
return $buffer;
}

protected function writeClose ( ) {
return "</" . $this->tag_ . ">";
}

} /* Node */

?>
Show details Hide details

Change log

r48 by html-gen...@hp.com on Jun 05, 2009   Diff
Fixed a bug that wouldn't let the user
create a FertileNode containing an "0" or
0 because PHP thought those were NULL
values (as checked by PHP's empty()
function).
Go to: 
Project members, sign in to write a code review

Older revisions

r37 by html-gen...@hp.com on Dec 26, 2008   Diff
Moving some code around in the Gagawa
PHP package.
All revisions of this file

File info

Size: 4683 bytes, 186 lines
Hosted by Google Code