Skip to content

Commit

Permalink
Refactor project to have generally available URLEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
sturton committed Jul 19, 2013
1 parent e92561e commit 9aa6e52
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 25 deletions.
64 changes: 64 additions & 0 deletions src/main/java/net/sourceforge/schemaspy/util/URLEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2013 Wakaleo Consulting.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sourceforge.schemaspy.util;

import java.io.UnsupportedEncodingException;
import net.sourceforge.schemaspy.Config;

/**
* Percent-encode Strings for use in URLs.
*
* @author sturton
*/
public class URLEncoder {

/*
* Encoding characterset
*/
private String charset = Config.getInstance().getCharset();

public URLEncoder(String charset) {
this.charset = charset ;
}

/**
* Return an URL-encoded version of the specified string in the specified encoding.
*
* <p>Any encoding failure results in the return of the original string.
* </p>
* @param str
* @return
*/
public String encode(String str) {
try {
String url = java.net.URLEncoder.encode(str, charset);
int len = url.length();
StringBuilder buf = new StringBuilder(len * 2); // x2 should limit # of reallocs
for (int i = 0; i < len; i++) {
buf.append( ( '+' == url.charAt(i) )
? "%20"
: url.charAt(i)
);
}
return buf.toString();
}
catch(UnsupportedEncodingException uee)
{
return str;
}
}

}
4 changes: 3 additions & 1 deletion src/main/java/net/sourceforge/schemaspy/view/DotNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.sourceforge.schemaspy.model.Table;
import net.sourceforge.schemaspy.model.TableColumn;
import net.sourceforge.schemaspy.model.TableIndex;
import net.sourceforge.schemaspy.util.URLEncoder;

public class DotNode {
private final Table table;
Expand All @@ -34,6 +35,7 @@ public class DotNode {
private final Set<TableColumn> excludedColumns = new HashSet<TableColumn>();
private final String lineSeparator = System.getProperty("line.separator");
private final boolean displayNumRows = Config.getInstance().isNumRowsEnabled();
private static final URLEncoder urlEncoder = new URLEncoder(Config.DOT_CHARSET);

/**
* Create a DotNode that is a focal point of a diagram.
Expand Down Expand Up @@ -170,7 +172,7 @@ else if (indexColumns.contains(column))

buf.append(" </TABLE>>" + lineSeparator);
if (!table.isRemote() || Config.getInstance().isOneOfMultipleSchemas())
buf.append(" URL=\"" + path + toNCR(tableName) + ".html\"" + lineSeparator);
buf.append(" URL=\"" + path + toNCR( urlEncoder.encode(tableName) ) + ".html\"" + lineSeparator);
buf.append(" tooltip=\"" + toNCR(fqTableName) + "\"" + lineSeparator);
buf.append(" ];");

Expand Down
29 changes: 5 additions & 24 deletions src/main/java/net/sourceforge/schemaspy/view/HtmlFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package net.sourceforge.schemaspy.view;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -32,12 +30,13 @@
import net.sourceforge.schemaspy.util.Dot;
import net.sourceforge.schemaspy.util.HtmlEncoder;
import net.sourceforge.schemaspy.util.LineWriter;
import net.sourceforge.schemaspy.util.URLEncoder;

public class HtmlFormatter {
protected final boolean encodeComments = Config.getInstance().isEncodeCommentsEnabled();
protected final boolean displayNumRows = Config.getInstance().isNumRowsEnabled();
private final boolean isMetered = Config.getInstance().isMeterEnabled();
private final String charset = Config.getInstance().getCharset();
private final URLEncoder urlEncoder = new URLEncoder( Config.getInstance().getCharset());

protected HtmlFormatter() {
}
Expand Down Expand Up @@ -291,32 +290,14 @@ protected void writeFooter(LineWriter html) throws IOException {
}


/**
* Return an URL-encoded version of the specified string in the specified encoding.
/*
* Return an URL-encoded version of the specified string.
*
* @param str
* @param charset
* @return
*/
protected String encodeHref(String str) {
try {
//return URLEncoder.encode(str, charset).replaceAll("+","%20");
String url = URLEncoder.encode(str, charset);
int len = url.length();
StringBuilder buf = new StringBuilder(len * 2); // x2 should limit # of reallocs
for (int i = 0; i < len; i++) {
buf.append( ( '+' == url.charAt(i) )
? "%20"
: url.charAt(i)
);
}
return buf.toString();
}
catch(UnsupportedEncodingException uee)
//catch(Exception ex)
{
return str;
}
return urlEncoder.encode(str);
}


Expand Down

0 comments on commit 9aa6e52

Please sign in to comment.