Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Text to String literal conversion
In connection with writing some unit tests, I need a utility to convert
text output to a String constant expression.

http://www.snible.org/java2/uni2java.html is almost exactly what I want,
but I would prefer the literals broken at newlines and concatenated.
That is:

aaa\bbb
kkk


gets converted to "aaa\\bbb\nkkk\n". I would prefer:

"aaa\\bbb\n" +
"kkk\n"

This seems like a wheel that should already exist, so I would rather not
invent it myself.

Patricia

Report this thread to moderator Post Follow-up to this message
Old Post
Patricia Shanahan
03-31-08 03:08 AM


Re: Text to String literal conversion
Patricia Shanahan wrote:
> In connection with writing some unit tests, I need a utility to convert
> text output to a String constant expression.
>
> http://www.snible.org/java2/uni2java.html is almost exactly what I want,
> but I would prefer the literals broken at newlines and concatenated.
> That is:
>
> aaa\bbb
> kkk
>
>
> gets converted to "aaa\\bbb\nkkk\n". I would prefer:
>
> "aaa\\bbb\n" +
> "kkk\n"
>
> This seems like a wheel that should already exist, so I would rather not
> invent it myself.

freemarker.template.utility.StringUtil
Should get you as close as the above mentioned.
<http://freemarker.org/>

> Patricia



Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Higgins
03-31-08 03:08 AM


Re: Text to String literal conversion
loop through char by char and if the char gets any special treatment
replace it with something

String s = "hi\n\\kkk"; //hi
//\kkk

StringBuilder b = new StringBuilder();
for(char c : s.toCharArray())
{
switch(c)
{
case '\n':b.append("\\n");break;
case '\r':b.append("\\r");break;
case '\':b.append("\\\\")break;
case '\"':b.append("\\\"");break;
case ''':b.append("\\'");break;
//Add in whatever else
default:b.append(c);
}
}

http://groups.google.com/group/java...eloupment?hl=en

Report this thread to moderator Post Follow-up to this message
Old Post
Chase Preuninger
04-01-08 03:26 AM


Re: Text to String literal conversion
On Sun, 30 Mar 2008 13:14:00 -0700, Patricia Shanahan <pats@acm.org>
wrote, quoted or indirectly quoted someone who said :

>In connection with writing some unit tests, I need a utility to convert
>text output to a String constant expression.

see the code in http://mindprod.com/applet/quoter.html
you can change it to suit yourself.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
04-01-08 03:26 AM


Re: Text to String literal conversion
In article <ov63v352r8amfd5jvltpc9hdfdlcrsbbc9@4ax.com>,
Roedy Green <see_website@mindprod.com.invalid> wrote:

> On Sun, 30 Mar 2008 13:14:00 -0700, Patricia Shanahan <pats@acm.org>
> wrote, quoted or indirectly quoted someone who said :
> 
>
> see the code in http://mindprod.com/applet/quoter.html
> you can change it to suit yourself.

Roedy: This utility is quite comprehensive, but it may be a challenge to
modify: the common13, csv, ledatastream and hunkio directories appear to
be missing:-) Any pointers?

John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Report this thread to moderator Post Follow-up to this message
Old Post
John B. Matthews
04-02-08 12:45 AM


Re: Text to String literal conversion
On Tue, 01 Apr 2008 12:46:39 -0400, "John B. Matthews"
<nospam@nospam.com> wrote, quoted or indirectly quoted someone who
said :

>Roedy: This utility is quite comprehensive, but it may be a challenge to
>modify: the common13, csv, ledatastream and hunkio directories appear to
>be missing:-) Any pointers?

all those packages are available to download.  see
http://mindprod.com/products.html

But if you want to just cannibalise the code to create string
literals, you need only one class in the bundle, namely
ToJavaStringLiteral.java

I have corrected the zip to include common13.  It did not used to need
it. I converted it from AWT to Swing recently, which now uses it.

Quoter does not use csv, ledatastream or hunkio.  It does not even
mention them in comments.  Have you  Quoter with some other
program?  Maybe fileio?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
04-02-08 12:45 AM


Re: Text to String literal conversion
In article <bpv4v3tit34795k4dv2burkmlu2jmlodv3@4ax.com>,
Roedy Green <see_website@mindprod.com.invalid> wrote:

> On Tue, 01 Apr 2008 12:46:39 -0400, "John B. Matthews"
> <nospam@nospam.com> wrote, quoted or indirectly quoted someone who
> said :
> 
>
> all those packages are available to download.  see
> http://mindprod.com/products.html
>
> But if you want to just cannibalise the code to create string
> literals, you need only one class in the bundle, namely
> ToJavaStringLiteral.java
>
> I have corrected the zip to include common13.  It did not used to need
> it. I converted it from AWT to Swing recently, which now uses it.
>
> Quoter does not use csv, ledatastream or hunkio.  It does not even
> mention them in comments.  Have you  Quoter with some other
> program?  Maybe fileio?

Thanks! The new source <http://mindprod.com/zips/quoter41.zip> builds
correctly; it contains the common13 directory, as imported in lines
83-85 of Quoter.java:

import com.mindprod.common13.CMPAboutJBox;
import com.mindprod.common13.HybridJ;
import com.mindprod.common13.JEButton;

I was confounding common11.CMPAboutBox & common13.CMPAboutJBox.

The others (csv, ledatastream or hunkio) are imported by unused classes.
For example, the class ImageInfo imports LEDataInputStream. Here are the
dependencies I found:

./com/mindprod/common11/ImageInfo.java:
import com.mindprod.ledatastream.LEDataInputStream;
./com/mindprod/entities/GenCheat.java:
import com.mindprod.csv.CSVReader;
./com/mindprod/entities/InsertFileEntities.java:
import com.mindprod.hunkio.HunkIO;
./com/mindprod/entities/StripFileEntities.java:
import com.mindprod.hunkio.HunkIO;

I was tracking down a problem with the applet occasionally losing
clipboard access in Safari; the application works fine.

John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Report this thread to moderator Post Follow-up to this message
Old Post
John B. Matthews
04-02-08 12:45 AM


Re: Text to String literal conversion
On Tue, 01 Apr 2008 20:30:21 -0400, "John B. Matthews"
<nospam@nospam.com> wrote, quoted or indirectly quoted someone who
said :

>./com/mindprod/common11/ImageInfo.java:
>    import com.mindprod.ledatastream.LEDataInputStream;
>./com/mindprod/entities/GenCheat.java:
>    import com.mindprod.csv.CSVReader;
>./com/mindprod/entities/InsertFileEntities.java:
>    import com.mindprod.hunkio.HunkIO;
>./com/mindprod/entities/StripFileEntities.java:
>    import com.mindprod.hunkio.HunkIO;

arrgh.  This means you need those packages just to do the build, even
though you never use those classes.

I have updated the ANT scripts to include ledatastream all the time
and csv and hunkio whenever entities is used. I will post within the
hour.

I wonder what sort of tool I could concoct to make sure my zips are
complete.

I found GenJar to make sure the jar is complete without excess fat.

I was thinking of writing a tool to make sure everything in the jar
was in the zip, but that is nowhere near sufficient.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
04-03-08 12:59 AM


Re: Text to String literal conversion
In article <fdn7v398fis3re1kceepbb3ppnes1rh59b@4ax.com>,
Roedy Green <see_website@mindprod.com.invalid> wrote:

> On Tue, 01 Apr 2008 20:30:21 -0400, "John B. Matthews"
> <nospam@nospam.com> wrote, quoted or indirectly quoted someone who
> said :
> 
>
> arrgh.  This means you need those packages just to do the build, even
> though you never use those classes.

This was not my experience using javac; after deleting all the class
files, the sources compiled with a few warnings and main executed
correctly. (Absent an E: drive, I can't use your build.xml file:-)

> I have updated the ANT scripts to include ledatastream all the time
> and csv and hunkio whenever entities is used. I will post within the
> hour.

The new zip compiles correctly. Although the web site still shows the
last update as "2008-02-09", the zip is indeed new.

> I wonder what sort of tool I could concoct to make sure my zips are
> complete.

Would ant's depend task be of use? With some restrictions, it appears to
determine dependencies based on references in the class files themselves:

<http://ant.apache.org/manual/OptionalTasks/depend.html>

[...]

John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

Report this thread to moderator Post Follow-up to this message
Old Post
John B. Matthews
04-03-08 12:59 AM


Re: Text to String literal conversion
On Wed, 02 Apr 2008 20:54:50 -0400, "John B. Matthews"
<nospam@nospam.com> wrote, quoted or indirectly quoted someone who
said :

>
>This was not my experience using javac; after deleting all the class
>files, the sources compiled with a few warnings and main executed
>correctly. (Absent an E: drive, I can't use your build.xml file:-)

use the simpler rebuild.xml file.  All you need do is modify this one
line.
<project name="common13" basedir="E:/" default="jar">
to change that E:

More detailed instructions willl appear on the website within the
hour.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Report this thread to moderator Post Follow-up to this message
Old Post
Roedy Green
04-03-08 04:05 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.