Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 801 → Rev 802

/sun/xmleditor/trunk/src/parser/Parser.java
4,10 → 4,10
* Coded by: Group 5, software practice summer 2003
* University of Bielefeld, Germany
*
* @version $Revision: 1.57 $
* @version $Revision: 1.58 $
*
* Last modification: $Date: 2003/07/24 19:44:08 $
* $Id: Parser.java,v 1.57 2003/07/24 19:44:08 smcsporr Exp $
* Last modification: $Date: 2003/07/25 11:51:49 $
* $Id: Parser.java,v 1.58 2003/07/25 11:51:49 smcsporr Exp $
*/
 
package src.parser;
60,7 → 60,7
*
* @author Group 5
*
* @version $Revision: 1.57 $ Last modification: $Date: 2003/07/24 19:44:08 $
* @version $Revision: 1.58 $ Last modification: $Date: 2003/07/25 11:51:49 $
*/
public class Parser {
 
176,17 → 176,14
DocumentBuilder dB = dBF.newDocumentBuilder();
/* Check if the file can be read. */
if (file.canRead()) {
/* Check if the file exists and is readable. */
if (file.exists() && file.canRead()) {
doc = (DocumentImpl)dB.parse(file);
} else {
log(LogInterface.TYPE_ERROR, " The file " + file.getName() +
" is not accessable.");
}
else {
log(LogInterface.TYPE_ERROR, " The file " + file.getName() + " is not readable.");
}
log(LogInterface.TYPE_INFO, "File " + file.getName() + " successfully parsed");
}
233,13 → 230,11
try {
DocumentBuilder dB = dBF.newDocumentBuilder();
doc = (DocumentImpl)dB.parse(new InputSource(new StringReader(inputString)));
}
/* Catch and log factory configuration errors. */
catch (FactoryConfigurationError factoryConfigExc) {
 
factoryConfigExc.printStackTrace();
 
/* Show error icon and message in the status panel. */
271,6 → 266,9
/**
* Parses a DTD grammar and returns the root element of an instance document.
*
* The file must be accessable to parse it, non-existing or non-readable
* files will produce an error message.
*
* @param DTDFile The DTD grammar file to be parsed.
*
* @return A <code>Vector</code> containing the name of the root element.
277,36 → 275,46
*
* @see java.util.Vector
*
* Last revision: 24-Jul-2003 by S. McSporran
* Last revision: 25-Jul-2003 by S. McSporran
*/
public String parseDTDGrammar(File DTDFile) {
XMLDTDDescription grammarDescription;
/* A string used to store the root element retrieved from the DTD. */
String root = null;
/* Set the grammar type to DTD. */
preParser.registerPreparser(XMLGrammarDescription.XML_DTD, null);
/* Checks, if the file exists and is accessable. */
if (DTDFile.exists() && DTDFile.canRead()) {
 
/* XNI specific implementation of a DTD grammar description. */
XMLDTDDescription grammarDescription;
 
/* Set the grammar type to DTD. */
preParser.registerPreparser(XMLGrammarDescription.XML_DTD, null);
/* Set the required parser features. */
preParser.setFeature(NAMESPACE_FEAT_ID, true);
preParser.setFeature(VALIDATION_FEAT_ID, true);
/* Set the required parser features. */
preParser.setFeature(NAMESPACE_FEAT_ID, true);
preParser.setFeature(VALIDATION_FEAT_ID, true);
try {
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 and cast the grammar's description to an
* XMLDTDDescription object. */
grammarDescription = (XMLDTDDescription) (preParser.preparseGrammar(XMLGrammarDescription.XML_DTD,
new XMLInputSource(null, DTDFile.getPath(), null)).getGrammarDescription());
 
/* Get the root name. */
root = grammarDescription.getRootName();
/* Get the root name. */
root = grammarDescription.getRootName();
}
/* Catch and log IO-errors. */
catch (IOException ioe) {
log(LogInterface.TYPE_ERROR, "I/O-error while parsing DTD file "
+ DTDFile.getPath() + " : " + ioe.getMessage());
}
} else {
log(LogInterface.TYPE_ERROR, "The file " + DTDFile.getName() +
" is not accessable.");
}
catch (IOException ioe) {
log(LogInterface.TYPE_ERROR, "I/O-error while parsing DTD file "
+ DTDFile.getPath() + " : " + ioe.getMessage());
}
return root;
}
315,6 → 323,9
* Parses an XML schema grammar and returns the top-level elements
* that are candidates of being the root element of an instance document.
*
* The file must be accessable to parse it, non-existing or non-readable
* files will produce an error message.
*
* @param schemaFile The XML schema file to be parsed.
*
* @return A <code>Vector</code> containing the names of the schema's top
325,44 → 336,52
* Last revision: 21-Jul-2003 by S. McSporran
*/
public Vector parseSchemaGrammar(File schemaFile) {
/** An XML schema grammar used for grammar parsing. */
XSGrammar schemaGrammar;
 
/* A Vector used to store the names of the schema's top level elements. */
Vector elementNames = new Vector();
 
/* Checks, if the file exists and is accessable. */
if (schemaFile.exists() && schemaFile.canRead()) {
/* Set the grammar type to XML schema. */
preParser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
/* An XML schema grammar used for grammar parsing. */
XSGrammar schemaGrammar;
/* Set the required parser features. */
preParser.setFeature(NAMESPACE_FEAT_ID, true);
preParser.setFeature(VALIDATION_FEAT_ID, true);
preParser.setFeature(VALIDATION_SCHEMA_FEAT_ID, true);
preParser.setFeature(SCHEMA_CHECKING_FEAT_ID, true);
/* Set the grammar type to XML schema. */
preParser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
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));
/* Set the required parser features. */
preParser.setFeature(NAMESPACE_FEAT_ID, true);
preParser.setFeature(VALIDATION_FEAT_ID, true);
preParser.setFeature(VALIDATION_SCHEMA_FEAT_ID, true);
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));
/* 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);
/* 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());
/* 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());
}
}
}
catch (IOException ioe) {
log(LogInterface.TYPE_ERROR, "I/O-error while parsing schema file "
+ schemaFile.getPath() + " : " + ioe.getMessage());
/* Catch and log IO-errors. */
catch (IOException ioe) {
log(LogInterface.TYPE_ERROR, "I/O-error while parsing schema file "
+ schemaFile.getPath() + " : " + ioe.getMessage());
}
} else {
log(LogInterface.TYPE_ERROR, "The file " + schemaFile.getName() +
" is not accessable.");
}
return elementNames;