alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  
"; } public static String blockmessage(String text) { return "<p class='diff'>

Friki example source code file (Model.java)

This example Friki source code file (Model.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Friki tags/keywords

diffvisitor, diffvisitor, io, ioexception, model, no, page, policy, string, string, stringbuffer, stringbuffer, stringwriter, text, tract, tract, util

The Friki Model.java source code

package com.efsol.friki;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Date;

import java.io.Writer;
import java.io.StringWriter;
import java.io.IOException;

import jlibdiff.Diff;
import jlibdiff.Hunk;
import jlibdiff.HunkAdd;
import jlibdiff.HunkChange;
import jlibdiff.HunkDel;
import jlibdiff.HunkVisitor;

import org.stringtree.factory.Container;
import org.stringtree.factory.DelegatedTractFetcher;
import org.stringtree.util.StringUtils;
import org.stringtree.util.tract.Tract;

class DiffVisitor
	extends HunkVisitor
{
	private StringBuffer buf;
	private int softChanges = 0;
	private int hardChanges = 0;
	
	public DiffVisitor()
	{
		start();
	}
	
	public static String message(String text)
	{
		return "<tr>
" + text + "
\n" + message(text) + "</table>

\n"; } public void start() { buf = new StringBuffer("<p class='diff'>
\n" + "<tr>\n"); } public void end() { buf.append("</table>

\n"); } public void load(Diff diff) { start(); diff.accept(this); end(); } public String convert(Diff diff) { load(diff); return toString(); } private void addLines(Hunk hunk) { int low = hunk.lowLine(0); int high = hunk.highLine(0); buf.append("<td class='lines'>"); buf.append(Integer.toString(low)); if (high != low) { buf.append(" - "); buf.append(Integer.toString(high)); } buf.append("</td>"); } public void visitHunkAdd(HunkAdd hunk) { String text = hunk.getNewContents(); if (!StringUtils.isBlank(text)) { buf.append("<tr>"); addLines(hunk); buf.append("<td class='added'>"); buf.append(text); buf.append("</td>\n"); ++hardChanges; } else { ++softChanges; } } public void visitHunkDel(HunkDel hunk) { String text = hunk.getOldContents(); if (!StringUtils.isBlank(text)) { buf.append("<tr>"); addLines(hunk); buf.append("<td> \n"); ++hardChanges; } else { ++softChanges; } } public void visitHunkChange(HunkChange hunk) { String text = hunk.getNewContents(); if (!StringUtils.isBlank(text)) { buf.append("<tr>"); addLines(hunk); buf.append("<td> \n"); ++hardChanges; } else { ++softChanges; } } public String toString() { String ret; if (hardChanges > 0) { ret = buf.toString(); } else if (softChanges > 0) { ret = message("No significant changes (only whitespace)"); } else { ret = message("No Changes"); } return ret; } } public class Model extends DelegatedTractFetcher implements Container { private PageRepository repository; private Renderer renderer; private ContentFilter filter; private Policy policy; private DateFormat logFormat; public Model(PageRepository repository, Renderer renderer, ContentFilter filter, Policy policy) { super(repository); this.repository = repository; this.renderer = renderer; this.filter = filter; this.policy = policy; logFormat = new SimpleDateFormat(policy.get("date.format")); } public String ensurePage(String page) { if (page==null) { page = policy.get("dflPage"); } else { page = repository.unescape(page); } return page; } public String ensureMode(String mode) { String modes = policy.get("modes"); if (!modes.endsWith(",")) { modes = modes + ","; policy.put("modes", modes); } if (mode==null || modes.indexOf("," + mode + ",") == -1) { mode = policy.get("viewMode"); } return mode; } public Tract update(String name, String content) { Tract page = repository.get(name); page.setContent(content); repository.put(name, page); return page; } public Page search(String pattern) throws IOException { Page page = new Page("Search Results"); page.put("hardlines", "true"); page.put("pattern", pattern); StringWriter buf = new StringWriter(); boolean found = false; Iterator it = repository.matchingPages(pattern); while (it.hasNext()) { buf.write("\n "); buf.write((String)it.next()); found = true; } if (!found) { buf.write("Sorry, no pages matched the pattern '"); buf.write(pattern); buf.write("'"); } page.setContent(buf.toString()); return page; } public void render(Tract page, String mode, Writer out, boolean isFiltered) throws IOException { renderer.render(page, mode, out, isFiltered); } public boolean contains(String name) { return repository.contains(name); } public void log(String page, String name, String author, boolean diffs) { StringBuffer ret = new StringBuffer(" "); ret.append(new Date()); ret.append(" ...... "); ret.append(name); if (diffs) { ret.append(" (??" + name + ")"); } if (!StringUtils.isBlank(author)) { ret.append(" ...... "); ret.append(author); } ret.append('\n'); repository.append(page, ret.toString()); } public String makeLinkSource(String name) { return filter.makeLinkSource(name); } public Object getObject(String name) { return repository.get(name); } public Tract get(String name) { String ensured = ensurePage(name); Tract ret = repository.get(ensured); return ret; } public String diff(String pageName) { String ret; Tract page = get(pageName); String previousName = page.getAttribute("previous.version"); if (!StringUtils.isBlank(previousName) || "old/EmptyPage".equals(previousName)) { Tract previous = get(previousName); try { Diff d = new Diff(); DiffVisitor visitor = new DiffVisitor(); String newContent = page.getContent(); String oldContent = previous.getContent(); d.diffString(oldContent, newContent); ret = visitor.convert(d); } catch (IOException ioe) { ioe.printStackTrace(); ret = DiffVisitor.blockmessage("Oops! Error extracting page differences."); } } else { ret = DiffVisitor.blockmessage("No Previous Version."); } return ret; } }

Other Friki examples (source code examples)

Here is a short list of links related to this Friki Model.java source code file:

PositionAddedRemovedChanged
  
"); buf.append(text); buf.append("</td> 
 "); buf.append(text); buf.append("</td>
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.