$ relaxer -java -useJAXP hello.rlx
generate java classes
use JAXP(Java's standard extension API for XML Parser) to build classes from physical resource like URI.
import java.io.*; import java.net.URL; import java.net.MalformedURLException; import javax.xml.parsers.*; import org.xml.sax.SAXException; import org.w3c.dom.*; /** * <b>Greeting</b> is generated by Relaxer based on hello.rlx. * This class is derived from: * * <!-- for programmer * <elementRule role="greeting"> * <sequence> * <element name="title" type="string"/> * <element name="message" type="string"/> * </sequence> * </elementRule> * * <tag name="greeting"> * <attribute name="seqNo" required="true" type="int"/> * <attribute name="to" required="true" type="string"/> * <attribute name="from" required="true" type="string"/> * </tag> * --> * <!-- for javadoc --> * <pre> <elementRule role="greeting"> * <sequence> * <element name="title" type="string"/> * <element name="message" type="string"/> * </sequence> * </elementRule> * <tag name="greeting"> * <attribute name="seqNo" required="true" type="int"/> * <attribute name="to" required="true" type="string"/> * <attribute name="from" required="true" type="string"/> * </tag> * </pre> * * @version hello.rlx (Tue Jul 25 09:14:52 JST 2000) * @author Relaxer 0.10 (by ASAMI@Yokohama) */ public class Greeting implements java.io.Serializable { private int seqNo; private String to; private String from; private String title; private String message; /** * Creates a <code>Greeting</code>. * */ public Greeting() { } /** * Creates a <code>Greeting</code> by the Stack <code>stack</code> * that contains Elements. * This constructor is supposed to be used internally * by the Relaxer system. * * @param stack */ public Greeting(RStack stack) { setup(stack); } /** * Creates a <code>Greeting</code> by the Document <code>doc</code>. * * @param doc */ public Greeting(Document doc) { setup(doc.getDocumentElement()); } /** * Creates a <code>Greeting</code> by the Element <code>element</code>. * * @param element */ public Greeting(Element element) { setup(element); } /** * Creates a <code>Greeting</code> by the File <code>file</code>. * * @param file * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public Greeting(File file) throws IOException, SAXException, ParserConfigurationException { setup(file); } /** * Creates a <code>Greeting</code> * by the String representation of URI <code>uri</code>. * * @param uri * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public Greeting(String uri) throws IOException, SAXException, ParserConfigurationException { setup(uri); } /** * Creates a <code>Greeting</code> by the URL <code>url</code>. * * @param url * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public Greeting(URL url) throws IOException, SAXException, ParserConfigurationException { setup(url); } /** * Creates a <code>Greeting</code> by the InputStream <code>in</code>. * * @param in * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public Greeting(InputStream in) throws IOException, SAXException, ParserConfigurationException { setup(in); } /** * Initializes the <code>Greeting</code> by the Document <code>doc</code>. * * @param doc */ public void setup(Document doc) { setup(doc.getDocumentElement()); } /** * Initializes the <code>Greeting</code> by the Element <code>element</code>. * * @param element */ public void setup(Element element) { init(element); } /** * Initializes the <code>Greeting</code> by the Stack <code>stack</code> * that contains Elements. * This constructor is supposed to be used internally * by the Relaxer system. * * @param stack */ public void setup(RStack stack) { setup(stack.popElement()); } /** * @param element */ private void init(Element element) { RStack stack = new RStack(element); seqNo = URelaxer.getAttributePropertyAsInt(element, "seqNo"); to = URelaxer.getAttributePropertyAsString(element, "to"); from = URelaxer.getAttributePropertyAsString(element, "from"); title = URelaxer.getElementPropertyAsString(stack.popElement()); message = URelaxer.getElementPropertyAsString(stack.popElement()); } /** * Creates a DOM representation of the object. * Result is appended to the Node <code>parent</code>. * * @param parent */ public void makeElement(Node parent) { Document doc; if (parent instanceof Document) { doc = (Document)parent; } else { doc = parent.getOwnerDocument(); } Element element = doc.createElement("greeting"); int size; URelaxer.setAttributePropertyByInt(element, "seqNo", seqNo); URelaxer.setAttributePropertyByString(element, "to", to); URelaxer.setAttributePropertyByString(element, "from", from); URelaxer.setElementPropertyByString(element, "title", title); URelaxer.setElementPropertyByString(element, "message", message); parent.appendChild(element); } /** * Initializes the <code>Greeting</code> by the File <code>file</code>. * * @param file * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public void setup(File file) throws IOException, SAXException, ParserConfigurationException { setup(file.toURL()); } /** * Initializes the <code>Greeting</code> * by the String representation of URI <code>uri</code>. * * @param uri * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public void setup(String uri) throws IOException, SAXException, ParserConfigurationException { setup(UJAXP.getDocument(uri)); } /** * Initializes the <code>Greeting</code> by the URL <code>url</code>. * * @param url * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public void setup(URL url) throws IOException, SAXException, ParserConfigurationException { setup(UJAXP.getDocument(url)); } /** * Initializes the <code>Greeting</code> by the InputStream <code>in</code>. * * @param in * @exception IOException * @exception SAXException * @exception ParserConfigurationException */ public void setup(InputStream in) throws IOException, SAXException, ParserConfigurationException { setup(UJAXP.getDocument(in)); } /** * Creates a DOM document representation of the object. * * @exception ParserConfigurationException * @return Document */ public Document makeDocument() throws ParserConfigurationException { Document doc = UJAXP.makeDocument(); makeElement(doc); return (doc); } /** * Gets the int property <b>seqNo</b>. * * @return int */ public final int getSeqNo() { return (seqNo); } /** * Sets the int property <b>seqNo</b>. * * @param seqNo */ public final void setSeqNo(int seqNo) { this.seqNo = seqNo; } /** * Gets the String property <b>to</b>. * * @return String */ public final String getTo() { return (to); } /** * Sets the String property <b>to</b>. * * @param to */ public final void setTo(String to) { this.to = to; } /** * Gets the String property <b>from</b>. * * @return String */ public final String getFrom() { return (from); } /** * Sets the String property <b>from</b>. * * @param from */ public final void setFrom(String from) { this.from = from; } /** * Gets the String property <b>title</b>. * * @return String */ public final String getTitle() { return (title); } /** * Sets the String property <b>title</b>. * * @param title */ public final void setTitle(String title) { this.title = title; } /** * Gets the String property <b>message</b>. * * @return String */ public final String getMessage() { return (message); } /** * Sets the String property <b>message</b>. * * @param message */ public final void setMessage(String message) { this.message = message; } /** * Tests if a Element <code>element</code> is valid * for the <code>Greeting</code>. * * @param element * @return boolean */ public static boolean isMatch(Element element) { String tagName = element.getTagName(); if (!"greeting".equals(tagName)) { return (false); } RStack target = new RStack(element); Element child; child = target.popElement(); if (child == null) { return (false); } if (!"title".equals(child.getTagName())) { return (false); } child = target.popElement(); if (child == null) { return (false); } if (!"message".equals(child.getTagName())) { return (false); } if (!target.isEmptyElement()) { return (false); } return (true); } /** * Tests if elements contained in a Stack <code>stack</code> * is valid for the <code>Greeting</code>. * This mehtod is supposed to be used internally * by the Relaxer system. * * @param stack * @return boolean */ public static boolean isMatch(RStack stack) { Element element = stack.peekElement(); if (element == null) { return (false); } return (isMatch(element)); } /** * Tests if elements contained in a Stack <code>stack</code> * is valid for the <code>Greeting</code>. * This method consumes the stack contents during matching operation. * This mehtod is supposed to be used internally * by the Relaxer system. * * @param stack * @return boolean */ public static boolean isMatchHungry(RStack stack) { Element element = stack.peekElement(); if (element == null) { return (false); } if (isMatch(element)) { stack.popElement(); return (true); } else { return (false); } } }