001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: SAXUtil.java,v 1.2 2006/09/29 12:32:11 drmlipp Exp $
021: *
022: * $Log: SAXUtil.java,v $
023: * Revision 1.2 2006/09/29 12:32:11 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.1 2003/06/30 20:05:12 drmlipp
027: * Initial import
028: *
029: * Revision 1.3 2003/06/27 08:51:46 lipp
030: * Fixed copyright/license information.
031: *
032: * Revision 1.2 2003/04/25 14:50:59 lipp
033: * Fixed javadoc errors and warnings.
034: *
035: * Revision 1.1 2002/08/30 11:00:10 huaiyang
036: * Help functions for SAXGenerator.
037: *
038: *
039: */
040: package de.danet.an.util.sax;
041:
042: import org.xml.sax.Attributes;
043: import org.xml.sax.ContentHandler;
044: import org.xml.sax.SAXException;
045: import org.xml.sax.helpers.AttributesImpl;
046:
047: /**
048: * The <code>SAXUtil</code> provides help functions for generating SAX
049: * events.
050: */
051: public class SAXUtil {
052:
053: //
054: // Helper methods
055: //
056:
057: /**
058: * Generate all notifications corresponding to an XML element
059: * with the given name and parameters that has the given
060: * text as content.
061: *
062: * @param contentHandler the content handler.
063: * @param elementName the unqualified element name.
064: * @param attrs optional attributes (may not be <code>null</code>).
065: * @param text the content.
066: * @throws SAXException if a general SAX error or warning occurs.
067: */
068: public static void textElement(ContentHandler contentHandler,
069: String elementName, Attributes attrs, String text)
070: throws SAXException {
071: contentHandler
072: .startElement("", elementName, elementName, attrs);
073: if (text != null) {
074: contentHandler.characters(text.toCharArray(), 0, text
075: .length());
076: }
077: contentHandler.endElement("", elementName, elementName);
078: }
079:
080: /**
081: * Generate a "dialog hint node", i.e. a hint how a node's
082: * attribute or a node's value is to be represented in a dialog.
083: *
084: * @param contentHandler main interface that most SAX applications
085: * implement. It will receive notification of the logic content of a
086: * document.
087: * @param attribute the name of the node's attribute which is
088: * attributed by the dialog hint. This parameter may be
089: * <code>null</code> if the dialog hint applies to the value of the node.
090: * @param type the type attribute of the dialog hint node.
091: * @param maxlength the maximum length of the attribute or node value.
092: * @throws SAXException if a general SAX error or warning occurs.
093: */
094: public static void generateDialogHint(
095: ContentHandler contentHandler, String attribute,
096: String type, int maxlength) throws SAXException {
097: AttributesImpl attrs = new AttributesImpl();
098: if (attribute != null) {
099: attrs.addAttribute("", "attribute", "attribute", "CDATA",
100: attribute);
101: }
102: attrs.addAttribute("", "type", "type", "CDATA", type);
103: attrs.addAttribute("", "maxlength", "maxlength", "CDATA",
104: Integer.toString(maxlength));
105: contentHandler.startElement("", "dialog-hint", "dialog-hint",
106: attrs);
107: contentHandler.endElement("", "dialog-hint", "dialog-hint");
108: }
109:
110: /**
111: * Generate a "dialog hint node", i.e. a hint how a node's
112: * attribute or a node's value is to be represented in a dialog.
113: *
114: * @param contentHandler main interface that most SAX applications
115: * implement. It will receive notification of the logic content of a
116: * document.
117: * @param attribute the name of the node's attribute which is
118: * attributed by the dialog hint. This parameter may be
119: * <code>null</code> if the dialog hint applies to the value of the node.
120: * @param type the type attribute of the dialog hint node.
121: * @throws SAXException if a general SAX error or warning occurs.
122: */
123: public static void generateDialogHint(
124: ContentHandler contentHandler, String attribute, String type)
125: throws SAXException {
126: AttributesImpl attrs = new AttributesImpl();
127: if (attribute != null) {
128: attrs.addAttribute("", "attribute", "attribute", "CDATA",
129: attribute);
130: }
131: attrs.addAttribute("", "type", "type", "CDATA", type);
132: contentHandler.startElement("", "dialog-hint", "dialog-hint",
133: attrs);
134: contentHandler.endElement("", "dialog-hint", "dialog-hint");
135: }
136:
137: /**
138: * Generate a element having text with a "dialog hint node",
139: * i.e. a hint how a node's attribute or a node's value is to be
140: * represented in a dialog.
141: *
142: * @param contentHandler main interface that most SAX applications
143: * implement. It will receive notification of the logic content of a
144: * document.
145: * @param elementName the name of the element to be generated.
146: * @param attrs the attributes of the element to be generated.
147: * @param text the text of the element to be generated.
148: * @param dialogHintType the type attribute of the dialog hint node.
149: * @param dialogHintLength the maximum length of the attribute or node
150: * value.
151: * @throws SAXException if a general SAX error or warning occurs.
152: */
153: public static void textElementWithDialogHint(
154: ContentHandler contentHandler, String elementName,
155: Attributes attrs, String text, String dialogHintType,
156: int dialogHintLength) throws SAXException {
157: contentHandler
158: .startElement("", elementName, elementName, attrs);
159: generateDialogHint(contentHandler, null, dialogHintType,
160: dialogHintLength);
161: if (text != null) {
162: contentHandler.characters(text.toCharArray(), 0, text
163: .length());
164: }
165: contentHandler.endElement("", elementName, elementName);
166: }
167:
168: /**
169: * Generate a element having text with a "dialog hint node",
170: * i.e. a hint how a node's attribute or a node's value is to be
171: * represented in a dialog.
172: *
173: * @param contentHandler main interface that most SAX applications
174: * implement. It will receive notification of the logic content of a
175: * document.
176: * @param elementName the name of the element to be generated.
177: * @param attrs the attributes of the element to be generated.
178: * @param text the text of the element to be generated.
179: * @param dialogHintType the type attribute of the dialog hint node.
180: * @throws SAXException if a general SAX error or warning occurs.
181: */
182: private static void textElementWithDialogHint(
183: ContentHandler contentHandler, String elementName,
184: Attributes attrs, String text, String dialogHintType)
185: throws SAXException {
186: contentHandler
187: .startElement("", elementName, elementName, attrs);
188: generateDialogHint(contentHandler, null, dialogHintType);
189: if (text != null) {
190: contentHandler.characters(text.toCharArray(), 0, text
191: .length());
192: }
193: contentHandler.endElement("", elementName, elementName);
194: }
195:
196: }
|