Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Personalizing Data Display With XML: Portal-to-Go , 13 of 16

## Portal-to-Go: Java Transformers

You can create Java Transformers using either of the following two interfaces:

- Document Object Model (DOM) interface, which manipulates the tree-based document object model

- Simple API for XML (SAX) interface, which interacts directly with events in the parsing process.

These two interfaces are illustrated in Figure 7-7.

#### Figure 7-7 The DOM and SAX Interfaces

Portal-to-Go includes a Java Transformer that converts Simple Result documents to plain text. The Transformer does not create markup tags in the resulting document, but it does apply simple text formatting elements, such as line breaks and tabs.

### Portal-to-Go Java Transformer Example 1: Converting Simple Result Elements to Another Format

Though simple, this example shows how you can convert Simple Result elements into another format.

    package oracle.panama.core.xform;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Element;
    import oracle.panama.PanamaException;
    import oracle.panama.core.LogicalDevice;
    import oracle.panama.core.Service;
    import oracle.panama.Arguments;
    import oracle.panama.core.parm.PanamaRequest;
    import oracle.panama.core.parm.AbstractRequest;

    public class SimpleResultToText implements Transform {
        public SimpleResultToText() {}

        private String format(Element el) {
            if (el == null) {
                return "";
            }
            StringBuffer buf = new StringBuffer();
            String name = el.getTagName();
            if (name != null && name.length() > 0) {
                buf.append(name);
                buf.append(": ");
            }
            buf.append(el.getNodeValue());
            return buf.toString();
        }

        public String transform(Element element, LogicalDevice device)
                                       throws PanamaException {
            PanamaRequest req = AbstractRequest.getCurrentRequest();
            Service service = req.getService();
            StringBuffer buf =
              new StringBuffer((service == null) ? "" : service.getName());
            NodeList list = element.getElementsByTagName("*");
            Element el;
            String tag;
            boolean newRow = false;
            for (int i = 0; i
                el = (Element)list.item(i);
                tag = el.getTagName();
                if (tag.equals("SimpleRow")) {
                    newRow = true;
                    buf.append("\n");
                } else if (tag.equals("SimpleCol")) {
                    if (!newRow) {
                        buf.append("\t");
                    } else {
                        newRow = false;
                    }
                    buf.append(format(el));
                } else if (tag.equals("SimpleText") ||
                           tag.equals("SimpleForm") ||
                           tag.equals("SimpleMenu")) {
                    newRow = true;
                    buf.append("\n");
                } else if (tag.equals("SimpleTextItem") ||
                           tag.equals("SimpleFormItem") ||
                           tag.equals("SimpleMenuItem")) {
                    if (!newRow) {
                        buf.append("\n");
                    } else {
                        newRow = false;
                    }
                    buf.append(format(el));
                }
            }
            return buf.toString();
        }
    }

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index