/plugins/XML/tags/release-2-8-0/xml/translate/EntityResolverWrapper.java

# · Java · 144 lines · 88 code · 11 blank · 45 comment · 21 complexity · 2a447e1e551db9481636600263f99101 MD5 · raw file

  1. package xml.translate;
  2. /*
  3. extracted from http://jing-trang.googlecode.com/svn/tags/V20091111/mod/resolver/src/main/com/thaiopensource/resolver/xml/sax/SAX.java
  4. all changes are marked with ELL comments
  5. Copyright (c) 2001-2003 Thai Open Source Software Center Ltd
  6. All rights reserved.
  7. SAX was provided under these conditions :
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are
  10. met:
  11. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in
  15. the documentation and/or other materials provided with the
  16. distribution.
  17. Neither the name of the Thai Open Source Software Center Ltd nor
  18. the names of its contributors may be used to endorse or promote
  19. products derived from this software without specific prior written
  20. permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  25. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. __________ END CONDITIONS ___________
  33. modifications Copyright (c) 2010 Eric Le Lay
  34. under the same license terms
  35. */
  36. import com.thaiopensource.resolver.ResolverException;
  37. import com.thaiopensource.resolver.AbstractResolver;
  38. import com.thaiopensource.resolver.BasicResolver;
  39. import com.thaiopensource.resolver.Input;
  40. import com.thaiopensource.resolver.Identifier;
  41. import com.thaiopensource.resolver.xml.ExternalIdentifier;
  42. import com.thaiopensource.resolver.xml.ExternalEntityIdentifier;
  43. import com.thaiopensource.resolver.xml.ExternalDTDSubsetIdentifier;
  44. import com.thaiopensource.resolver.xml.sax.SAX;
  45. import org.xml.sax.EntityResolver;
  46. import org.xml.sax.InputSource;
  47. import org.xml.sax.SAXException;
  48. import org.xml.sax.ext.EntityResolver2;
  49. import java.io.IOException;
  50. public final class EntityResolverWrapper extends AbstractResolver {
  51. private final EntityResolver entityResolver;
  52. private final EntityResolver2 entityResolver2;
  53. private final boolean promiscuous;
  54. public EntityResolverWrapper(EntityResolver entityResolver, boolean promiscuous) {
  55. this.entityResolver = entityResolver;
  56. if (entityResolver instanceof EntityResolver2)
  57. entityResolver2 = (EntityResolver2)entityResolver;
  58. else
  59. entityResolver2 = null;
  60. this.promiscuous = promiscuous;
  61. }
  62. public void resolve(Identifier id, Input input) throws IOException, ResolverException {
  63. if (input.isResolved())
  64. return;
  65. String publicId;
  66. String entityName = null;
  67. if (id instanceof ExternalIdentifier) {
  68. publicId = ((ExternalIdentifier)id).getPublicId();
  69. if (id instanceof ExternalEntityIdentifier)
  70. entityName = ((ExternalEntityIdentifier)id).getEntityName();
  71. else if (id instanceof ExternalDTDSubsetIdentifier)
  72. entityName = "[dtd]";
  73. }
  74. else {
  75. if (!promiscuous)
  76. return;
  77. publicId = null;
  78. }
  79. try {
  80. InputSource inputSource;
  81. if (entityName != null && entityResolver2 != null)
  82. inputSource = entityResolver2.resolveEntity(entityName,
  83. publicId,
  84. id.getBase(),
  85. id.getUriReference());
  86. else
  87. inputSource = entityResolver.resolveEntity(publicId, getSystemId(id));
  88. if (inputSource != null)
  89. SAX.setInput(input, inputSource);
  90. }
  91. catch (SAXException e) {
  92. throw SAX.toResolverException(e);
  93. }
  94. }
  95. static String getSystemId(Identifier id) {
  96. try {
  97. return BasicResolver.resolveUri(id);
  98. }
  99. catch (ResolverException e) { }
  100. return id.getUriReference();
  101. }
  102. // ELL: adding an implementation of open()
  103. public void open(Input input) throws IOException, ResolverException {
  104. if(input.isUriDefinitive() && !input.isOpen()){
  105. InputSource inputSource;
  106. try {
  107. if (entityResolver2 != null) {
  108. inputSource = entityResolver2.resolveEntity(null, //entity name
  109. null, // public Id
  110. null, // base uri
  111. input.getUri());
  112. } else {
  113. inputSource = entityResolver.resolveEntity(null, // public Id
  114. input.getUri());
  115. }
  116. }
  117. catch (SAXException e) {
  118. throw SAX.toResolverException(e);
  119. }
  120. if (inputSource != null) SAX.setInput(input, inputSource);
  121. }
  122. }
  123. // ELL: end changes
  124. }