Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 808 → Rev 809

/sun/xmleditor/trunk/src/parser/Parser.java
4,14 → 4,16
* Coded by: Group 5, software practice summer 2003
* University of Bielefeld, Germany
*
* @version $Revision: 1.58 $
* @version $Revision: 1.59 $
*
* Last modification: $Date: 2003/07/25 11:51:49 $
* $Id: Parser.java,v 1.58 2003/07/25 11:51:49 smcsporr Exp $
* Last modification: $Date: 2003/07/25 12:12:11 $
* $Id: Parser.java,v 1.59 2003/07/25 12:12:11 smcsporr Exp $
*/
 
package src.parser;
 
import java.awt.Point;
 
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
35,10 → 37,13
import org.apache.xerces.impl.Constants;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.XMLGrammarPreparser;
import org.apache.xerces.xni.grammars.Grammar;
import org.apache.xerces.xni.grammars.XSGrammar;
import org.apache.xerces.xni.grammars.XMLDTDDescription;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
import org.apache.xerces.xni.parser.XMLErrorHandler;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParseException;
 
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
60,7 → 65,7
*
* @author Group 5
*
* @version $Revision: 1.58 $ Last modification: $Date: 2003/07/25 11:51:49 $
* @version $Revision: 1.59 $ Last modification: $Date: 2003/07/25 12:12:11 $
*/
public class Parser {
 
68,10 → 73,6
private static final String SYMBOL_TABLE =
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
/** Xerces XNI property identifier for grammar pool. */
private static final String GRAMMAR_POOL =
Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
/** Xerces feature ID for namespace processing. */
private static final String NAMESPACE_FEAT_ID =
"http://xml.org/sax/features/namespaces";
90,7 → 91,13
 
/** Used to parse a DTD or schema grammar. */
private XMLGrammarPreparser preParser = new XMLGrammarPreparser();
/** An error handler for the grammar preparser that catches grammar parsing errors. */
private GrammarErrorHandler grammarErrorHandler = new GrammarErrorHandler();
 
/** An XML grammar implementation representation for grammar parsing. */
Grammar grammar;
 
/** The main Document of this class */
private DocumentImpl doc;
106,7 → 113,7
private Transformer myTransformer;
private TransformerFactory transFactory;
/* Create a new DocumentBuilderFactory. */
/** Create a new DocumentBuilderFactory. */
private DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
 
/**
130,6 → 137,9
dBF.setValidating(false);
dBF.setNamespaceAware(false);
preParser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
preParser.setErrorHandler(grammarErrorHandler);
}
 
 
173,7 → 183,7
public void parse(File file) {
try {
 
DocumentBuilder dB = dBF.newDocumentBuilder();
/* Check if the file exists and is readable. */
183,19 → 193,19
log(LogInterface.TYPE_ERROR, " The file " + file.getName() +
" is not accessable.");
}
 
log(LogInterface.TYPE_INFO, "File " + file.getName() + " successfully parsed");
}
 
/* Catch and log factory configuration errors. */
catch (FactoryConfigurationError factoryConfigExc) {
factoryConfigExc.printStackTrace();
 
/* Show error icon and message in the status panel. */
setNewIcon(StatusInterface.ICON_ERROR);
sendMessage("Parser factory error: " + factoryConfigExc.getMessage());
}
 
/* Catch and log configuration errors. */
catch (ParserConfigurationException parseConfigExc) {
297,13 → 307,18
try {
/* Parse the specified DTD file and cast the grammar's description to an
* XMLDTDDescription object. */
grammarDescription = (XMLDTDDescription) (preParser.preparseGrammar(XMLGrammarDescription.XML_DTD,
new XMLInputSource(null, DTDFile.getPath(), null)).getGrammarDescription());
/* Parse the specified DTD file. */
grammar = preParser.preparseGrammar(XMLGrammarDescription.XML_DTD,
new XMLInputSource(null, DTDFile.getPath(), null));
if (grammar != null) {
/* Cast parsed grammar into a DTD grammar. */
grammarDescription = (XMLDTDDescription) grammar.getGrammarDescription();
 
/* Get the root name. */
root = grammarDescription.getRootName();
/* Get the root name. */
root = grammarDescription.getRootName();
}
}
/* Catch and log IO-errors. */
356,21 → 371,27
preParser.setFeature(SCHEMA_CHECKING_FEAT_ID, true);
try {
/* Parse the specified schema file and cast the grammar to an XSGrammar object. */
schemaGrammar = (XSGrammar) preParser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
new XMLInputSource(null, schemaFile.getPath(), null));
/* Parse the specified schema file. */
grammar = preParser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
new XMLInputSource(null, schemaFile.getPath(), null));
if (grammar != null) {
 
/* Cast parsed grammar into a schema grammar. */
schemaGrammar = (XSGrammar) grammar;
/* Create an XSModel from the parsed grammar. */
XSModel schemaModel = schemaGrammar.toXSModel();
/* Create an XSModel from the parsed grammar. */
XSModel schemaModel = schemaGrammar.toXSModel();
/* Retrieve the top level elements from the XSModel. */
XSNamedMap schemaComponentMap =
schemaModel.getComponents(XSConstants.ELEMENT_DECLARATION);
/* Get the number of elements and store these in a Vector. */
int componentMapLength = (schemaComponentMap.getLength()-1);
for (int i = 0; i <= componentMapLength; i++) {
elementNames.add(schemaComponentMap.item(i).getName());
/* Retrieve the top level elements from the XSModel. */
XSNamedMap schemaComponentMap =
schemaModel.getComponents(XSConstants.ELEMENT_DECLARATION);
/* Get the number of elements and store these in a Vector. */
int componentMapLength = (schemaComponentMap.getLength()-1);
for (int i = 0; i <= componentMapLength; i++) {
elementNames.add(schemaComponentMap.item(i).getName());
}
}
}
449,4 → 470,78
private void sendMessage(String message) {
statusInterfaceReference.setMessage(message);
}
/**
* <code>setPoint</code> sets a new point to display in the status panel.
*
* @param point: A <code>Point</code> object containing the information to display.
*/
private void setPoint(Point point) {
statusInterfaceReference.setPoint(point);
}
/**
* The <b><code>GrammarErrorHandler</code></b> class supplies an error handler for the
* an XML grammar preparser.
* An error handler retrieves and handles error events sent by a parser. These
* errors include warnings, errors and fatal errors.
*
* @author S. McSporran
*
* @version $Revision: 1.26 $ Last modification: $Date: 2003/07/25 11:52:17 $
*/
private class GrammarErrorHandler implements XMLErrorHandler {
/**
* A method that catches parsing warnings and sends them to the log panel.
* Warnings indicate that something might be missing or incorrect in a grammar file,
* while no wellformedness rule has been broken.
*
* @param domain The domain of the warning, suggested to be a valid URI.
* @param key The warning key; can be any string and is implementation dependent.
* @param xpe A warning event sent by a parser.
*
* author S. McSporran
*
* Last revision: 25-Jul-2003 by S. McSporran
*/
public void warning(String domain, String key, XMLParseException xpe){
log(LogInterface.TYPE_WARNING, "Grammar parsing warning: " + xpe.getMessage());
}
/**
* A method that catches parsing errors and sends them to the log panel.
* Errors occur during parsing, if a grammar rule has been broken, but
* the parser will be able to continue.
*
* @param domain The domain of the error, suggested to be a valid URI.
* @param key The error key; can be any string and is implementation dependent.
* @param xpe An error event sent by a parser.
*
* author S. McSporran
*
* Last revision: 25-Jul-2003 by S. McSporran
*/
public void error(String domain, String key, XMLParseException xpe) {
log(LogInterface.TYPE_ERROR, "Error ! Grammar parsing failure: " + xpe.getMessage());
}
/**
* A method that catches parsing fatal errors and sends them to the log panel.
* Fatal errors indicate that a grammar rule has been broken that makes continued
* parsing impossible.
*
* @param domain The domain of the fatal error, suggested to be a valid URI.
* @param key The fatal error key; can be any string and is implementation dependent.
* @param xpe A fatal error event sent by a parser.
*
* author S. McSporran
*
* Last revision: 25-Jul-2003 by S. McSporran
*/
public void fatalError(String domain, String key, XMLParseException xpe) {
log(LogInterface.TYPE_ERROR, "Fatal error ! Grammar parsing failure: " +
xpe.getMessage());
}
}
}