Skip to content

Commit

Permalink
Added method to remove all ampersands properly from text strings and …
Browse files Browse the repository at this point in the history
…unit test for it.
  • Loading branch information
SomeoneElseOSM committed Jan 8, 2014
1 parent 41aed1c commit 4ef5cac
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
56 changes: 41 additions & 15 deletions src/Notes01.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,13 @@ private static void process_notes_xml( Node root_node, String passed_display_nam
if ( comment_action.equals( "opened" ))
{
comment_open_text = myGetNodeValue( this_l4_item );

/* ------------------------------------------------------------------------------------------------------------
* Remove any & from the string. This must be done because MapSource doesn't like "&" in comment text, despite
* the fact that it's actually possible to enter it on the Garmin keyboard.
* Escape any & from in string. This must be done because MapSource doesn't like raw "&" in comment text. We
* change to "&amp;" as that's what an ampersand entered on the Garmin keyboard would come through as.
* ------------------------------------------------------------------------------------------------------------ */
int i = comment_open_text.indexOf( '&' );

if ( i != -1 )
{
if ( i == 0 )
{
comment_open_text = "and " + comment_open_text.substring( i+1 );
}
else
{
comment_open_text = comment_open_text.substring( 0, i ) + " and " + comment_open_text.substring( i+1 );
}
}
comment_open_text = resolve_ampersands( comment_open_text );

/* ------------------------------------------------------------------------------------------------------------
* The actual "note" on the Garmin device itself will be truncated after 30 characters.
*
Expand Down Expand Up @@ -429,7 +419,43 @@ private static void process_notes_xml( Node root_node, String passed_display_nam
}
}


/* ------------------------------------------------------------------------------------------------------------
* Any strings sent to a Garmin (via MapSource or GPSBabel) must have ampersands escaped so that "&" is sent
* as "&amp;"
*
* A future option may be to use e.g.
* http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
* but I'm not aware yet of anything other than ampersand that causes problems, so haven't done that yet.
* ------------------------------------------------------------------------------------------------------------ */
static String resolve_ampersands( String comment_open_text )
{
String ampersand_replacement = "&amp;";
String result_text = comment_open_text;

int i = result_text.indexOf( '&' );

while ( i != -1 )
{
if ( i == 0 )
{
result_text = ampersand_replacement + result_text.substring( i+1 );
}
else
{
result_text = result_text.substring( 0, i ) + ampersand_replacement + result_text.substring( i+1 );
}

/* ------------------------------------------------------------------------------------------------------------
* Start searching for any text after the "&amp;" that we have just added.
* ------------------------------------------------------------------------------------------------------------ */
i = result_text.indexOf( '&', i + ampersand_replacement.length() );
}

return result_text;
}


static void process_notes_url_common ( URL passed_url, String passed_display_name, String passed_symbol ) throws Exception
{
if ( arg_debug >= Log_Informational_2 )
Expand Down
64 changes: 64 additions & 0 deletions src/Notes01TestAmpersand01.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import static org.junit.Assert.*;

import org.junit.Test;


public class Notes01TestAmpersand01
{

@Test
/* ------------------------------------------------------------------------------
* "resolve_ampersands" is supposed to replace all examples of "&" and replace
* them with "&amp;". We test this by processing various strings and verifying
* that the result strings have "&amp;" where the original strings had just "&".
*
* The test strings chosen are ones "likely to cause a problem".
* ------------------------------------------------------------------------------ */
public void testResolve_ampersands()
{
testResolve_ampersands_string( "wibble" );
testResolve_ampersands_string( "foo & bar" );
testResolve_ampersands_string( "foo & bar & baz" );
testResolve_ampersands_string( "foo && bar" );
testResolve_ampersands_string( "<cmt>Three bus stops here - one too many. This one was originally across the road at http://www.openstreetmap.org/?mlat=53.16487 and mlon=-1.23164&zoom=18#map=18/53.16487/-1.23164 - need to check that there is a bus stop here outside the garage not the other side of Radmanthwaite Road.</cmt>" );
testResolve_ampersands_string( "" );
testResolve_ampersands_string( "&" );
testResolve_ampersands_string( "&&" );
}

private void testResolve_ampersands_string( String test1 )
{
Notes01 myNotes01 = new Notes01();
String test2 = myNotes01.resolve_ampersands( test1 );
testResolve_ampersands_string_test( test1, test2 );
}


/* ------------------------------------------------------------------------------
* This section is called both with the original "before and after" strings and
* recursively with the sections of the strings after "&" and "&amp;"
* respectively.
* ------------------------------------------------------------------------------ */
private void testResolve_ampersands_string_test( String test1, String test2 )
{
/* ------------------------------------------------------------------------------
* First, compare the part of the resulting string before the first ampersand.
* ------------------------------------------------------------------------------ */
int i1 = test1.indexOf( "&" );
int i2 = test2.indexOf( "&amp;" );
assertEquals( i1, i2 );

/* ------------------------------------------------------------------------------
* Next we need to compare the part of the string after the first ampersand.
* We only need to do this is there is a bit of string after the first ampersand.
*
* We do this by recursively calling with the remaining parts of the string.
* ------------------------------------------------------------------------------ */
if ( i1 != -1 )
{
String rest1 = test1.substring( i1+1 );
String rest2 = test2.substring( i2+5 );
testResolve_ampersands_string_test( rest1, rest2 );
}
}
}

0 comments on commit 4ef5cac

Please sign in to comment.