Chapter 49. Editing

Table of Contents

1. Overview
1.1. XML Editors
2. XML Schema
2.1. Types of Schema
3. Eclipse
3.1. XMLBuddy
4. GNU Emacs
4.1. XEmacs and PSGML-Mode
5. VIM
5.1. Marks and cursor position
6. Developing OpenLaszlo Applications with IntelliJ
6.1. Step 1: Define the lzx file type
6.2. Step 2: Declare the LZX DTD
6.3. Step 3: Develop your application
6.4. Step 4: Validate your application
6.5. Namespaces and OpenLaszlo Schema
6.6. Emacs note

1. Overview

LZX source files are text files. Any standard text editor such as Notepad (on Windows) or TextEdit (on MacOS) can be used to edit them.

1.1. XML Editors

LZX source files are a particular type of text file: they are XML files. An XML-aware editor such as Eclipse, BBEdit, or Emacs (with psgml-mode or nxml-mode) will provide additional editing facilities such as automatic indentation, syntax highlighting (coloring markup characters such as < and tag names such as <canvas> ), and well-formedness checking (letting you know when you have an unbalanced < or quotation mark).

To configure an XML-aware editor to edit LZX files, you must configure it to edit those files in XML mode. This is done by registering the lzx extension with the XML mode of the editor. How to do this depends on the exact editor; this chapter gives instructions for some popular editors.

2. XML Schema

An XML schema lists the tag and attribute names that can occur within an XML document. Many XML editing tools are schema-aware. These tools can associate an XML document with a schema file, and use the schema file for code completion (to complete a tag or attribute name, or suggest a list of valid attribute values) and validation (to indicate invalid tag or attribute names within the editor, so that you don't have to wait until you compile the file).

The method for associating an XML document with a schema file depends on the editor. This chapter gives instructions for some popular editors.

2.1. Types of Schema

There are three types of schemas in popular use: DTDs, XSD, and RNG (and its associated format RNC). The LZX schema is distributed in each of these formats. They can be found in the lps/tools directory.

A Document Type Definition (DTD) is the oldest type, and is recognized by the most editors. Unfortunately, the DTD format is very weak compared with the other schema description languages, and can't indicate certain contextual information such as that a <dataset> can contain arbitrary XML. A DTD should only be used in an editor that doesn't support the other formats.

W3 and OASIS are standards bodies. The W3 standard for describing XML documents is the XML Schema Definition (XSD). The OASIS standard is RELAXNG. RNG and RNC files are RELAXNG files that contain schema definitions. RNG files are in XML; RNC files are intended to be human-readable.

3. Eclipse

Eclipse is an open source development environment created by IBM and maintained by the Eclipse Foundation. You can use different Eclipse plug-ins to develop LZX programs.

3.1. XMLBuddy

There are four steps to using Eclipse with XMLBuddy

  • Step 1: Download Eclipse

    Download Eclipse from http://www.eclipse.org/downloads/index.php.

  • Step 2: Download XMLBuddy, an XML plugin for Eclipse

    Download XMLBuddy from http://www.xmlbuddy.com. Get the right version for your Eclipse.

    Installation is simple. After you unzip XMLBuddy, just drag the folder into your eclipse/plugins folder.

  • Step 3: Configure Eclipse to use XMLBuddy for *.lzx files

    In Eclipse:

    Figure 49.1. Configuring Eclipse for XMLBuddy

    Configuring Eclipse for XMLBuddy

    1. Go to menu: Window -> Preferences

    2. Twist down "Workbench", and select "File Associations"

    3. Click "Add" next to the "File Types" box and enter *.lzx

    4. Click "Add" next to the "Associated Editors" box and select XMLBuddy

  • Step 4: Configure XMLBuddy to use the LZX schema for canvas files

    1. Go to menu: Window -> Preferences

    2. Twist down XMLBuddy, and then XML, and then Validation

    3. Click on DTD

    4. Click "New..."

    5. In the "New Default DTD" dialog, enter:

      Root Name: canvas
          PUBLIC Id: -//Laszlo Systems, Inc.//DTD LZX 2003/05//EN
          SYSTEM Id: http://www.laszlosystems.com/lps/tools/lzx.dtd

  • Step 5: Configure XMLBuddy to use the LZX schema for library files

    Repeat steps (4-5), but with a Root Name of "library".

4. GNU Emacs

For GNUEmacs, an XML mode that understands RELAX-NG schemas at http://www.thaiopensource.com/download/ and a discussion group for this package at http://groups.yahoo.com/group/emacs-nxml-mode/.

4.1. XEmacs and PSGML-Mode

The lzx.el file tells emacs to recognize *.lzx files as XML files. With the DTD, this provides syntax-directed editing and validation of XML entities in LZX files.

If mmm-mode is installed, this file will also create an mmm submode to recognize the content of <method> and <script> tags as JavaScript, and direct mmm to use this submode for *.lzx files. This provides syntax coloring and intelligent indentation and navigation for JavaScript code within LZX files.

4.1.1. Installation

Copy lzx.el into a directory on the load-path, optionally byte-compile it (using M-x byte-compile-file), and place the following lines into your .emacs:

    (add-to-list 'load-path "path/to/mmm-mode-0.4.7")
    (load-library "mmm-mode")
    (require 'mmm-mode)
    (require 'lzx)

(If you don't wish to use mmm mode, only the last line is required.)

If you want mmm mode to be invoked automatically when you open a file, add the following line to your .emacs file:

    (mmm-add-find-file-hook)

5. VIM

Install the lzx.vim syntax file.

5.1. Marks and cursor position

Table 49.1. VIM Editing Keys

`[mark] will jump you to the appropriate column where the mark was set
'[mark] will only jump you to the beginning of the correct line.

Also, the mark "[" is set to the point where you last entered insert mode. One often has the problem of wanting to leave insert mode where editing began. you can now do this with the following remapping

    imap ^D <ESC>`[

Now, if you hit control-D in insert mode, you leave where tou entered the insert mode.

Even more useful is the ability to repeat a previous command without changing the cursor position. Since this is pretty much always the behavior you want, you can remap ".", or you could use some other combination.

    noremap . .`[

6. Developing OpenLaszlo Applications with IntelliJ

IntelliJ by JetBrains (www.jetbrains.com) is currently one of the most popular Java IDEs. IntelliJ also provides very good support for XML, and is therefore an excellent tool for developing OpenLaszlo applications. IntelliJ is particularly well suited for Java developers who want to manage the full life-cycle of an application using a single development environment.

This document describes the steps required to optimize the IntelliJ environment for the development of Laszlo applications.

6.1. Step 1: Define the lzx file type

  1. In the main menu, select Options>IDE Settings>File Types

  2. Select XML files in the Recognized File Types box

  3. Click Add for the Registered extensions box

  4. Type lzx as the new extension and click OK

  5. Click OK to close the IDE settings dialog box

Figure 49.2. Defining the LZX file type in IntelliJ

Defining the LZX file type in IntelliJ

6.2. Step 2: Declare the LZX DTD

  1. In IntelliJ, create a new file with the lzx extension, for exemple test.lzx

  2. <!DOCTYPE canvas SYSTEM "http://laszlosystems.com/lps-v1/tools/lzx.dtd">

    Type the following declaration as the first line of the file:
  3. The dtd URL appears in red. Click anywhere in the URL: a light bulb appears to the left of the line

  4. Figure 49.3. Declaring the LZX DTD in IntelliJ

    Declaring the LZX DTD in IntelliJ


    Click the light bulb and select Fetch External Resource from the popup window. The DTD is fetched into an IntelliJ system file:
  5. You are now ready to develop your OpenLaszlo application, leveraging the rich XML and code completion features provided by IntelliJ.

6.3. Step 3: Develop your application

  1. <!DOCTYPE canvas SYSTEM "http://laszlosystems.com/lps-v1/tools/lzx.dtd">

    Type the following line as the first line of your .lzx source files:
  2. Enjoy IntelliJ rich code completion features. For example:

    • Type the < character: a list of available LZX tags is displayed in a drop down list. Select the tag you want to insert.
    • After typing an LZX tag name, a list of LZX tag attributes is displayed in a drop down list. Select the attribute you want to insert.
    • You can press CTRL+SPACE to display a popup window with a list of LZX tag attributes

6.4. Step 4: Validate your application

  1. In the main menu select Tools>Validate

  2. If your XML document is not well formed or not valid (not compliant with the DTD), errors are reported in the Validate panel that opens at the bottom of the screen. Double-click an error in the list to position the cursor at the location of the error in the code.

6.5. Namespaces and OpenLaszlo Schema

OpenLaszlo applications can be written with a namespace:

  <canvas xlmns="http://www.laszlosystems.com/2003/05/lzx">...</canvas>

or without:

  <canvas>...</canvas>

If there is no namespace, the compiler defaults it to the LZX namespace (http://www.laszlosystems.com/2003/05/lzx").

As of OpenLaszlo release 3.1, the schema in lax.rnc has three deficiencies:

  • - It only works for sources that include the namespace declaration; e.g. it won't validate <canvas/> because it doesn't declare any elements in the empty namespace.

  • - It only knows about the foundation classes, not the components; e.g. it won't validate <canvas xmlns="..."><button/></canvas> because <button> isn't a foundation class.

  • - It isn't aware of tags that are defined in the application or its libraries; e.g. <canvas xmlns="..."><class name="myclass"/><myclass/></canvas>

The difficulty is that the <class> tag in LZX actually extends the schema by adding new tags and attributes. We have a hand-written basic schema (WEB-INF/lps/schema/lzx.rnc) which is used to start with, but then user and system component libraries can extend the schema, depending on the application.

<window> , for example, is defined in a source library in lps/components/lz/window.lzx, so it is not in the base schema. So it is difficult to use a static RNG schema, because it needs to be modified as the app defines new classes.

The LZX compiler can be asked to give you the RNG schema from a source file, so something might be able to be hooked up to keep regenerating the schema from the source file, although it would be hard because the sources are often in an inconsistent state as you develop your application, so the parser has to be very forgiving about badly formed XML.

We have worked around the first problem with a transform of that schema that strips out the namespace declaration. That's tools/lzx+libraries-nons.rnc. We have worked around the second problem with a script that makes a copy of the schema and adds the components. This is tools/lzx+libraries.rnc. The third problem can't be fixed without modifying nxml-mode to either add declarations for <class> declarations that it sees, or request the schema for an application from the compiler. (From the command line, the --schema option does this.) OpenLaszlo does neither of these.

The LZX tag set is defined in a relax.ng schema located in the lps/tools directory.

If you are going to use the schema,

  • Use tools/lzx+libraries.rnc for files that include the XML namespace declaration

  • Use tools/lzx+libraries-nons.rnc for files that don't include a namespace declaration.

6.6. Emacs note

nxml-mode can be set to choose between these schemes automatically by pointing it at the schema locator file in tools/nxml-schemas.xml:

  (setq rng-schema-locating-files
      (append
       (list (substitute-env-vars "$LPS_HOME/tools/nxml-schemas.xml"))
       rng-schema-locating-files))
[Note] Note

(SUBSTITUTE-ENV-VARS isn't a standard gnu emacs functions; it's just a hack that we use in the .init file that has a few environment variables hardwired.)