You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractObjectReader.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding.tools;
  18. //Java
  19. import java.io.IOException;
  20. import java.util.Map;
  21. //SAX
  22. import org.xml.sax.SAXException;
  23. import org.xml.sax.InputSource;
  24. import org.xml.sax.XMLReader;
  25. import org.xml.sax.ContentHandler;
  26. import org.xml.sax.DTDHandler;
  27. import org.xml.sax.ErrorHandler;
  28. import org.xml.sax.EntityResolver;
  29. /**
  30. * This class can be used as base class for XMLReaders that generate SAX
  31. * events from Java objects.
  32. */
  33. public abstract class AbstractObjectReader implements XMLReader {
  34. private static final String NAMESPACES =
  35. "http://xml.org/sax/features/namespaces";
  36. private static final String NS_PREFIXES =
  37. "http://xml.org/sax/features/namespace-prefixes";
  38. private Map features = new java.util.HashMap();
  39. private ContentHandler orgHandler;
  40. /** Proxy for easy SAX event generation */
  41. protected EasyGenerationContentHandlerProxy handler;
  42. /** Error handler */
  43. protected ErrorHandler errorHandler;
  44. /**
  45. * Constructor for the AbstractObjectReader object
  46. */
  47. public AbstractObjectReader() {
  48. setFeature(NAMESPACES, false);
  49. setFeature(NS_PREFIXES, false);
  50. }
  51. /* ============ XMLReader interface ============ */
  52. /**
  53. * @see org.xml.sax.XMLReader#getContentHandler()
  54. */
  55. public ContentHandler getContentHandler() {
  56. return this.orgHandler;
  57. }
  58. /**
  59. * @see org.xml.sax.XMLReader#setContentHandler(ContentHandler)
  60. */
  61. public void setContentHandler(ContentHandler handler) {
  62. this.orgHandler = handler;
  63. this.handler = new EasyGenerationContentHandlerProxy(handler);
  64. }
  65. /**
  66. * @see org.xml.sax.XMLReader#getErrorHandler()
  67. */
  68. public ErrorHandler getErrorHandler() {
  69. return this.errorHandler;
  70. }
  71. /**
  72. * @see org.xml.sax.XMLReader#setErrorHandler(ErrorHandler)
  73. */
  74. public void setErrorHandler(ErrorHandler handler) {
  75. this.errorHandler = handler;
  76. }
  77. /**
  78. * @see org.xml.sax.XMLReader#getDTDHandler()
  79. */
  80. public DTDHandler getDTDHandler() {
  81. return null;
  82. }
  83. /**
  84. * @see org.xml.sax.XMLReader#setDTDHandler(DTDHandler)
  85. */
  86. public void setDTDHandler(DTDHandler handler) {
  87. }
  88. /**
  89. * @see org.xml.sax.XMLReader#getEntityResolver()
  90. */
  91. public EntityResolver getEntityResolver() {
  92. return null;
  93. }
  94. /**
  95. * @see org.xml.sax.XMLReader#setEntityResolver(EntityResolver)
  96. */
  97. public void setEntityResolver(EntityResolver resolver) {
  98. }
  99. /**
  100. * @see org.xml.sax.XMLReader#getProperty(String)
  101. */
  102. public Object getProperty(java.lang.String name) {
  103. return null;
  104. }
  105. /**
  106. * @see org.xml.sax.XMLReader#setProperty(String, Object)
  107. */
  108. public void setProperty(java.lang.String name, java.lang.Object value) {
  109. }
  110. /**
  111. * @see org.xml.sax.XMLReader#getFeature(String)
  112. */
  113. public boolean getFeature(java.lang.String name) {
  114. return ((Boolean) features.get(name)).booleanValue();
  115. }
  116. /**
  117. * Returns true if the NAMESPACES feature is enabled.
  118. * @return boolean true if enabled
  119. */
  120. protected boolean isNamespaces() {
  121. return getFeature(NAMESPACES);
  122. }
  123. /**
  124. * Returns true if the MS_PREFIXES feature is enabled.
  125. * @return boolean true if enabled
  126. */
  127. protected boolean isNamespacePrefixes() {
  128. return getFeature(NS_PREFIXES);
  129. }
  130. /**
  131. * @see org.xml.sax.XMLReader#setFeature(String, boolean)
  132. */
  133. public void setFeature(java.lang.String name, boolean value) {
  134. this.features.put(name, new Boolean(value));
  135. }
  136. /**
  137. * @see org.xml.sax.XMLReader#parse(String)
  138. */
  139. public void parse(String systemId) throws IOException, SAXException {
  140. throw new SAXException(
  141. this.getClass().getName()
  142. + " cannot be used with system identifiers (URIs)");
  143. }
  144. /**
  145. * @see org.xml.sax.XMLReader#parse(InputSource)
  146. */
  147. public abstract void parse(InputSource input)
  148. throws IOException, SAXException;
  149. }