Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 834 → Rev 835

/sun/xmleditor/trunk/src/document/OurDocument.java
4,15 → 4,16
* Coded by: Group 5, software practice summer 2003
* University of Bielefeld, Germany
*
* @version $Revision: 1.64 $
* @version $Revision: 1.65 $
*
* Last modification: $Date: 2003/07/26 10:51:23 $
* $Id: OurDocument.java,v 1.64 2003/07/26 10:51:23 smcsporr Exp $
* Last modification: $Date: 2003/07/26 10:56:21 $
* $Id: OurDocument.java,v 1.65 2003/07/26 10:56:21 smcsporr Exp $
*/
package src.document;
 
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
 
43,7 → 44,7
*
* @author S. McSporran
*
* @version $Revision: 1.64 $ Last modification: $Date: 2003/07/26 10:51:23 $
* @version $Revision: 1.65 $ Last modification: $Date: 2003/07/26 10:56:21 $
*/
public class OurDocument {
53,7 → 54,6
/** Declarations of a transformer and a factory. */
private TransformerFactory transFactory;
private Transformer docTransformer;
private Transformer trans;
/** A reference variable to the log panel used to print out log messages. */
private LogInterface LogInterfaceReference;
271,6 → 271,7
rootElement.removeAttribute("xsi:schemaLocation");
myDocument = changeDoc;
log(LogInterface.TYPE_INFO, "Schema removed");
} else {
/* Get the root element of the XML file. */
318,16 → 319,21
/* Set the document parameter as source for the transformation. */
DOMSource dSource = new DOMSource(changeDoc);
 
String oldDocType = changeDoc.getDoctype().getName();
 
/* Save the transformation result to a string buffer. */
StringWriter strWriter = new StringWriter();
PrintWriter outputWr = new PrintWriter(strWriter);
StreamResult sResult = new StreamResult(outputWr);
 
if (dtdFile == null){
if (dtdFile == null) {
try {
log(LogInterface.TYPE_INFO, "dtdFile null");
/* Transform the document, delete any DOCTYPE entry. */
trans.setOutputProperty(OutputKeys.STANDALONE, null);
trans.transform(dSource,sResult);
docTransformer.transform(dSource,sResult);
/* Get the buffer associated with the StringWriter outputWr. */
String bufferString = strWriter.getBuffer().toString();
335,7 → 341,7
aParser.parse(bufferString);
myDocument = aParser.getDocument();
log(LogInterface.TYPE_INFO, "DTD "+ myDocument.getDoctype() + " removed.");
log(LogInterface.TYPE_INFO, "DTD "+ oldDocType + " removed.");
}
/* Catch and log transformation errors. */
344,6 → 350,8
}
} else {
log(LogInterface.TYPE_INFO, "dtdFile non-null");
/* Process an identity transform with the parameter DTD file. */
String docTypeFile = dtdFile.getPath();
/* Set the 'DOCTYPE' entry in the XML file. */
373,7 → 381,9
* This method saves a document to a specified file.
*
* A transformer will be used to output the document. An optional
* DOCTYPE-entry will be saved.
* DOCTYPE-entry will be saved. You will need write access according the
* specified path and file name, otherwise writing operations will be
* impossible.
*
* @param saveDoc The document to be saved.
* @param ftsi A valid file name.
382,41 → 392,82
*
* author S. McSporran
*
* Last revision: 23-Jul-2003 by S. McSporran
* Last revision: 26-Jul-2003 by S. McSporran
*/
public void saveDocument(Document saveDoc, File ftsi) {
/* Check, if the file is writeable. */
if (ftsi.canWrite()) {
/* Set the document parameter as input source for the transformer. */
DOMSource dSource = new DOMSource(saveDoc);
/* If the file already exists, check if it is writeable. */
if (ftsi.exists()) {
if (ftsi.canWrite()) {
/* Set the document parameter as input source for the transformer. */
DOMSource dSource = new DOMSource(saveDoc);
 
/* Set the resulting stream to the file. */
StreamResult strResult = new StreamResult(ftsi);
/* Set the resulting stream to the file. */
StreamResult strResult = new StreamResult(ftsi);
 
/* Adds the DOCTYPE-entry to the file to save (if present). */
if (((saveDoc.getDoctype()) != null) && (saveDoc.getDoctype().getInternalSubset() == null)) {
String docTypeFile = (new File(saveDoc.getDoctype().getSystemId())).getName();
docTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeFile);
/* Adds the DOCTYPE-entry to the file to save (if present). */
if (((saveDoc.getDoctype()) != null) && (saveDoc.getDoctype().getInternalSubset() == null)) {
String docTypeFile = (new File(saveDoc.getDoctype().getSystemId())).getName();
docTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeFile);
}
 
try {
/* Create an OutputStream from saveDoc with ftsi as output target. */
docTransformer.transform(dSource, strResult);
log(LogInterface.TYPE_INFO, "Document saved successfully.");
}
 
/* Catch and log exceptions thrown by the transformer. */
catch (TransformerException te) {
log(LogInterface.TYPE_ERROR, "Transformation error: " +
te.getMessage());
}
} else {
log(LogInterface.TYPE_ERROR, "Cannot write to file " + ftsi.getName() + " !");
}
 
}
/* If the file doesn't exist, try to create a new file. */
else {
try {
/* Create an OutputStream from saveDoc with ftsi as output target. */
docTransformer.transform(dSource, strResult);
log(LogInterface.TYPE_INFO, "Document saved successfully.");
}
/* If file creation is successfull (write access). */
if (ftsi.createNewFile()) {
 
/* Catch and log exceptions thrown by the transformer. */
catch (TransformerException te) {
log(LogInterface.TYPE_ERROR, "Transformation error: " +
te.getMessage());
/* Set the document parameter as input source for the transformer. */
DOMSource dSource = new DOMSource(saveDoc);
 
/* Set the resulting stream to the file. */
StreamResult strResult = new StreamResult(ftsi);
 
/* Adds the DOCTYPE-entry to the file to save (if present). */
if (((saveDoc.getDoctype()) != null) && (saveDoc.getDoctype().getInternalSubset()
== null)) {
String docTypeFile = (new File(saveDoc.getDoctype().getSystemId())).getName();
docTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeFile);
}
 
try {
/* Create an OutputStream from saveDoc with ftsi as output target. */
docTransformer.transform(dSource, strResult);
log(LogInterface.TYPE_INFO, "Document saved successfully.");
}
 
/* Catch and log exceptions thrown by the transformer. */
catch (TransformerException te) {
log(LogInterface.TYPE_ERROR, "Transformation error: " +
te.getMessage());
}
}
}
} else {
log(LogInterface.TYPE_ERROR, "Cannot write to file " + ftsi.getName() + " !");
/* Catch and log I/O-errors during file creation. */
catch (IOException ioe) {
log(LogInterface.TYPE_ERROR, "Error creating file " + ftsi.getName() + " ! " +
"Check, if you have write access in the target directory.");
}
}
}
 
/**
* <code>setSaved</code> set's the current <code>DocumentImpl</code> object