The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Embedded Text via XML Annotations

Posted by evanx on April 16, 2007 at 05:08 AM | Comments (8)

We hear talk of introducing "native XML" into Java7. I'm not sure what form that might take?

Embedding HTML and SQL (and JavaScript et al) "text" into Java is cumbersome eg. using multi-line concatenated string literals. And this doesn't lend itself towards leveraging IDEs eg. for auto-completion, error-detection and syntax-highlighting. What might work for me is allowing "annotations" that are correctly formed multi-line XML text, as a mechanism to embed "structured" text into Java code in a way that is easy to type (and cut and paste) and easy to read (especially if the IDE does some syntax highlighting).

Then i could use these annotations for HTML snippets as follows.

public class HTMLBuilder {
   ...
   <html xmlns="html" id="sectionHeadingDiv">
      <div class="sectionHeadingStyle" />
   </html>
   public String getSectionHeadingHTML(String heading) {
      HTML snippet = getAnnotationHTML("sectionHeadingDiv");
      snippet.getChildElement("div").setText(heading);
      return snippet.toString();
   }
}

Also for SQL...

public class PersonDao {
   ...
   <sql xmlns="sql" id="selectPersonByPersonId">
      <query>
         select * from person 
         where person_id = :personId
      </query>
   </sql>
   public Person getPerson(String personId) throws SQLException {
      SQL query = getAnnotationSQL("selectPersonByPersonId");
      query.setParameter("personId", personId);
      return (Person) query.fetch(Person.class);
   }
}

and also other any other cases where i might wanna embed multi-line text into a Java class, eg. maybe some dynamic scripting stuff.

The compiler might just treat these as String annotations, and an Ant task might verify the XML at compile time, and IDE's could hopefully join in the fun as well (with auto-completion, highlighting, verification). Waddaya think?


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Interesting idea. With XML, SQL and the ability to embed JavaScript, JRuby etc. I think a generic string literal block semantic would be helpful. I did some hacking on the compiler and have a multi line literal implementation. It was straight forward. Now I used backquotes, which I hear is considered bad form, but it worked well. I'd just like to see something generic instead of just for XML.

    Posted by: aberrant on April 16, 2007 at 08:50 AM

  • I agree that something generic would be more useful than XML per se. Having said that, XML could be used to wrap everything else?

    Posted by: evanx on April 16, 2007 at 10:21 AM

  • If I have to start reading developers code with sql, xml and javascript intertwined like a tomatoe vine then I am going to find that Google guys post about the next big language... :-)

    Posted by: aronsmith on April 17, 2007 at 07:03 PM


  • :) java applications, especially web ones, are already mash of HTML, CSS, XML, Javascript, SQL innit. But typically it all sits in other files rather than embedded Strings in java classes. But in PHP and the like, the HTML and PHP often sits side by side in the same source file. I'm not sure if this is generally a good thing (no separation of concerns), but i'm sure it is often quite convenient.

    I find myself embedding HTML/CSS snippets in (multiline) Java Strings, and it's really messy with the double quotes and plus sign on each line, and escaping quotes. I would like to type freely. Similarly with SQL queries. Also for embedding styled HTML text for Swing components, embedded help and what-not, although more correctly this should be in translatable resource files. But sometimes for pet projects, rapid and convenient prototyping is the most important concern.

    Rather than embedded Strings, a better solution is to put all such stuff in an XML resource file, and process that with JDOM or the like, extracting snippets. But still, i think the option of embedded XML in Java would be handy for some people, some of the time!? ;)


    Posted by: evanx on April 18, 2007 at 12:27 AM

  • If we talk about language changes for supporting multiline
    strings we can just use different delimiters instead of quotes for such
    strings. E.g. it can be '#' symbol. In this case the code looked like:

    public Person getPerson(String personId) throws SQLException {
    SQL query = getAnnotationSQL(#
    select * from person
    where person_id = :personId#)

    query.setParameter("personId", personId);
    return (Person) query.fetch(Person.class);
    }


    So, why do we need XML here? We don't!

    If we go further and continue with Java language changes
    we could get even more elegant constructions. We can introduce
    and "sql" keyword (like "for" keyword) which could contain a plain SQL
    in "{ }" after. And this SQL could be checked by compiler and mixed
    with Java variables like it is shown below:

    public Person getPerson(String personId) throws SQLException {
    SQL query = sql {
    select * from person
    where person_id = {personId}
    }
    return (Person) query.fetch(Person.class);
    }

    Such construction even eliminates the need of calling: query.setParameter("personId", personId);
    Isn't that better? :)
    I suppose that embedding of SQL into Java would be much more
    helpful then embedding XML. I'd stayed XML for web developers in
    their JSPs HTMLs etc :) but not in Java.

    Posted by: maxz1 on April 18, 2007 at 10:21 AM

  • I am not particularly crazy about the idea of embedding XML into the Java language. I am however a little bit intrigued about the idea of Embedded Domain Specific Languages (EDSL), and how they can be used in this particular scenarios. So if embedding XML is absolutely a must, then EDSL the best way to go about it. Looking at the example posted by maxz makes me wonder if BGGA closures control abstraction can help here as well.

    Posted by: mikeazzi on April 18, 2007 at 11:40 AM


  • curly braces do look nice. I don't like the necessity of string references in my examples either, or that the embedded text are static annotations. Using curly brackets to embed multi-line text appeals to me.

    One advantage of having an XML wrapper is that content-type can be specified, and gleaned by IDE, which can then assist with appropriate editor (with auto-completion, error-detection and syntax-highlighting).

    Posted by: evanx on April 18, 2007 at 02:06 PM

  • i think i'll solve this by having an XML resource file as follows,
    and reading in the XML snippets into Java strings,
    where the "id" attribute specifies the variable name.


    <div id="sectionHeading" class='sectionStyle'>%s</div>

    and for SQL and other content

    <sql id="selectPersonSql">
    select ...
    </sql>

    where these snippets, resources et al, are read as follows

    XMLResource xml = new XMLResource(getClass());
    String sectionHeading = xml.getElementText("sectionHeading");
    String query = xml.getText("selectPersonSql");

    where XMLResource might just use jDOM to parse the resource file.

    A further option might be to inject these resources into Java class eg. according to annotations, from the XML file, using reflection.

    Besides strings, it would be interesting to support other types too, eg. integers (eg. for component widths and what-not), colors, fonts, and other such resources, ie. so the XML file might be used as alternative to .properties resource file.

    Posted by: evanx on April 21, 2007 at 05:31 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds