1 package xdoclet.sdk.xgg.binding;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.PrintStream;
8 import java.util.Collection;
9 import java.util.Locale;
10
11 import javax.xml.parsers.FactoryConfigurationError;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.parsers.SAXParserFactory;
14
15 import org.apache.commons.logging.LogFactory;
16 import org.xml.sax.InputSource;
17 import org.xml.sax.SAXException;
18
19 import com.sun.msv.grammar.Grammar;
20 import com.sun.msv.reader.dtd.DTDReader;
21 import com.sun.msv.reader.util.GrammarLoader;
22 import com.sun.msv.reader.util.IgnoreController;
23 import com.sun.msv.writer.relaxng.Driver;
24
25 import xdoclet.XDocletException;
26 import xdoclet.sdk.xgg.XGGPlugin;
27
28 /***
29 * This class represents a DTD or an XSD.
30 *
31 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a>
32 * @author <a href="mailto:christian at inx-soft.com">Christian Kvalheim</a>
33 * @created 7 mai 2003
34 * @version $Revision: 1.5 $
35 */
36 public class Binder {
37 private File _constraints;
38
39 /***
40 * @deprecated remove this when debugging is over.
41 */
42 private XGGPlugin _plugin;
43
44
45 /***Constructor for the Binder object */
46 public Binder() { }
47
48
49 /***
50 * Debug constructor
51 *
52 * @param plugin
53 * @deprecated Don't use this constructor. It's for debugging only.
54 */
55 public Binder(XGGPlugin plugin) {
56 this();
57 _plugin = plugin;
58 }
59
60
61 /***
62 * Returns a collection of Element objects, representing the DTD/XSD/RNG
63 * structure.
64 *
65 * @return a Collection of {@link Element}.
66 * @exception XDocletException Description of the Exception
67 */
68 public Collection getElements() throws XDocletException {
69 if (_constraints == null) {
70 throw new XDocletException("constraints has not been set.");
71 }
72 // debugging
73 if (_plugin != null) {
74 // writeRngFile(_constraints);
75 }
76
77 try {
78 InputSource inputSource = new InputSource(new FileReader(_constraints));
79 SAXParserFactory factory = SAXParserFactory.newInstance();
80 factory.setNamespaceAware(true);
81 Grammar grammar = null;
82
83 if (_constraints.getName().toLowerCase(Locale.US).endsWith(".dtd")) {
84 grammar = DTDReader.parse(
85 inputSource,
86 new IgnoreController());
87 } else if (_constraints.getName().toLowerCase(Locale.US).endsWith(".xsd")) {
88 grammar = GrammarLoader.loadSchema(
89 inputSource,
90 new IgnoreController(),
91 factory);
92 } else {
93 String message = "constraints must end with .dtd or .xsd but was: " + _constraints.getName();
94 LogFactory.getLog(Binder.class).error(message);
95 throw new XDocletException(message);
96 }
97
98 // Find the root element and let our visitor traverse from there.
99 XGGVisitor visitor = new XGGVisitor(grammar.getPool(), _constraints.getName());
100 grammar.getTopLevel().visit(visitor);
101 return visitor.getElements();
102 } catch (FactoryConfigurationError factoryConfigurationError) {
103 LogFactory.getLog(Binder.class).error(factoryConfigurationError.getMessage(), factoryConfigurationError);
104 throw new XDocletException(factoryConfigurationError);
105 } catch (SAXException e) {
106 LogFactory.getLog(Binder.class).error(e.getMessage(), e);
107 throw new XDocletException(e);
108 } catch (ParserConfigurationException e) {
109 LogFactory.getLog(Binder.class).error(e.getMessage(), e);
110 throw new XDocletException(e);
111 } catch (IOException e) {
112 LogFactory.getLog(Binder.class).error(e.getMessage(), e);
113 throw new XDocletException(e);
114 } catch (Throwable e) {
115 LogFactory.getLog(Binder.class).error(e.getMessage(), e);
116 throw new XDocletException(e);
117 }
118 }
119
120
121 /***
122 * Sets the DTD or XSD file to generate from.
123 *
124 * @param constraints the DTD or XSD file to generate from.
125 * @exception XDocletException Description of the Exception
126 */
127 public void setConstraints(File constraints) throws XDocletException {
128 if (!constraints.exists()) {
129 throw new XDocletException("File doesn't exist: " + constraints.getAbsolutePath());
130 }
131 _constraints = constraints;
132 }
133
134
135 /***
136 * Writes the RNG file that is used during conversion from DTD/XSD.
137 * This file is created by Sun's rngconv (taking a DTD or XSD ans input).
138 *
139 * @param constraints Description of the Parameter
140 * @exception XDocletException Description of the Exception
141 * @deprecated This is for debugging only and should be removed.
142 */
143 private void writeRngFile(File constraints) throws XDocletException {
144 String fileName = constraints.getName().substring(0, constraints.getName().lastIndexOf('.')) + ".rng";
145 File destinationDir = _plugin.getDestinationDir();
146 destinationDir.mkdirs();
147 File rngFile = new File(_plugin.getDestinationDir(), fileName);
148
149 PrintStream realOut = System.out;
150 try {
151 System.setOut(new PrintStream(new FileOutputStream(rngFile)));
152
153 String[] args = new String[]{constraints.getAbsolutePath()};
154 try {
155 LogFactory.getLog(Binder.class).info("Writing " + rngFile.getAbsolutePath());
156 Driver.main(args);
157 } catch (Exception e) {
158 LogFactory.getLog(Binder.class).error("Failed to run rngconv", e);
159 throw new IOException(e.getMessage());
160 } finally {
161 System.setOut(realOut);
162 }
163 System.out.println("Wrote debugging RNG for " + constraints.getAbsolutePath() + " to " + rngFile.getAbsolutePath());
164 } catch (Exception e) {
165 throw new XDocletException(e);
166 }
167 }
168 }
This page was automatically generated by Maven