0

I need to format a static bit of headings that were coded within a java file as follows

public String getShortDescription() {

        if (code != null) {
            if (isOption1()) {
                return "Option 1 heading";          
            } else if (isOption2()) {
                return "Option2 heading";   
            } else if (isOption3()) {
                return "Option3 heading";   
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

Is there a quick way to add css classes to the heading part i.e return "Option <span class="heading">1 heading</span>" ?

user1400854
  • 297
  • 1
  • 5
  • 14
  • I would rethink this - bad idea. – duffymo Jul 24 '13 at 11:44
  • I agree and understand why you are saying this. I just ask if there was a way to quickly achieve this without the need to re code this existing java file because I am not a java developer. – user1400854 Jul 24 '13 at 12:35
  • If you must, create a custom class that includes the description and CSS class and return that instead of String. – duffymo Jul 24 '13 at 12:51

1 Answers1

0

Normally it's bad to mix your data and display markup within your Java code. However, if there is no other workaround, then you could do the following:

public static String wrapText(String text, String cssClass) {
    if (text == null) {
        throw new IllegalArgumentException("Text to wrap is null");
    }
    if (cssClass == null) {
        cssClass = "";
    }
    return "<span class=\"" + cssClass + "\">" + text + "</span>";
}

public String getShortDescription() {
    if (isOption1()) {
        return "Option " + wrapText("1 heading","heading");
    }
    ...
}
user1171848
  • 276
  • 3
  • 10
  • thanks for your response. I have applied the above but it just spits out the html, doesn't apply the formatting – user1400854 Jul 24 '13 at 14:49
  • What do you mean by "doesn't apply the formatting"? The Java method should be returning html with the included and css class. If you need to apply styles to the "heading" class, you will need a separate stylesheet in your html document. – user1171848 Jul 24 '13 at 15:10
  • What I mean is, the browser renders Option Heading 1. I already have css specified for the class="heading" – user1400854 Jul 24 '13 at 15:24
  • My best guess is that some other code is escaping the html content. That's the only reason I can think why your browser wouldn't be rendering the html content. You could view source to see if the < and > tags are being replaced by < and >. – user1171848 Jul 24 '13 at 18:23
  • I have used this escapeXml="false" and it worked. Great, thanks! – user1400854 Jul 24 '13 at 18:36