[Httpunit-commit] CVS: httpunit/site/com/meterware/website PageFragment.java,1.1,1.2 Site.java,1.4,1
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-05-26 15:42:16
|
Update of /cvsroot/httpunit/httpunit/site/com/meterware/website
In directory sc8-pr-cvs1:/tmp/cvs-serv20538/site/com/meterware/website
Modified Files:
PageFragment.java Site.java SiteElement.java WebPage.java
Log Message:
Enhancements to site generator
Index: PageFragment.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/website/PageFragment.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PageFragment.java 18 May 2003 05:20:47 -0000 1.1
+++ PageFragment.java 26 May 2003 15:42:13 -0000 1.2
@@ -24,42 +24,46 @@
import org.xml.sax.InputSource;
import org.cyberneko.html.parsers.DOMParser;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
-import java.beans.IntrospectionException;
-import java.lang.reflect.InvocationTargetException;
-import java.text.ParseException;
import com.meterware.xml.DocumentSemantics;
import com.meterware.website.FragmentTemplate;
-import com.meterware.httpunit.site.Faq;
-import com.meterware.httpunit.site.News;
-import com.meterware.httpunit.site.Citations;
-import com.meterware.httpunit.site.Developers;
/**
*
- * @author <a href="mailto:rus...@ht...">Russell Gold</a>
+ * @author <a href="mailto:rus...@me...">Russell Gold</a>
**/
public class PageFragment {
private String _source;
+ private String _extract;
+ private static File _root;
- public String asText() throws IOException, SAXException, IntrospectionException, InvocationTargetException, IllegalAccessException, ParserConfigurationException, ParseException {
- File file = new File( _source );
- if (isXmlFile( file )) {
- return getXMLBasedFragment( file );
- } else if (isHtmlFile( file )) {
- return getHtmlBasedFragment( file );
- } else {
- return getTextFragment( file );
+
+ static void setRoot( File root ) {
+ _root = root;
+ }
+
+
+ public String asText() {
+ try {
+ File file = new File( _source );
+ if (!file.exists()) file = new File( _root, _source );
+ if (isXmlFile( file )) {
+ return getXMLBasedFragment( file );
+ } else if (isHtmlFile( file )) {
+ return getHtmlBasedFragment( file );
+ } else {
+ return getTextFragment( file );
+ }
+ } catch (SAXException e) {
+ throw new RuntimeException( "Error parsing " + _source + ": " + e );
+ } catch (IOException e) {
+ throw new RuntimeException( "Error reading " + _source + ": " + e );
}
}
@@ -80,7 +84,7 @@
private String getHtmlBasedFragment( File file ) throws IOException, SAXException {
- org.cyberneko.html.parsers.DOMParser parser = new DOMParser();
+ DOMParser parser = new DOMParser();
parser.parse( new InputSource( new FileInputStream( file ) ) );
Document document = parser.getDocument();
NodeList nl = document.getElementsByTagName( "body" );
@@ -124,21 +128,11 @@
}
- private String getXMLBasedFragment( File file ) throws SAXException, IOException, ParserConfigurationException, IntrospectionException, IllegalAccessException, InvocationTargetException, ParseException {
- Document document = parseDocument( file );
- FragmentTemplate template = FragmentTemplate.getTemplateFor( com.meterware.xml.DocumentSemantics.getRootNode( document ).getNodeName() );
- com.meterware.xml.DocumentSemantics.build( document, template );
- return template.asText();
- }
-
-
- private static Document parseDocument( File file ) throws SAXException, IOException, ParserConfigurationException {
- DocumentBuilder result;
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- result = factory.newDocumentBuilder();
- DocumentBuilder db = result;
- final Document document = db.parse( file );
- return document;
+ private String getXMLBasedFragment( File file ) throws SAXException, IOException {
+ Document document = DocumentSemantics.parseDocument( file );
+ FragmentTemplate template = FragmentTemplate.getTemplateFor( DocumentSemantics.getRootNode( document ).getNodeName() );
+ DocumentSemantics.build( document, template, file.getAbsolutePath() );
+ return _extract == null ? template.asText() : DocumentSemantics.getStringProperty( _extract, template );
}
@@ -146,4 +140,8 @@
_source = source;
}
+
+ public void setExtract( String extract ) {
+ _extract = extract;
+ }
}
Index: Site.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/website/Site.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Site.java 18 May 2003 14:14:44 -0000 1.4
+++ Site.java 26 May 2003 15:42:13 -0000 1.5
@@ -24,6 +24,7 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
@@ -42,6 +43,7 @@
**/
public class Site {
+ private ArrayList _commonElements = new ArrayList();
private ArrayList _pages = new ArrayList();
private String _project;
@@ -51,10 +53,11 @@
private CopyRight _copyRight;
- public static void generate( File siteFile, File directory ) throws SAXException, IOException, ParserConfigurationException, IntrospectionException, IllegalAccessException, InvocationTargetException, ParseException {
- Document document = parseDocument( siteFile );
+ public static void generate( File siteFile, File directory ) throws SAXException, IOException {
+ PageFragment.setRoot( siteFile.getParentFile() );
+ Document document = DocumentSemantics.parseDocument( siteFile );
Site site = new Site();
- DocumentSemantics.build( document, site );
+ DocumentSemantics.build( document, site, siteFile.getAbsolutePath() );
WebPage.setRoot( directory );
for (int i = 0; i < site._pages.size(); i++) {
@@ -64,8 +67,10 @@
}
- private static Document parseDocument( File file ) throws SAXException, IOException, ParserConfigurationException {
- return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( file );
+ public CommonElement createCommonElement() {
+ CommonElement element = new CommonElement();
+ _commonElements.add( element );
+ return element;
}
@@ -198,6 +203,34 @@
}
+ public class CommonElement {
+ private ArrayList _fragments = new ArrayList();
+ private String _div;
+
+
+ public PageFragment createFragment() {
+ PageFragment fragment = new PageFragment();
+ _fragments.add( fragment );
+ return fragment;
+ }
+
+
+ public void setDiv( String div ) {
+ _div = div;
+ }
+
+
+ public void appendTo( StringBuffer sb ) {
+ sb.append( "<div id='" ).append( _div ).append( "'>" ).append( FragmentTemplate.LINE_BREAK );
+ for (int i = 0; i < _fragments.size(); i++) {
+ PageFragment pageFragment = (PageFragment) _fragments.get( i );
+ sb.append( pageFragment.asText() ).append( FragmentTemplate.LINE_BREAK );
+ }
+ sb.append( "</div>" ).append( FragmentTemplate.LINE_BREAK );
+ }
+ }
+
+
class Template implements SiteTemplate {
public void appendPageHeader( StringBuffer sb, WebPage currentPage ) {
@@ -208,6 +241,11 @@
sb.append( "<image src='" ).append( relativeURL( currentPage.getLocation(), _logo ) ).append( "'/>" ).append( FragmentTemplate.LINE_BREAK );
sb.append( "<h1>" ).append( currentPage.getTitle() ).append( "</h1>" ).append( FragmentTemplate.LINE_BREAK );
appendMenu( sb, currentPage.getLocation() );
+
+ for (int i = 0; i < _commonElements.size(); i++) {
+ CommonElement commonElement = (CommonElement) _commonElements.get( i );
+ commonElement.appendTo( sb );
+ }
sb.append( "<div id='Content'>" ).append( FragmentTemplate.LINE_BREAK );
if (currentPage.isLicense()) sb.append( "<p>" ).append( _copyRight.getNotice() ).append( "</p>" ). append( FragmentTemplate.LINE_BREAK );
}
Index: SiteElement.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/website/SiteElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SiteElement.java 18 May 2003 05:20:47 -0000 1.1
+++ SiteElement.java 26 May 2003 15:42:13 -0000 1.2
@@ -35,7 +35,7 @@
**/
interface SiteElement {
- void generatePage( SiteTemplate template ) throws ParserConfigurationException, IllegalAccessException, SAXException, IOException, ParseException, IntrospectionException, InvocationTargetException;
+ void generatePage( SiteTemplate template );
void appendMenuItem( StringBuffer sb, SiteTemplate template, String currentLocation );
Index: WebPage.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/website/WebPage.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WebPage.java 18 May 2003 05:20:47 -0000 1.1
+++ WebPage.java 26 May 2003 15:42:13 -0000 1.2
@@ -19,16 +19,10 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
-import org.xml.sax.SAXException;
-
-import javax.xml.parsers.ParserConfigurationException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
-import java.beans.IntrospectionException;
-import java.lang.reflect.InvocationTargetException;
-import java.text.ParseException;
import com.meterware.website.MenuTarget;
import com.meterware.website.PageFragment;
@@ -93,20 +87,24 @@
}
- public void generatePage( SiteTemplate template ) throws ParserConfigurationException, IllegalAccessException, SAXException, IOException, ParseException, IntrospectionException, InvocationTargetException {
- StringBuffer sb = new StringBuffer();
- template.appendPageHeader( sb, this );
- for (int i = 0; i < _fragments.size(); i++) {
- PageFragment pageFragment = (PageFragment) _fragments.get( i );
- sb.append( pageFragment.asText() ).append( FragmentTemplate.LINE_BREAK );
+ public void generatePage( SiteTemplate template ) {
+ try {
+ StringBuffer sb = new StringBuffer();
+ template.appendPageHeader( sb, this );
+ for (int i = 0; i < _fragments.size(); i++) {
+ PageFragment pageFragment = (PageFragment) _fragments.get( i );
+ sb.append( pageFragment.asText() ).append( FragmentTemplate.LINE_BREAK );
+ }
+ template.appendPageFooter( sb, this );
+
+ File file = new File( _root, _location );
+ file.getParentFile().mkdir();
+ FileWriter fw = new FileWriter( file );
+ fw.write( sb.toString() );
+ fw.close();
+ } catch (IOException e) {
+ throw new RuntimeException( "Error writing page '" + e );
}
- template.appendPageFooter( sb, this );
-
- File file = new File( _root, _location );
- file.getParentFile().mkdir();
- FileWriter fw = new FileWriter( file );
- fw.write( sb.toString() );
- fw.close();
}
|