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