Source Code Cross Referenced for Node.java in  » Web-Server » Rimfaxe-Web-Server » com » rimfaxe » xml » xmlreader » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Rimfaxe Web Server » com.rimfaxe.xml.xmlreader 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.rimfaxe.xml.xmlreader;
002:
003:        import java.io.*;
004:        import java.util.*;
005:
006:        import com.rimfaxe.xml.xmlreader.xpath.*;
007:
008:        /**
009:         * An XML node.
010:
011:         <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
012:         This file is part of Sparta, an XML Parser, DOM, and XPath library.
013:         This library is free software; you can redistribute it and/or
014:         modify it under the terms of the GNU Lesser General Public License
015:         as published by the Free Software Foundation; either version 2.1 of
016:         the License, or (at your option) any later version.  This library
017:         is distributed in the hope that it will be useful, but WITHOUT ANY
018:         WARRANTY; without even the implied warranty of MERCHANTABILITY or
019:         FITNESS FOR A PARTICULAR PURPOSE.</small></blockquote>
020:         @see <a "href="doc-files/LGPL.txt">GNU Lesser General Public License</a>
021:         @version  $Date: 2003/01/09 00:59:33 $  $Revision: 1.5 $
022:         @author Eamonn O'Brien-Strain
023:         */
024:
025:        public abstract class Node implements  Cloneable {
026:
027:            void notifyObservers() {
028:                if (doc_ != null)
029:                    doc_.notifyObservers();
030:            }
031:
032:            void setOwnerDocument(Document doc) {
033:                doc_ = doc;
034:            }
035:
036:            /** The document that contains this node.  null IFF this is a Document */
037:            public Document getOwnerDocument() {
038:                return doc_;
039:            }
040:
041:            /** The element that contains this node or null if this is a Document
042:             * or the root element of a document. */
043:            public Element getParentNode() {
044:                return parentNode_;
045:            }
046:
047:            /** Return the previous node in the parent's list of children, or null if no such node. */
048:            public Node getPreviousSibling() {
049:                return previousSibling_;
050:            }
051:
052:            /** Return the next node in the parent's list of children, or null if no such node. */
053:            public Node getNextSibling() {
054:                return nextSibling_;
055:            }
056:
057:            /** @see #setAnnotation */
058:            public Object getAnnotation() {
059:                return annotation_;
060:            }
061:
062:            /** Use by client to attach arbitrary data to DOM document.
063:             *  Does not update indices and other observers.
064:             *  */
065:            public void setAnnotation(Object annotation) {
066:                annotation_ = annotation;
067:            }
068:
069:            void setParentNode(Element parentNode) {
070:                parentNode_ = parentNode;
071:            }
072:
073:            void insertAtEndOfLinkedList(Node lastChild) {
074:                previousSibling_ = lastChild;
075:                if (lastChild != null)
076:                    lastChild.nextSibling_ = this ;
077:            }
078:
079:            void removeFromLinkedList() {
080:                if (previousSibling_ != null)
081:                    previousSibling_.nextSibling_ = nextSibling_;
082:                if (nextSibling_ != null)
083:                    nextSibling_.previousSibling_ = previousSibling_;
084:                nextSibling_ = null;
085:                previousSibling_ = null;
086:            }
087:
088:            void replaceInLinkedList(Node replacement) {
089:                if (previousSibling_ != null)
090:                    previousSibling_.nextSibling_ = replacement;
091:                if (nextSibling_ != null)
092:                    nextSibling_.previousSibling_ = replacement;
093:                replacement.nextSibling_ = nextSibling_;
094:                replacement.previousSibling_ = previousSibling_;
095:                nextSibling_ = null;
096:                previousSibling_ = null;
097:            }
098:
099:            /** XML representation of this node. */
100:            public String toXml() throws IOException {
101:                StringWriter writer = new StringWriter();
102:                toXml(writer);
103:                return writer.toString();
104:            }
105:
106:            /** For an xpath expression of the form "xpathPrefix/@attrName" set the
107:             *  attribute "attrName" to attrValue on all elements that match
108:             *  "XpathPrefix" which is an arbitrary xpath expression matching elements,
109:             *  or for an xpath expression of the form "xpathPrefixe/text()" set the
110:             *  text of all matching text nodes.
111:             *
112:             (The following doc is used in the examples below.)<pre>
113:            
114:             <a>
115:             <b x="ppp"/>
116:             <b y="qqq"/>
117:             <b/>
118:             </a>
119:            
120:             </pre>
121:             * <ul>
122:             * <li>node.xpathSetStrings( "xpathPrefix/@attrName", value )
123:             * is equivalent to<pre>
124:             foreach element in node.xpathSelectElement(xpathPrefix):
125:             element.setAttribute( "attrName", value );
126:             </pre>
127:             <li> doc.xpathSetStrings( "/a/b/@x", "rrr" )
128:             will result in<pre>
129:            
130:             <a>
131:             <b x="rrr"/>
132:             <b x="rrr" y="qqq"/>
133:             <b x="rrr"/>
134:             </a>
135:            
136:             </pre>
137:             (Every matching child gets its attribute set.)
138:            
139:             <li> To set only the first child you would have to do
140:             doc.xpathSetStrings( "/a/b[@x]/@x", "rrr" )
141:             which would result in:<pre>
142:            
143:             <a>
144:             <b x="rrr"/>
145:             <b y="qqq"/>
146:             <b/>
147:             </a>
148:            
149:             </pre>
150:            
151:             <li> Not matching calls silently do nothing.
152:            
153:             <li> doc.xpathSetStrings("/a/b/text()", "TTT" ) will result in:
154:             <pre>
155:            
156:             <a>
157:             <b x="ppp">TTT</b>
158:             <b y="qqq">TTT</b>
159:             <b>TTT</b>
160:             </a>
161:            
162:             </pre>
163:            
164:             <li> doc.xpathSetStrings("/a/text()", "TTT" ) will result in:
165:             <pre>
166:            
167:             <a>TTT<b x="ppp"/><b y="qqq"/><b/></a>
168:            
169:             </pre>
170:             </ul>
171:             */
172:            public void xpathSetStrings(String xpath, String value)
173:                    throws ParseException
174:            //throws XPathException, IOException
175:            {
176:                //This is currently implemented using string manipulation.  It would
177:                // be better to work on the XPath objects.
178:                try {
179:                    int slash = xpath.lastIndexOf("/");
180:                    if (!xpath.substring(slash + 1).equals("text()")
181:                            && xpath.charAt(slash + 1) != '@')
182:                        throw new ParseException(
183:                                "Last step of Xpath expression \""
184:                                        + xpath
185:                                        + "\" is not \"text()\" and does not start with a '@'. It starts with a '"
186:                                        + xpath.charAt(slash + 1) + "'");
187:                    String elemXPath = xpath.substring(0, slash);
188:                    if (xpath.charAt(slash + 1) == '@') {
189:                        String attrName = xpath.substring(slash + 2);
190:                        if (attrName.length() == 0)
191:                            throw new ParseException(
192:                                    "Xpath expression \""
193:                                            + xpath
194:                                            + "\" specifies zero-length attribute name\"");
195:                        for (Enumeration i = xpathSelectElements(elemXPath); i
196:                                .hasMoreElements();) {
197:                            Element element = (Element) i.nextElement();
198:                            element.setAttribute(attrName, value);
199:                        }
200:                    } else {
201:                        for (Enumeration i = xpathSelectElements(elemXPath); i
202:                                .hasMoreElements();) {
203:                            Element parentOfText = (Element) i.nextElement();
204:
205:                            //Create a set of text nodes.  Need to do this
206:                            //because do not want to delete from what we are
207:                            //iterating over.
208:
209:                            // change LinkedList to Vector to make the code work
210:                            // with PersonalJava.
211:                            // List textNodes = new LinkedList();
212:                            Vector textNodes = new Vector();
213:                            for (Node j = parentOfText.getFirstChild(); j != null; j = j
214:                                    .getNextSibling())
215:                                if (j instanceof  Text) {
216:                                    // textNodes.add((Text) j);
217:                                    textNodes.addElement((Text) j);
218:                                }
219:
220:                            if (textNodes.size() == 0) {
221:
222:                                //If no existing text node add one
223:                                Text text = new Text(value);
224:                                parentOfText.appendChild(text);
225:
226:                            } else {
227:
228:                                //Set value of first text node
229:                                // Text first = (Text) textNodes.remove(0);
230:                                Text first = (Text) textNodes.elementAt(0);
231:                                textNodes.removeElementAt(0);
232:
233:                                first.setData(value);
234:
235:                                //Remove all subsequent text nodes
236:                                // for (Iterator j = textNodes.iterator(); j.hasNext();) {
237:                                //     Text text = (Text) j.next();
238:                                for (int j = 0; j < textNodes.size(); j++) {
239:                                    Text text = (Text) textNodes.elementAt(j);
240:
241:                                    parentOfText.removeChild(text);
242:                                }
243:
244:                            }
245:
246:                        }
247:                    }
248:
249:                } catch (DOMException e) {
250:                    throw new Error("Assertion failed " + e);
251:                } catch (IndexOutOfBoundsException e) {
252:                    throw new ParseException(
253:                            "Xpath expression \""
254:                                    + xpath
255:                                    + "\" is not in the form \"xpathExpression/@attributeName\"");
256:                }
257:            }
258:
259:            Element makeMatching(final Element parent, Step step,
260:                    final String msgContext) throws ParseException,
261:                    XPathException {
262:                NodeTest nodeTest = step.getNodeTest();
263:                if (!(nodeTest instanceof  ElementTest))
264:                    throw new ParseException("\"" + nodeTest + "\" in \""
265:                            + msgContext + "\" is not an element test");
266:                ElementTest elemTest = (ElementTest) nodeTest;
267:                final String tagName = elemTest.getTagName();
268:
269:                final Element newChild = new Element(tagName);
270:
271:                BooleanExpr predicate = step.getPredicate();
272:
273:                predicate.accept(new BooleanExprVisitor() {
274:                    public void visit(TrueExpr a) {
275:                        //do nothing
276:                    }
277:
278:                    public void visit(AttrExistsExpr a) throws XPathException {
279:                        newChild.setAttribute(a.getAttrName(), "something");
280:                    }
281:
282:                    public void visit(AttrEqualsExpr a) throws XPathException {
283:                        newChild
284:                                .setAttribute(a.getAttrName(), a.getAttrValue());
285:                    }
286:
287:                    public void visit(AttrNotEqualsExpr a)
288:                            throws XPathException {
289:                        newChild.setAttribute(a.getAttrName(), "not "
290:                                + a.getAttrValue());
291:                    }
292:
293:                    public void visit(AttrLessExpr a) throws XPathException {
294:                        newChild.setAttribute(a.getAttrName(), Double
295:                                .toString(Double.MIN_VALUE));
296:                    }
297:
298:                    public void visit(AttrGreaterExpr a) throws XPathException {
299:                        newChild.setAttribute(a.getAttrName(), Double
300:                                .toString(Double.MAX_VALUE));
301:                    }
302:
303:                    public void visit(TextExistsExpr a) throws XPathException {
304:                        newChild.appendChild(new Text("something"));
305:                    }
306:
307:                    public void visit(TextEqualsExpr a) throws XPathException {
308:                        newChild.appendChild(new Text(a.getValue()));
309:                    }
310:
311:                    public void visit(TextNotEqualsExpr a)
312:                            throws XPathException {
313:                        newChild.appendChild(new Text("not " + a.getValue()));
314:                    }
315:
316:                    public void visit(PositionEqualsExpr a)
317:                            throws XPathException {
318:                        int posn = a.getPosition();
319:                        if (parent == null && posn != 1)
320:                            throw new XPathException(XPath.get(msgContext),
321:                                    "Position of root node must be 1");
322:                        int lastPosition = 1; //newChild is at position 1
323:                        while (lastPosition < posn) {
324:                            parent.appendChild(new Element(tagName));
325:                            ++lastPosition;
326:                        }
327:
328:                    }
329:                });
330:                return newChild;
331:            }
332:
333:            /** Select all the elements that match the relative XPath
334:                expression with respect to this node. */
335:            public abstract Enumeration xpathSelectElements(String xpath)
336:                    throws ParseException;
337:
338:            //throws XPathException, IOException;
339:
340:            /** Select all the strings that match the relative XPath
341:                expression with respect to this node. */
342:            public abstract Enumeration xpathSelectStrings(String xpath)
343:                    throws ParseException;
344:
345:            //throws XPathException, IOException;
346:
347:            /** Select the first element that matches the relative XPath
348:                expression with respect to this node, or null if
349:                there is no match.*/
350:            public abstract Element xpathSelectElement(String xpath)
351:                    throws ParseException;
352:
353:            /** Select the first element that matches the relative XPath
354:                expression with respect to this node, or null if
355:                there is no match. */
356:            public abstract String xpathSelectString(String xpath)
357:                    throws ParseException;
358:
359:            /** Return a deep copy of this node. */
360:            public Object clone() {
361:                try {
362:                    return super .clone();
363:                } catch (CloneNotSupportedException e) {
364:                    throw new Error("assertion failed " + e);
365:                }
366:            }
367:
368:            //public abstract Node cloneNode(Document doc) throws DOMException;
369:
370:            /** Hierarchically concatenated text nodes.*/
371:            public String toString() {
372:                try {
373:                    StringWriter writer = new StringWriter();
374:                    toString(writer);
375:                    return writer.toString();
376:                } catch (IOException e) {
377:                    return super .toString();
378:                }
379:            }
380:
381:            abstract void toString(Writer writer) throws IOException;
382:
383:            abstract void toXml(Writer writer) throws IOException;
384:
385:            /** Quote special XML characters '<', '>', '&', '"' if necessary,
386:             *  and write to character stream.  We write to a character stream
387:             *  rather than simply returning a stream to avoid creating
388:             *  unneccessary objects.*/
389:            static protected void htmlEncode(Writer writer, String string)
390:                    throws IOException {
391:                int n = string.length();
392:                int writeNext = 0;
393:                for (int i = 0; i < n; ++i) {
394:                    int ch = string.charAt(i);
395:                    String encoded;
396:                    if (ch >= 128)
397:                        encoded = "&#" + ch + ";";
398:                    else
399:                        switch (ch) {
400:                        case '<':
401:                            encoded = "&lt;";
402:                            break;
403:                        case '>':
404:                            encoded = "&gt;";
405:                            break;
406:                        case '&':
407:                            encoded = "&amp;";
408:                            break;
409:                        case '\"':
410:                            encoded = "&quot;";
411:                            break;
412:                        default:
413:                            encoded = null;
414:                            break;
415:                        }
416:                    if (encoded != null) {
417:                        writer.write(string, writeNext, i - writeNext);
418:                        writer.write(encoded);
419:                        writeNext = i + 1;
420:                    }
421:                }
422:                if (writeNext < n)
423:                    writer.write(string, writeNext, n - writeNext);
424:            }
425:
426:            /**
427:             * @label ownerDocument
428:             */
429:            private Document doc_ = null;
430:
431:            /**
432:             * @label parent
433:             */
434:            private Element parentNode_ = null;
435:
436:            /**
437:             * @label previousSibling
438:             */
439:            private Node previousSibling_ = null;
440:
441:            /**
442:             * @label nextSibling
443:             */
444:            private Node nextSibling_ = null;
445:            private Object annotation_ = null;
446:        }
447:
448:        // $Log: Node.java,v $
449:        // Revision 1.5  2003/01/09 00:59:33  yuhongx
450:        // Use JDK1.1 API to make code work with PersonalJava.
451:        //
452:        // Revision 1.4  2002/12/13 23:09:24  eobrain
453:        // Fix javadoc.
454:        //
455:        // Revision 1.3  2002/12/13 18:12:15  eobrain
456:        // Fix xpathEnsure to handle case when the XPath given specifies a root node tagname that conflicts with the existing root node.  Extend xpathEnsure to work with any type of predicate.  Replace hacky string manipulation code with code that works on the XPath parse tree.
457:        //
458:        // Revision 1.2  2002/11/06 02:57:59  eobrain
459:        // Organize imputs to removed unused imports.  Remove some unused local variables.
460:        //
461:        // Revision 1.1.1.1  2002/08/19 05:03:54  eobrain
462:        // import from HP Labs internal CVS
463:        //
464:        // Revision 1.15  2002/08/18 04:21:39  eob
465:        // Sparta no longer throws XPathException -- it throws ParseException
466:        // instead.
467:        //
468:        // Revision 1.14  2002/08/15 21:27:48  eob
469:        // Constructor no longer needs documenent.
470:        //
471:        // Revision 1.13  2002/08/15 05:08:54  eob
472:        // Notify observers.
473:        //
474:        // Revision 1.12  2002/07/25 21:10:15  sermarti
475:        // Adding files that mysteriously weren't added from Sparta before.
476:        //
477:        // Revision 1.11  2002/07/08 22:37:18  eob
478:        // Add xpathEnsure
479:        //
480:        // Revision 1.10  2002/06/21 00:27:30  eob
481:        // Allow setting of text()
482:        //
483:        // Revision 1.9  2002/06/14 19:37:34  eob
484:        // Make toString of Node do the same as in XSLT -- recursive
485:        // concatenation of all text in text nodes.
486:        //
487:        // Revision 1.8  2002/05/23 21:24:58  eob
488:        // Change htmlEncode so that it uses charArray write instead of single
489:        // character writes.  This optimization was done because performance
490:        // profiling showed that this method is heavily used.
491:        //
492:        // Revision 1.7  2002/05/11 00:14:04  eob
493:        // Add xpathSetAttributes
494:        //
495:        // Revision 1.6  2002/05/09 16:49:27  eob
496:        // Add support for replace.
497:        //
498:        // Revision 1.5  2002/03/28 01:23:18  jrowson
499:        // fixed bugs related to client side caching
500:        //
501:        // Revision 1.4  2002/03/26 23:11:42  eob
502:        // Encode characters >= 128 bits.
503:        //
504:        // Revision 1.3  2002/02/23 02:06:19  eob
505:        // Add clone method.  Tweak toXml API.
506:        //
507:        // Revision 1.2  2002/02/01 21:54:43  eob
508:        // Move toXml up to Node from Element.
509:        //
510:        // Revision 1.1  2002/01/05 07:32:46  eob
511:        // initial
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.