001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.beans;
019:
020: import java.beans.XMLDecoder;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Stack;
024: import java.util.Vector;
025: import org.apache.harmony.beans.internal.nls.Messages;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.helpers.DefaultHandler;
029:
030: public class Handler extends DefaultHandler {
031:
032: private Vector<Object> result;
033:
034: private Vector<Command> commands;
035:
036: private XMLDecoder decoder;
037:
038: private Map<String, Command> references;
039:
040: private Stack<Command> stack;
041:
042: private int tabCount;
043:
044: public Handler(XMLDecoder decoder, Vector<Object> result) {
045: this .decoder = decoder;
046: this .result = result;
047: this .commands = new Vector<Command>();
048: this .references = new HashMap<String, Command>();
049: this .stack = new Stack<Command>();
050: }
051:
052: // clear collections to prepare parsing document
053: @Override
054: public void startDocument() {
055: references.clear();
056: tabCount = 0;
057: }
058:
059: // create new command and put it on stack
060: @Override
061: public void startElement(String namespaceURI, String localeName,
062: String tagName, Attributes attrs) throws SAXException {
063: Command.printAttrs(tabCount, tagName, attrs);
064: Command cmd = tagName.equals("java") ? new Command(decoder, tagName, //$NON-NLS-1$
065: Command.parseAttrs(tagName, attrs))
066: : new Command(tagName, Command.parseAttrs(tagName,
067: attrs));
068: stack.push(cmd);
069: ++tabCount;
070: }
071:
072: // add data to command
073: @Override
074: public void characters(char[] text, int start, int length)
075: throws SAXException {
076: if (length > 0) {
077: String data = String.valueOf(text, start, length).replace(
078: '\n', ' ').replace('\t', ' ').trim();
079: if (data.length() > 0) {
080: Command.prn(tabCount, tabCount
081: + ">setting data=" + data //$NON-NLS-1$
082: + "<EOL>"); //$NON-NLS-1$
083: Command cmd = stack.peek();
084: cmd.setData(data);
085: }
086: }
087: }
088:
089: // pop command from stack and put it to one of collections
090: @Override
091: public void endElement(String namespaceURI, String localeName,
092: String tagName) throws SAXException {
093: Command cmd = stack.pop();
094: // cmd.setTabCount(tabCount);
095:
096: // find if command works in context
097: if (!stack.isEmpty()) {
098: Command ctx = stack.peek();
099: ctx.addChild(cmd);
100: }
101:
102: // upper level commands
103: if (stack.size() == 1 && cmd.isExecutable()) {
104: commands.add(cmd);
105: }
106:
107: // store reference to command
108: if (cmd.hasAttr("id")) { //$NON-NLS-1$
109: references.put(cmd.getAttr("id"), cmd); //$NON-NLS-1$
110: }
111:
112: try {
113: cmd.exec(references);
114: } catch (Exception e) {
115: SAXException e2 = new SAXException(e.getMessage());
116:
117: e2.initCause(e);
118: throw e2;
119: }
120:
121: if (--tabCount < 0) {
122: tabCount = 0;
123: }
124:
125: Command.prn(tabCount, tabCount + ">...<" + tagName + "> end"); //$NON-NLS-1$ //$NON-NLS-2$
126: }
127:
128: // iterate over deferred commands and execute them again
129: @Override
130: public void endDocument() throws SAXException {
131: for (int i = 0; i < commands.size(); ++i) {
132: Command cmd = commands.elementAt(i);
133: try {
134: cmd.backtrack(references);
135: } catch (Exception e) {
136: throw new SAXException(Messages.getString("beans.0B")); //$NON-NLS-1$
137: }
138: // if(!backtracked)
139: // throw new SAXException("Command " + cmd.getTagName() +
140: // " is unresolved on second run() call.");
141: }
142:
143: for (int i = 0; i < commands.size(); ++i) {
144: Command cmd = commands.elementAt(i);
145: result.add(cmd.getResultValue());
146: }
147: }
148: }
|