Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0d9ad1a439d424429bf49f911468f10f9672110c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.core.internal.catalog;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.wst.common.uriresolver.internal.URI;
import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;
import org.eclipse.wst.xml.core.internal.Logger;
import org.eclipse.wst.xml.core.internal.XMLCoreMessages;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalogElement;
import org.eclipse.wst.xml.core.internal.catalog.provisional.ICatalogEntry;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 
 * 
 */
public final class CatalogReader
{
  public static void read(Catalog xmlCatalog, InputStream input) throws IOException
  {
    try
    {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setNamespaceAware(true);
      SAXParser parser = factory.newSAXParser();
      parser.parse(new InputSource(input), new CatalogContentHandler(xmlCatalog));
    }
    catch (ParserConfigurationException e)
    {
      Logger.logException(e);
    }
    catch (SAXException e)
    {
      Logger.logException(e);
    }
  }
  protected static class CatalogContentHandler extends DefaultHandler
  {
    protected Catalog catalog;
    protected Stack baseURIStack = new Stack();

    public CatalogContentHandler(Catalog xmlCatalog)
    {
      this.catalog = xmlCatalog;
      String base = xmlCatalog.getBase();
      if(base == null || base == "") {  //$NON-NLS-1$
    	  base = xmlCatalog.getLocation();
      }
      baseURIStack.push(base);
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
      // set base
      String base = attributes.getValue(OASISCatalogConstants.ATTR_BASE); //$NON-NLS-1$
      if (base != null && !base.equals("")) //$NON-NLS-1$
      {
          baseURIStack.push(base);
      } else {
    	  baseURIStack.push(baseURIStack.peek());
      }

	  // processing for backward compatability start
	  if (localName.equals(CompatabilityConstants.TAG_USER_ENTRY))
      {
		int type = ICatalogEntry.ENTRY_TYPE_PUBLIC;
        String typeName = attributes.getValue("", CompatabilityConstants.ATT_TYPE); //$NON-NLS-1$
        if (typeName != null)
        {
          if (typeName.compareToIgnoreCase("SYSTEM") == 0) //$NON-NLS-1$
          {
            type = ICatalogEntry.ENTRY_TYPE_SYSTEM;
          }
        }
		  ICatalogElement catalogElement = catalog.createCatalogElement(type);
	      if (catalogElement instanceof CatalogEntry)
	      {
	        CatalogEntry catalogEntry = (CatalogEntry) catalogElement;
		    String key = attributes.getValue("", CompatabilityConstants.ATT_ID);   		     //$NON-NLS-1$
	        catalogEntry.setKey(key);
	        String entryUri = attributes.getValue("", CompatabilityConstants.ATT_URI);    //$NON-NLS-1$
	        
	        // For relative URIs, try to resolve them using the corresponding base URI.
	        if(URI.createURI(entryUri).isRelative()) {
	        	entryUri = URI.resolveRelativeURI(entryUri, baseURIStack.peek().toString());
	        }

	        catalogEntry.setURI(URIHelper.ensureURIProtocolFormat(entryUri));  
	        String webURL = attributes.getValue("", CompatabilityConstants.ATT_WEB_URL); //$NON-NLS-1$
			if (webURL != null)
			{
				catalogEntry.setAttributeValue(
						ICatalogEntry.ATTR_WEB_URL, webURL);
			}
	      }
		  catalog.addCatalogElement(catalogElement);
		  return;
      }
	  //  processing for backward compatability start
      
	  int type = ICatalogEntry.ENTRY_TYPE_PUBLIC;
	  String key = null;
	 //dw String catalogId = attributes.getValue("", OASISCatalogConstants.ATTR_ID);
      if (OASISCatalogConstants.TAG_PUBLIC.equals(localName))
      {
        key = attributes.getValue("", OASISCatalogConstants.ATTR_PUBLIC_ID); //$NON-NLS-1$
      }
      else if (OASISCatalogConstants.TAG_SYSTEM.equals(localName))
      {
        key = attributes.getValue("", OASISCatalogConstants.ATTR_SYSTEM_ID); //$NON-NLS-1$
        type = ICatalogEntry.ENTRY_TYPE_SYSTEM;
      }
      else if (OASISCatalogConstants.TAG_URI.equals(localName))
      {
        key = attributes.getValue("", OASISCatalogConstants.ATTR_NAME); //$NON-NLS-1$
        type = ICatalogEntry.ENTRY_TYPE_URI;
      }
      else if (OASISCatalogConstants.TAG_NEXT_CATALOG.equals(localName))
      {
        String nextCatalogId = attributes.getValue("", OASISCatalogConstants.ATTR_ID); //$NON-NLS-1$

        String location = attributes.getValue("", OASISCatalogConstants.ATTR_CATALOG);    //$NON-NLS-1$
        NextCatalog delegate = new NextCatalog();
        delegate.setCatalogLocation(location);  
        delegate.setId(nextCatalogId);
        catalog.addCatalogElement(delegate);
        return;
      }
      else{
    	  // do not handle other entries
    	  return;
      }
      if (key == null || key.equals("")) //$NON-NLS-1$
      {
        Logger.log(Logger.ERROR, XMLCoreMessages.Catalog_entry_key_not_set);
        return;
      }
      String entryURI = attributes.getValue("", OASISCatalogConstants.ATTR_URI); // mandatory //$NON-NLS-1$
      if (entryURI == null || entryURI.equals("")) //$NON-NLS-1$
      {
        Logger.log(Logger.ERROR, XMLCoreMessages.Catalog_entry_uri_not_set);
        return;
      }
 
      ICatalogElement catalogElement = catalog.createCatalogElement(type);
      if (catalogElement instanceof CatalogEntry)
      {
        CatalogEntry catalogEntry = (CatalogEntry) catalogElement;
        catalogEntry.setKey(key); 

        //      For relative URIs, try to resolve them using the corresponding base URI.
        if(URI.createURI(entryURI).isRelative()) {
        	entryURI = URI.resolveRelativeURI(entryURI, baseURIStack.peek().toString());
        } 

        catalogEntry.setURI(URIHelper.ensureURIProtocolFormat(entryURI));     

      }
      // process any other attributes
      for (int j = 0; j < attributes.getLength(); j++)
      {
        String attrName = attributes.getLocalName(j);
        if (!attrName.equals(OASISCatalogConstants.ATTR_URI) && !attrName.equals(OASISCatalogConstants.ATTR_NAME) && !attrName.equals(OASISCatalogConstants.ATTR_PUBLIC_ID)
            && !attrName.equals(OASISCatalogConstants.ATTR_SYSTEM_ID) && !attrName.equals(OASISCatalogConstants.ATTR_CATALOG) && !attrName.equals(OASISCatalogConstants.ATTR_ID)
            && !attrName.equals(OASISCatalogConstants.ATTR_BASE))
        {
          String attrValue = attributes.getValue(attrName);
          if (attrValue != null && !attrValue.equals("")) //$NON-NLS-1$
          {
            catalogElement.setAttributeValue(attrName, attrValue);
          }
        }
      }
      catalog.addCatalogElement(catalogElement);

    }

    public void endElement(String uri, String localName, String qName) throws SAXException
    {
      if (baseURIStack.size() > 0)
      {
        baseURIStack.pop();
      }
    }
    
  }  
  
  // for backward compatability
  interface CompatabilityConstants{
	  
	  public static final String TAG_ID_XML_CATALOG_SETTINGS = "XMLCatalogSettings"; //$NON-NLS-1$
	  public static final String TAG_ID_USER_ENTRIES = "UserEntries"; //$NON-NLS-1$
	  public static final String TAG_USER_ENTRY = "UserEntry"; //$NON-NLS-1$
	  public static final String ATT_TYPE = "TYPE"; //$NON-NLS-1$
	  public static final String ATT_ID = "ID"; //$NON-NLS-1$
	  public static final String ATT_URI = "URI"; //$NON-NLS-1$
	  public static final String ATT_WEB_URL = "WEB_URL"; //$NON-NLS-1$
  }
}

Back to the top