5

What is the best way to handle large string constants in Java?

Imagine that I have a test fixture for SOAP and I want to send the following string:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
       <QuoteRequest xmlns="https://www.bcponline.co.uk">
            <header>
                <SourceCode>BN325</SourceCode>
                <MessageSource>B2B3</MessageSource>
                <Profile>B08A</Profile>
                <Password>AP3U86V</Password>
                <OperatorID>flightline</OperatorID>
                <ShowWebOptions>0</ShowWebOptions>
            </header>
            <serviceSelection>
                <ServiceProviderCode></ServiceProviderCode>
                <ProductCode>CarParking</ProductCode>
                <IATACode>lgw</IATACode>
            </serviceSelection>
            <quoteDetails>
                <DepartureDate>21-Jun-2005</DepartureDate>
                <DepartureTime>07:00</DepartureTime>
                <ReturnDate>28-Jun-2005</ReturnDate>
                <ReturnTime>07:00</ReturnTime>
                <QuoteReference></QuoteReference>
                <NoPax>1</NoPax>
            </quoteDetails>
            <sPostCode></sPostCode>
        </QuoteRequest>
    </soap:Body>
</soap:Envelope>

I'd rather not put quotes and pluses around every line. If I put it in a file it's extra code and it would be somewhat hard to put several strings in the same file. XML has problems escaping text (I have to use CDATA ugliness). Is there an easier way?

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
User1
  • 39,458
  • 69
  • 187
  • 265
  • lame suggestion: how about encoding the line breaks as `\n`s? – Mark Elliot Jan 25 '11 at 21:43
  • 1
    You can put it in properties file. Still have to write code, but not that much and you can put more than one string in it – jny Jan 25 '11 at 21:45
  • Related: http://stackoverflow.com/questions/782810/working-with-large-text-snippets-in-java-source – Rob Hruska Jan 25 '11 at 22:13
  • [rant]I can't believe Java still hasn't fixed this. It's one of the main reasons that Java sucks for so many applications. Why is it so hard for them to get this?[/rant] – interstar Feb 08 '13 at 16:34

2 Answers2

3

If the strings are unrelated, you could put them in separate files even if it's a lot of files (what is the problem with that?).

If you insist on one file, you could come up with a unique delimiter, but you would be paying a price when attempting to randomly access a specific entry.

Data files should almost always be externalized (likely in a separate directory) and read when needed, rather than hardcoded into the code. It's cleaner, reduces code size, reduces need for compilation, and allows you to use the same data file for multiple test. Most test fixtures as well as build and integration tools support external files.

Or, you could write code or a builder that builds SOAP from arguments, making this all a lot more concise (if you're willing to pay the runtime cost). (Correction: I see you changed your sample, this would be nasty to auto-generate).

Uri
  • 88,451
  • 51
  • 221
  • 321
  • *If you insist on one file, you could come up with a unique delimiter* - I'm using this with an empty line as delimiter, it's very easy to read. *but you would be paying a price when attempting to randomly access a specific entry.* - This normally never happens, he was thinking about putting it in program, so it's surely not in order of GB in size, so he can keep it all in memory. – maaartinus Jan 25 '11 at 21:51
  • @maaartinus: I would be very careful about using an empty line as a delimiter in a format that allows empty lines as whitespace (is that correct?). It takes one merge error or a careless cut-paste to mess things up. – Uri Jan 25 '11 at 22:02
  • Sure, but I am careful: Actually, in my format, the first line of a "paragraph" is it's name. I use an enum for all the names. In case I mess it up, there's a missing name or something like that. Any missing, duplicated, or unknown name raises an exception during load. – maaartinus Jan 25 '11 at 22:27
  • Where would you prefer to put such file? In the same folder as the class folder? – birgersp Aug 06 '14 at 10:21
-1

What about using a StringBuilder? You can always use StringBuilder.toString() to get the String...

chahuistle
  • 2,627
  • 22
  • 30
  • That sounds even harder than using quotes and pluses. At least it will be faster for the computer to process. – User1 Jan 25 '11 at 21:55
  • 1
    @User1 - it wouldn't even be faster. Little pieces of the string would be in your class file, which would have to get assembled at runtime, rather than just having the entire pre-assembled string compiled in. – Mike Daniels Jan 25 '11 at 21:59