PageRenderTime 75ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/translate/BufferDtdInputFormat.java

#
Java | 211 lines | 154 code | 14 blank | 43 comment | 8 complexity | fda6fa15e336d74d894c061b32eb06c7 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. Copyright (c) 2001-2003 Thai Open Source Software Center Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. Neither the name of the Thai Open Source Software Center Ltd nor
  14. the names of its contributors may be used to endorse or promote
  15. products derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. _________ END NOTICE ___________
  29. modifications Copyright (c) 2010 Eric Le Lay, under the same licence terms
  30. */
  31. package xml.translate;
  32. import com.thaiopensource.relaxng.input.dtd.DtdInputFormat;
  33. import com.thaiopensource.relaxng.input.dtd.Converter;
  34. import com.thaiopensource.relaxng.input.dtd.ResolverEntityManager;
  35. import com.thaiopensource.relaxng.edit.SchemaCollection;
  36. import com.thaiopensource.relaxng.input.InputFormat;
  37. import com.thaiopensource.relaxng.output.common.ErrorReporter;
  38. import com.thaiopensource.relaxng.translate.util.AbsoluteUriParam;
  39. import com.thaiopensource.relaxng.translate.util.AbstractParam;
  40. import com.thaiopensource.relaxng.translate.util.InvalidParamValueException;
  41. import com.thaiopensource.relaxng.translate.util.InvalidParamsException;
  42. import com.thaiopensource.relaxng.translate.util.NCNameParam;
  43. import com.thaiopensource.relaxng.translate.util.NmtokenParam;
  44. import com.thaiopensource.relaxng.translate.util.Param;
  45. import com.thaiopensource.relaxng.translate.util.ParamFactory;
  46. import com.thaiopensource.relaxng.translate.util.ParamProcessor;
  47. import com.thaiopensource.resolver.Resolver;
  48. import com.thaiopensource.resolver.Input;
  49. import com.thaiopensource.resolver.Identifier;
  50. import com.thaiopensource.resolver.ResolverException;
  51. import com.thaiopensource.util.Localizer;
  52. import com.thaiopensource.xml.dtd.om.Dtd;
  53. import com.thaiopensource.xml.dtd.parse.DtdParserImpl;
  54. import com.thaiopensource.xml.dtd.parse.ParseException;
  55. import com.thaiopensource.xml.em.UriEntityManager;
  56. import com.thaiopensource.xml.em.OpenEntity;
  57. import com.thaiopensource.xml.util.Naming;
  58. import org.xml.sax.ErrorHandler;
  59. import org.xml.sax.SAXException;
  60. import org.xml.sax.SAXParseException;
  61. import java.io.IOException;
  62. import java.io.Reader;
  63. import java.util.Map;
  64. /* copy of http://jing-trang.googlecode.com/svn/tags/V20091111/mod/convert-from-dtd/src/main/com/thaiopensource/relaxng/input/dtd/DtdInputFormat.java
  65. * It is necessary in order to use an EntityResolverWrapper to resolve the input if it's an open buffer
  66. */
  67. public class BufferDtdInputFormat extends DtdInputFormat {
  68. static private class NamespaceDeclParamFactory implements ParamFactory {
  69. private final Map<String, String> prefixMap;
  70. NamespaceDeclParamFactory(Map<String, String> prefixMap) {
  71. this.prefixMap = prefixMap;
  72. }
  73. public Param createParam(String name) {
  74. if (!name.startsWith("xmlns:"))
  75. return null;
  76. final String prefix = name.substring(6);
  77. if (!Naming.isNcname(prefix))
  78. return null;
  79. return new AbsoluteUriParam() {
  80. public void setAbsoluteUri(String uri) {
  81. prefixMap.put(prefix, uri);
  82. }
  83. };
  84. }
  85. }
  86. static private abstract class DeclPatternParam extends AbstractParam {
  87. private final Localizer localizer;
  88. DeclPatternParam(Localizer localizer) {
  89. this.localizer = localizer;
  90. }
  91. public void set(String value) throws InvalidParamValueException {
  92. if (value.indexOf('%') < 0)
  93. throw new InvalidParamValueException(localizer.message("no_percent"));
  94. if (value.lastIndexOf('%') != value.indexOf('%'))
  95. throw new InvalidParamValueException(localizer.message("multiple_percent"));
  96. if (!Naming.isNcname(value.replace('%', 'x')))
  97. throw new InvalidParamValueException(localizer.message("not_ncname_with_percent"));
  98. setDeclPattern(value);
  99. }
  100. abstract void setDeclPattern(String pattern);
  101. }
  102. public SchemaCollection load(String uri, String[] params, String outputFormat, ErrorHandler eh, Resolver resolver)
  103. throws InvalidParamsException, IOException, SAXException {
  104. final ErrorReporter er = new ErrorReporter(eh, DtdInputFormat.class);
  105. final BufferDtdConverter.Options options = new BufferDtdConverter.Options();
  106. if ("xsd".equals(outputFormat)) {
  107. options.inlineAttlistDecls = true;
  108. options.generateStart = false;
  109. }
  110. ParamProcessor pp = new ParamProcessor();
  111. pp.declare("inline-attlist",
  112. new AbstractParam() {
  113. public void set(boolean value) {
  114. options.inlineAttlistDecls = value;
  115. }
  116. });
  117. pp.declare("xmlns",
  118. new AbsoluteUriParam() {
  119. public void set(String value) throws InvalidParamValueException {
  120. if (value.equals(""))
  121. setAbsoluteUri(value);
  122. else
  123. super.set(value);
  124. }
  125. protected void setAbsoluteUri(String value) {
  126. options.defaultNamespace = value;
  127. }
  128. });
  129. pp.declare("any-name",
  130. new NCNameParam() {
  131. protected void setNCName(String value) {
  132. options.anyName = value;
  133. }
  134. });
  135. pp.declare("strict-any",
  136. new AbstractParam() {
  137. public void set(boolean value) {
  138. options.strictAny = value;
  139. }
  140. });
  141. pp.declare("annotation-prefix",
  142. new NCNameParam() {
  143. protected void setNCName(String value) {
  144. options.annotationPrefix = value;
  145. }
  146. });
  147. pp.declare("colon-replacement",
  148. new NmtokenParam() {
  149. protected void setNmtoken(String value) {
  150. options.colonReplacement = value;
  151. }
  152. });
  153. pp.declare("generate-start",
  154. new AbstractParam() {
  155. public void set(boolean value) {
  156. options.generateStart = value;
  157. }
  158. });
  159. pp.declare("element-define",
  160. new DeclPatternParam(er.getLocalizer()) {
  161. void setDeclPattern(String pattern) {
  162. options.elementDeclPattern = pattern;
  163. }
  164. });
  165. pp.declare("attlist-define",
  166. new DeclPatternParam(er.getLocalizer()) {
  167. void setDeclPattern(String pattern) {
  168. options.attlistDeclPattern = pattern;
  169. }
  170. });
  171. pp.setParamFactory(new NamespaceDeclParamFactory(options.prefixMap));
  172. pp.process(params, eh);
  173. try {
  174. // ELL: replaced UriEntityManager with ResolverEntityManager and called em.open()
  175. ResolverEntityManager em = new ResolverEntityManager(resolver);
  176. Dtd dtd = new DtdParserImpl().parse(em.open(uri), em);
  177. // end ELL
  178. try {
  179. return new BufferDtdConverter(dtd, er, options).convert();
  180. }
  181. catch (ErrorReporter.WrappedSAXException e) {
  182. throw e.getException();
  183. }
  184. }
  185. catch (ParseException e) {
  186. throw new SAXParseException(e.getMessageBody(), null, e.getLocation(), e.getLineNumber(), e.getColumnNumber());
  187. }
  188. }
  189. }