Source Code Cross Referenced for SAXalXMLProvider.java in  » Web-Framework » RSF » uk » org » ponder » saxalizer » 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 Framework » RSF » uk.org.ponder.saxalizer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on Oct 6, 2004
003:         */
004:        package uk.org.ponder.saxalizer;
005:
006:        import java.io.InputStream;
007:        import java.io.OutputStream;
008:        import java.io.StringReader;
009:
010:        import uk.org.ponder.saxalizer.mapping.MappableXMLProvider;
011:        import uk.org.ponder.saxalizer.mapping.SAXalizerMapperEntry;
012:        import uk.org.ponder.util.UniversalRuntimeException;
013:
014:        /**
015:         * @author Antranig Basman (antranig@caret.cam.ac.uk)
016:         *  
017:         */
018:        public class SAXalXMLProvider implements  MappableXMLProvider {
019:            private SAXalizerMappingContext mappingcontext;
020:            private EntityResolverStash entityresolverstash;
021:
022:            public void setEntityResolverStash(
023:                    EntityResolverStash entityresolverstash) {
024:                this .entityresolverstash = entityresolverstash;
025:            }
026:
027:            public SAXalizerMappingContext getMappingContext() {
028:                return mappingcontext;
029:            }
030:
031:            private ThreadLocal saxalizergetter = new ThreadLocal() {
032:                public Object initialValue() {
033:                    SAXalizerHelper togo = new SAXalizerHelper(mappingcontext);
034:                    togo.setEntityResolverStash(entityresolverstash);
035:                    return togo;
036:                }
037:            };
038:
039:            private DeSAXalizer getDeSAXalizer() {
040:                return new DeSAXalizer(mappingcontext);
041:            }
042:
043:            public SAXalXMLProvider(SAXalizerMappingContext mappingcontext) {
044:                this .mappingcontext = mappingcontext;
045:            }
046:
047:            public void writeXML(Object towrite, OutputStream os) {
048:                DeSAXalizer desaxalizer = getDeSAXalizer();
049:                String resourcename = mappingcontext.classnamemanager
050:                        .getClassName(towrite.getClass());
051:                if (resourcename == null) {
052:                    throw new UniversalRuntimeException(
053:                            "Object of unknown type " + towrite.getClass()
054:                                    + " supplied to writeXML");
055:                }
056:                try {
057:                    desaxalizer.serializeSubtree(towrite, resourcename, os);
058:                } catch (Throwable t) {
059:                    throw UniversalRuntimeException.accumulate(t,
060:                            "Error writing object of class "
061:                                    + towrite.getClass());
062:                }
063:            }
064:
065:            public Object readXML(Object classorobject, InputStream is) {
066:                SAXalizerHelper saxalizer = (SAXalizerHelper) saxalizergetter
067:                        .get();
068:                Class objclass = classorobject == null ? null
069:                        : classorobject instanceof  Class ? (Class) classorobject
070:                                : classorobject.getClass();
071:                try {
072:                    Object toread = classorobject == null ? null
073:                            : classorobject == objclass ? objclass
074:                                    .newInstance() : classorobject;
075:                    return saxalizer.produceSubtree(toread, is);
076:                } catch (Throwable t) {
077:                    // Xerces appears to be crap and will not clear its parsing condition once it has 
078:                    // been set - throw the whole helper away and start again.
079:                    saxalizergetter.set(new SAXalizerHelper(mappingcontext));
080:                    throw UniversalRuntimeException.accumulate(t,
081:                            "Error reading object of class " + objclass);
082:                }
083:            }
084:
085:            public void loadMapping(InputStream is) {
086:                SAXalizerMapperEntry entry = (SAXalizerMapperEntry) readXML(
087:                        SAXalizerMapperEntry.class, is);
088:                mappingcontext.mapper.addEntry(entry);
089:            }
090:
091:            public void registerClass(String classname, Class resourceclass) {
092:                mappingcontext.classnamemanager.registerClass(classname,
093:                        resourceclass);
094:            }
095:
096:            public String toString(Object towrite) {
097:                return toString(towrite, true);
098:            }
099:
100:            public String toString(Object towrite, boolean compactmode) {
101:                DeSAXalizer desaxalizer = getDeSAXalizer();
102:                String resourcename = mappingcontext.classnamemanager
103:                        .getClassName(towrite.getClass());
104:                if (resourcename == null) {
105:                    throw new UniversalRuntimeException(
106:                            "Object of unknown type " + towrite.getClass()
107:                                    + " supplied to writeXML");
108:                }
109:                try {
110:                    return desaxalizer.toString(towrite, resourcename,
111:                            compactmode);
112:                } catch (Throwable t) {
113:                    throw UniversalRuntimeException.accumulate(t,
114:                            "Error writing object of class "
115:                                    + towrite.getClass());
116:                }
117:            }
118:
119:            private Class findClass(String classname) {
120:                Class objclass = mappingcontext.classnamemanager
121:                        .findClazz(classname);
122:                if (objclass == null) {
123:                    throw new UniversalRuntimeException("Root tag " + classname
124:                            + " had no entry for object type");
125:                }
126:                return objclass;
127:            }
128:
129:            public Object fromString(String toread) {
130:                int roottagi = toread.indexOf('<');
131:                if (roottagi == -1) {
132:                    throw new UniversalRuntimeException(
133:                            "Couldn't find root tag in string " + toread);
134:                }
135:                int roottagt = roottagi + 1;
136:
137:                int endtag = toread.indexOf('>', roottagi);
138:                if (endtag == -1) {
139:                    throw new UniversalRuntimeException(
140:                            "Unterminated root tag in string " + toread);
141:                }
142:
143:                for (; roottagt < endtag; ++roottagt) {
144:                    char c = toread.charAt(roottagt);
145:                    // QQQQQ technically not correct, XML whitespace does not agree with Java.
146:                    if (Character.isWhitespace(c))
147:                        break;
148:                }
149:                String roottag = toread.substring(roottagi + 1, roottagt);
150:                Class objclass = findClass(roottag);
151:
152:                StringReader sr = new StringReader(toread);
153:                SAXalizerHelper saxalizer = (SAXalizerHelper) saxalizergetter
154:                        .get();
155:                try {
156:                    return saxalizer.produceSubtree(objclass.newInstance(), sr);
157:                } catch (Throwable t) {
158:                    throw UniversalRuntimeException.accumulate(t,
159:                            "Error reading object of class " + objclass);
160:                }
161:            }
162:
163:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.