Add doctype management.

This commit is contained in:
Christian P. MOMON 2010-04-11 18:45:59 +02:00
parent cf6c2c775a
commit 394f69e66b
4 changed files with 47 additions and 8 deletions

View file

@ -1,7 +1,7 @@
package fr.devinsy.xid;
import java.io.*;
import org.w3c.dom.*;
/**
@ -17,6 +17,7 @@ public class FilePresenter extends DomPresenter
protected String sourceFilePathname;
protected long sourceFileTime;
protected String doctype;
/*
*
@ -26,6 +27,7 @@ public class FilePresenter extends DomPresenter
this.sourceFilePathname = null;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
@ -37,6 +39,7 @@ public class FilePresenter extends DomPresenter
this.sourceFilePathname = filePathname;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
@ -48,6 +51,7 @@ public class FilePresenter extends DomPresenter
this.sourceFilePathname = filePathname;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
@ -81,10 +85,10 @@ public class FilePresenter extends DomPresenter
{
StringBuffer result;
String sourceFilePath = this.sourceFilePathname;
logger.info("doXid for file [" + this.sourceFilePathname + "]");
// Get the good tree.
File source = new File (sourceFilePath);
File source = new File (this.sourceFilePathname);
if (source == null)
{
@ -95,17 +99,17 @@ public class FilePresenter extends DomPresenter
}
else if (!source.exists ())
{
String errorMessage = "source file defined but not found (" + sourceFilePath + ")";
String errorMessage = "source file defined but not found (" + this.sourceFilePathname + ")";
logger.error (errorMessage);
result = null;
throw new Exception (errorMessage);
}
else if ((this.doc == null) ||
(this.sourceFileTime != source.lastModified ()))
(this.sourceFileTime != source.lastModified ()))
{
this.sourceFileTime = source.lastModified ();
this.doc = Presenter.fileToTree (sourceFilePath);
this.doc = Presenter.fileToTree (this.sourceFilePathname);
this.doctype = getDoctype(this.sourceFilePathname);
if (this.doc != null)
{
Presenter.addMetaTag (doc, "generator", "XID 0.0");
@ -114,6 +118,9 @@ public class FilePresenter extends DomPresenter
// Build the web page.
StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(source.length()));
htmlCode.write(doctype);
htmlCode.write('\n');
Presenter.doXid (htmlCode, doc, datas);
result = htmlCode.getBuffer();
@ -122,6 +129,33 @@ public class FilePresenter extends DomPresenter
}
static public String getDoctype (String filePathname) throws Exception
{
String result;
//
BufferedReader in = new BufferedReader (new FileReader (filePathname));
String doctype = in.readLine();
in.close();
logger.info("doctype=[" + doctype + "]");
//
if ((doctype.startsWith ("<!DOCTYPE")) ||
(doctype.startsWith ("<!doctype")))
{
result = doctype;
}
else
{
result = null;
}
//
return (result);
}
/*
* Xid a file without data.
*/

View file

@ -99,6 +99,11 @@ public class StringPresenter extends DomPresenter
}
StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(this.html.length()));
if ((this.html.startsWith ("<!DOCTYPE")) ||
(this.html.startsWith ("<!doctype")))
{
htmlCode.write(this.html.substring(0, this.html.indexOf('>')));
}
Presenter.doXid (htmlCode, doc, datas);
StringBuffer htmlTarget = htmlCode.getBuffer();