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