venice-main-classic/src/com/silverwrist/venice/db/PostLinkRewriter.java
Eric J. Bowersox 4c5c7ffe85 added the ability for the HTML checker to keep track of internal and external
references in any post, so we can do trackbacks
2004-12-30 08:08:13 +00:00

199 lines
5.7 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.db;
import java.sql.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.htmlcheck.Rewriter;
import com.silverwrist.venice.htmlcheck.RewriterServices;
import com.silverwrist.venice.htmlcheck.MarkupData;
import com.silverwrist.venice.except.ValidationException;
import com.silverwrist.venice.svc.GlobalSite;
import com.silverwrist.venice.util.IDUtils;
public class PostLinkRewriter implements Rewriter
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
public static final String URI_PREFIX = "x-postlink:";
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private final GlobalSite globalsite; // global site containing utilities
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
public PostLinkRewriter(GlobalSite globalsite)
{
this.globalsite = globalsite;
} // end constructor
/*--------------------------------------------------------------------------------
* Internal functions
*--------------------------------------------------------------------------------
*/
private static final String buildPostLink(PostLinkDecoder pl, PostLinkDecoderContext ctxt)
{
StringBuffer b = new StringBuffer();
boolean started = false;
if (pl.getCommunity()==null)
b.append(ctxt.getCommunityName());
else
{ // append the real community name
started = true;
b.append(pl.getCommunity());
} // end else
b.append('!');
if (pl.getConference()==null)
{ // need to default the context
if (started)
return b.toString();
b.append(ctxt.getConferenceName());
} // end if
else
{ // append the proper conference name
started = true;
b.append(pl.getConference());
} // end else
b.append('.');
if (pl.getTopic()==-1)
{ // need to default the topic number
if (started)
return b.toString();
b.append(ctxt.getTopicNumber());
} // end if
else
{ // append the proper topic number
started = true;
b.append(pl.getTopic());
} // end else
// append the post link information, if applicable
b.append('.');
if (pl.getFirstPost()==-1)
return b.toString();
b.append(pl.getFirstPost());
if (pl.getFirstPost()==pl.getLastPost())
return b.toString();
b.append('-');
if (pl.getLastPost()==-1)
return b.toString();
b.append(pl.getLastPost());
return b.toString();
} // end buildPostLink
/*--------------------------------------------------------------------------------
* Implementations from interface Rewriter
*--------------------------------------------------------------------------------
*/
public String getName()
{
return null;
} // end getName
public MarkupData rewrite(String data, RewriterServices svc)
{
PostLinkDecoderContext ctxt;
try
{ // attempt to get the decoder context
ctxt = (PostLinkDecoderContext)(svc.getRewriterContextValue("PostLinkDecoderContext"));
if (ctxt==null)
return null; // decoder can't function
} // end try
catch (ClassCastException x)
{ // decoder can't function without the context
return null;
} // end catch
PostLinkDecoder pl;
try
{ // create the post link decoder
pl = new PostLinkDecoder(data);
if (pl.needDatabaseVerification())
{ // open up a database connection and verify the community/conference names
Connection conn = null;
try
{ // verify against the database
conn = globalsite.getConnection(null);
pl.verifyNames(conn);
} // end try
catch (SQLException e)
{ // just return null on failures
return null;
} // end catch
finally
{ // release the connection when we're done
SQLUtil.shutdown(conn);
} // end finally
} // end if
} // end try
catch (ValidationException ve)
{ // we can't validate the post link - just bail out
return null;
} // end catch
// build the post link and add it as an internal reference
String link = buildPostLink(pl,ctxt);
svc.addInternalReference(link);
// build the necessary markup and return it
StringBuffer open_a = new StringBuffer("<a href=\"");
open_a.append(URI_PREFIX).append(link).append("\"");
String catenate = svc.getRewriterAttrValue("ANCHORTAIL");
if (!(StringUtil.isStringEmpty(catenate)))
open_a.append(' ').append(catenate);
open_a.append('>');
return new MarkupData(open_a.toString(),data,"</a>");
} // end rewrite
} // end class PostLinkRewriter