Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 package de.mfuchs.xmlstorage;
00040
00041 import java.util.Stack;
00042
00043
00044
00045
00046
00047
00048 public class XMLPos
00049 {
00050
00051
00052
00053
00054 public XMLPos(XMLNode root)
00055 {
00056 _root = root;
00057 _cur = root;
00058 }
00059
00060
00061
00062
00063
00064 public XMLPos(final XMLPos other)
00065 {
00066 _root = other._root;
00067 _cur = other._cur;
00068
00069 }
00070
00071
00072
00073
00074
00075
00076 public XMLPos(final XMLPos other, final String nodeName)
00077 {
00078 _root = other._root;
00079 _cur = other._cur;
00080
00081 smartCreate(nodeName);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 public XMLPos(final XMLPos other, final String nodeName, final String attrName, final String attrValue)
00094 {
00095 _root = other._root;
00096 _cur = other._cur;
00097
00098 smartCreate(nodeName, attrName, attrValue);
00099 }
00100
00101
00102
00103
00104
00105 public XMLNode cur()
00106 {
00107 return _cur;
00108 }
00109
00110
00111
00112
00113
00114
00115 public String get(final String attrName)
00116 {
00117 return _cur.get(attrName);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 public String get(final String attrName, final String def)
00127 {
00128 return _cur.get(attrName, def);
00129 }
00130
00131
00132
00133
00134
00135
00136 public void put(final String attrName, final String value)
00137 {
00138 _cur.put(attrName, value);
00139 }
00140
00141
00142
00143
00144
00145 public void addDown(XMLNode child)
00146 {
00147 _cur.addChild(child);
00148 goTo(child);
00149 }
00150
00151
00152
00153
00154
00155 public boolean back()
00156 {
00157 if (!_stack.empty()) {
00158 _cur = _stack.pop();
00159 return true;
00160 } else
00161 return false;
00162 }
00163
00164
00165
00166
00167
00168 public boolean goDown()
00169 {
00170 XMLNode node = _cur.getFirstChild();
00171
00172 if (node != null) {
00173 goTo(node);
00174 return true;
00175 } else
00176 return false;
00177 }
00178
00179
00180
00181
00182
00183
00184 public boolean goDown(final String name)
00185 {
00186 XMLNode node = _cur.find(name);
00187
00188 if (node != null) {
00189 goTo(node);
00190 return true;
00191 } else
00192 return false;
00193 }
00194
00195
00196
00197
00198
00199
00200 public boolean go(String path)
00201 {
00202 return go(new XPath(path));
00203 }
00204
00205
00206
00207
00208
00209
00210 public boolean go(XPath xpath)
00211 {
00212 XMLNode node = xpath._absolute? _root: _cur;
00213
00214 node = node.findRelative(xpath);
00215
00216 if (node != null) {
00217 goTo(node);
00218 return true;
00219 } else
00220 return false;
00221 }
00222
00223
00224
00225
00226
00227 public void create(final String name)
00228 {
00229 addDown(new XMLNode(name));
00230 }
00231
00232
00233
00234
00235
00236 public void smartCreate(final String name)
00237 {
00238 XMLNode node = _cur.find(name);
00239
00240 if (node != null)
00241 goTo(node);
00242 else
00243 addDown(new XMLNode(name));
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253 public void smartCreate(final String name, final String attrName, final String attrValue)
00254 {
00255 XMLNode node = _cur.find(name, attrName, attrValue);
00256
00257 if (node != null)
00258 goTo(node);
00259 else {
00260 node = new XMLNode(name);
00261 addDown(node);
00262 node.put(attrName, attrValue);
00263 }
00264 }
00265
00266
00267
00268
00269 public String getContent()
00270 {
00271 return _cur.getContent();
00272 }
00273
00274
00275
00276
00277 public void setContent(final String s)
00278 {
00279 setContent(s, false);
00280 }
00281
00282
00283
00284
00285
00286 public void setContent(final String s, boolean cdata)
00287 {
00288 _cur.setContent(s, cdata);
00289 }
00290
00291
00292
00293
00294 public String toString()
00295 {
00296 return _cur.toString();
00297 }
00298
00299 protected XMLNode _root;
00300 protected XMLNode _cur;
00301 protected Stack<XMLNode> _stack = new Stack<XMLNode>();
00302
00303
00304
00305
00306
00307 protected void goTo(XMLNode child)
00308 {
00309 _stack.push(_cur);
00310 _cur = child;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 public void putSubattrib(final String nodeName, final String attrName, final String value)
00320 {
00321 _cur.putSubattrib(nodeName, attrName, value);
00322 }
00323 }