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.8KB

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