001: package com.rimfaxe.xml.xmlreader;
002:
003: import java.util.*;
004:
005: /** Streamlined thermopylae specific implementation of
006: * the org.xml.sax.helpers.NamespaceSupport class.
007:
008: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
009: This file is part of Sparta, an XML Parser, DOM, and XPath library.
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the <a href="doc-files/LGPL.txt">GNU
012: Lesser General Public License</a> as published by the Free Software
013: Foundation; either version 2.1 of the License, or (at your option)
014: any later version. This library is distributed in the hope that it
015: will be useful, but WITHOUT ANY WARRANTY; without even the implied
016: warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
017: PURPOSE. </small></blockquote>
018: @version $Date: 2003/01/27 23:30:59 $ $Revision: 1.2 $
019: @author Sergio Marti
020: **/
021:
022: public class NamespaceSupport {
023:
024: static final String XMLNS = "http://www.w3.org/XML/1998/namespace";
025: static final String XMLPREF = "xml";
026:
027: private final Stack context_ = new Stack();
028: private Map currContext_ = null;
029: private final Stack oldContext_ = new Stack();
030:
031: private String defaultNS_ = "";
032:
033: /* Cache last prefix for speed */
034: private String currPrefix_ = null;
035: private String currFullPrefix_ = null;
036: private String currNS_ = null;
037: private int depth_ = 0;
038:
039: public NamespaceSupport() {
040: }
041:
042: public void pushContext() {
043: if (currContext_ != null)
044: context_.push(currContext_);
045: if (!oldContext_.isEmpty()) {
046: currContext_ = (Map) oldContext_.pop();
047: currContext_.clear();
048: } else
049: currContext_ = new HashMap();
050:
051: depth_++;
052: }
053:
054: Iterator popContext() {
055: depth_--;
056: if (depth_ < 0)
057: currPrefix_ = null;
058:
059: oldContext_.push(currContext_);
060: Iterator keySetIterator = currContext_.keySet().iterator();
061: if (!context_.isEmpty())
062: currContext_ = (Map) context_.pop();
063: else
064: currContext_ = null;
065: return keySetIterator;
066: }
067:
068: void declarePrefix(String prefix, String uri) {
069: if (prefix.equals("xml") || prefix.equals("xmlns"))
070: return;
071: if (prefix.equals("")) {
072: defaultNS_ = uri;
073: return;
074: }
075: currContext_.put(prefix, uri);
076: }
077:
078: String[] processName(String fullName, String[] result,
079: boolean attribute) throws ParseException {
080:
081: if (currPrefix_ != null && fullName.startsWith(currFullPrefix_)) {
082: result[0] = currNS_;
083: result[1] = fullName.substring(currFullPrefix_.length());
084: result[2] = fullName;
085: return result;
086: }
087:
088: int a = fullName.indexOf(':');
089: if (a > 0) {
090: String prefix = fullName.substring(0, a);
091:
092: System.out.println("NameSpace " + prefix);
093:
094: if (prefix.equals("xml") || prefix.equals("xmlns")) {
095: return null;
096: }
097:
098: result[1] = fullName.substring(a + 1);
099: result[2] = fullName;
100:
101: result[0] = (String) currContext_.get(prefix);
102:
103: if (result[0] == null) {
104: for (int i = context_.size() - 1; i >= 0; i--) {
105: Map ht = (Map) (context_.elementAt(i));
106: result[0] = (String) (ht.get(prefix));
107: if (result[0] != null)
108: break;
109: }
110: }
111:
112: if (result[0] == null)
113: throw new ParseException("Error processing tag "
114: + fullName + ". No namespace mapping found.");
115:
116: depth_ = 0;
117: currNS_ = result[0];
118: currPrefix_ = prefix;
119: currFullPrefix_ = prefix + ":";
120: } else {
121: if (attribute)
122: result[0] = "";
123: else
124: result[0] = defaultNS_;
125: result[1] = result[2] = fullName;
126: }
127:
128: return result;
129: }
130: }
131:
132: // $Log: NamespaceSupport.java,v $
133: // Revision 1.2 2003/01/27 23:30:59 yuhongx
134: // Replaced Hashtable with HashMap.
135: //
136: // Revision 1.1.1.1 2002/08/19 05:04:16 eobrain
137: // import from HP Labs internal CVS
138: //
139: // Revision 1.5 2002/08/19 00:39:45 eob
140: // Tweak javadoc comment.
141: //
142: // Revision 1.4 2002/08/18 05:45:59 eob
143: // Add copyright and other formatting and commenting in preparation for
144: // release to SourceForge.
145: //
146: // Revision 1.3 2002/08/17 00:54:14 sermarti
147: //
148: // Revision 1.2 2002/08/15 23:40:23 sermarti
149: //
150: // Revision 1.1 2002/08/09 22:36:49 sermarti
|