src/java.xml/share/classes/com/sun/org/apache/xml/internal/resolver/readers/XCatalogReader.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 // XCatalogReader.java - Read XML Catalog files
   6 
   7 /*
   8  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
   9  * as applicable.
  10  *
  11  * Licensed under the Apache License, Version 2.0 (the "License");
  12  * you may not use this file except in compliance with the License.
  13  * You may obtain a copy of the License at
  14  *
  15  *      http://www.apache.org/licenses/LICENSE-2.0
  16  *
  17  * Unless required by applicable law or agreed to in writing, software
  18  * distributed under the License is distributed on an "AS IS" BASIS,
  19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20  * See the License for the specific language governing permissions and
  21  * limitations under the License.
  22  */
  23 
  24 package com.sun.org.apache.xml.internal.resolver.readers;
  25 
  26 import java.util.Vector;
  27 import com.sun.org.apache.xml.internal.resolver.Catalog;
  28 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
  29 import com.sun.org.apache.xml.internal.resolver.CatalogException;
  30 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
  31 
  32 import org.xml.sax.*;
  33 
  34 import javax.xml.parsers.*;
  35 
  36 /**
  37  * Parse "xcatalog" XML Catalog files, this is the XML Catalog format
  38  * developed by John Cowan and supported by Apache.
  39  *
  40  * @see Catalog
  41  *
  42  * @author Norman Walsh
  43  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
  44  *
  45  */
  46 public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
  47   /** The catalog object needs to be stored by the object so that
  48    * SAX callbacks can use it.
  49    */
  50   protected Catalog catalog = null;
  51 
  52   /** Set the current catalog. */
  53   public void setCatalog (Catalog catalog) {
  54     this.catalog = catalog;

  55   }
  56 
  57   /** Get the current catalog. */
  58   public Catalog getCatalog () {
  59     return catalog;
  60   }
  61 
  62   /** The constructor */
  63   public XCatalogReader(SAXParserFactory parserFactory) {





  64     super(parserFactory);

  65   }
  66 
  67   // ----------------------------------------------------------------------
  68   // Implement the SAX DocumentHandler interface
  69 
  70   /** The SAX <code>setDocumentLocator</code> method does nothing. */
  71   public void setDocumentLocator (Locator locator) {
  72     return;
  73   }
  74 
  75   /** The SAX <code>startDocument</code> method does nothing. */
  76   public void startDocument ()
  77     throws SAXException {
  78     return;
  79   }
  80 
  81   /** The SAX <code>endDocument</code> method does nothing. */
  82   public void endDocument ()
  83     throws SAXException {
  84     return;
  85   }
  86 
  87   /**
  88    * The SAX <code>startElement</code> method recognizes elements
  89    * from the plain catalog format and instantiates CatalogEntry
  90    * objects for them.
  91    *
  92    * @param namespaceURI The namespace name of the element.
  93    * @param localName The local name of the element.
  94    * @param qName The QName of the element.
  95    * @param atts The list of attributes on the element.
  96    *
  97    * @see CatalogEntry
  98    */
  99   public void startElement (String namespaceURI,
 100                             String localName,
 101                             String qName,
 102                             Attributes atts)
 103     throws SAXException {
 104 
 105     int entryType = -1;
 106     Vector entryArgs = new Vector();
 107 
 108     if (localName.equals("Base")) {
 109       entryType = catalog.BASE;
 110       entryArgs.add(atts.getValue("HRef"));
 111 
 112       catalog.getCatalogManager().debug.message(4, "Base", atts.getValue("HRef"));
 113     } else if (localName.equals("Delegate")) {
 114       entryType = catalog.DELEGATE_PUBLIC;
 115       entryArgs.add(atts.getValue("PublicId"));
 116       entryArgs.add(atts.getValue("HRef"));
 117 
 118       catalog.getCatalogManager().debug.message(4, "Delegate",
 119                     PublicId.normalize(atts.getValue("PublicId")),
 120                     atts.getValue("HRef"));
 121     } else if (localName.equals("Extend")) {
 122       entryType = catalog.CATALOG;
 123       entryArgs.add(atts.getValue("HRef"));
 124 
 125       catalog.getCatalogManager().debug.message(4, "Extend", atts.getValue("HRef"));
 126     } else if (localName.equals("Map")) {
 127       entryType = catalog.PUBLIC;
 128       entryArgs.add(atts.getValue("PublicId"));
 129       entryArgs.add(atts.getValue("HRef"));
 130 
 131       catalog.getCatalogManager().debug.message(4, "Map",
 132                     PublicId.normalize(atts.getValue("PublicId")),
 133                     atts.getValue("HRef"));
 134     } else if (localName.equals("Remap")) {
 135       entryType = catalog.SYSTEM;
 136       entryArgs.add(atts.getValue("SystemId"));
 137       entryArgs.add(atts.getValue("HRef"));
 138 
 139       catalog.getCatalogManager().debug.message(4, "Remap",
 140                     atts.getValue("SystemId"),
 141                     atts.getValue("HRef"));
 142     } else if (localName.equals("XMLCatalog")) {
 143       // nop, start of catalog
 144     } else {
 145       // This is equivalent to an invalid catalog entry type
 146       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
 147     }
 148 
 149     if (entryType >= 0) {
 150       try {
 151         CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
 152         catalog.addEntry(ce);
 153       } catch (CatalogException cex) {
 154         if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
 155           catalog.getCatalogManager().debug.message(1, "Invalid catalog entry type", localName);
 156         } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
 157           catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", localName);
 158         }
 159       }
 160     }
 161     }
 162 
 163     /** The SAX <code>endElement</code> method does nothing. */
 164     public void endElement (String namespaceURI,
 165                             String localName,
 166                             String qName)
 167       throws SAXException {
 168       return;
 169     }
 170 
 171   /** The SAX <code>characters</code> method does nothing. */
 172   public void characters (char ch[], int start, int length)
 173     throws SAXException {
 174     return;
 175   }
 176 
 177   /** The SAX <code>ignorableWhitespace</code> method does nothing. */
 178   public void ignorableWhitespace (char ch[], int start, int length)
 179     throws SAXException {
 180     return;
 181   }
 182 
 183   /** The SAX <code>processingInstruction</code> method does nothing. */
 184   public void processingInstruction (String target, String data)
 185     throws SAXException {
 186     return;
 187   }



















 188 }
   1 /*
   2  * Licensed to the Apache Software Foundation (ASF) under one or more
   3  * contributor license agreements.  See the NOTICE file distributed with
   4  * this work for additional information regarding copyright ownership.
   5  * The ASF licenses this file to You under the Apache License, Version 2.0
   6  * (the "License"); you may not use this file except in compliance with
   7  * the License.  You may obtain a copy of the License at


   8  *




   9  *      http://www.apache.org/licenses/LICENSE-2.0
  10  *
  11  * Unless required by applicable law or agreed to in writing, software
  12  * distributed under the License is distributed on an "AS IS" BASIS,
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14  * See the License for the specific language governing permissions and
  15  * limitations under the License.
  16  */
  17 
  18 package com.sun.org.apache.xml.internal.resolver.readers;
  19 
  20 import java.util.Vector;
  21 import com.sun.org.apache.xml.internal.resolver.Catalog;
  22 import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
  23 import com.sun.org.apache.xml.internal.resolver.CatalogException;
  24 import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
  25 
  26 import org.xml.sax.*;
  27 
  28 import javax.xml.parsers.*;
  29 
  30 /**
  31  * Parse "XCatalog" XML Catalog files, this is the XML Catalog format
  32  * developed by John Cowan and supported by Apache.
  33  *
  34  * @see Catalog
  35  *
  36  * @author Norman Walsh
  37  * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
  38  *
  39  */
  40 public class XCatalogReader extends SAXCatalogReader implements SAXCatalogParser {
  41   /** The catalog object needs to be stored by the object so that
  42    * SAX callbacks can use it.
  43    */
  44   protected Catalog catalog = null;
  45 
  46   /** Set the current catalog. */
  47   public void setCatalog (Catalog catalog) {
  48     this.catalog = catalog;
  49     debug = catalog.getCatalogManager().debug;
  50   }
  51 
  52   /** Get the current catalog. */
  53   public Catalog getCatalog () {
  54     return catalog;
  55   }
  56 
  57   /** Default constructor */
  58   public XCatalogReader() {
  59     super();
  60   }
  61 
  62   /** Constructor allowing for providing custom SAX parser factory */
  63   public XCatalogReader(SAXParserFactory parserFactory, Catalog catalog) {
  64     super(parserFactory);
  65     setCatalog(catalog);
  66   }
  67 
  68   // ----------------------------------------------------------------------
  69   // Implement the SAX ContentHandler interface
  70 
  71   /** The SAX <code>setDocumentLocator</code> method does nothing. */
  72   public void setDocumentLocator (Locator locator) {
  73     return;
  74   }
  75 
  76   /** The SAX <code>startDocument</code> method does nothing. */
  77   public void startDocument ()
  78     throws SAXException {
  79     return;
  80   }
  81 
  82   /** The SAX <code>endDocument</code> method does nothing. */
  83   public void endDocument ()
  84     throws SAXException {
  85     return;
  86   }
  87 
  88   /**
  89    * The SAX <code>startElement</code> method recognizes elements
  90    * from the plain catalog format and instantiates CatalogEntry
  91    * objects for them.
  92    *
  93    * @param namespaceURI The namespace name of the element.
  94    * @param localName The local name of the element.
  95    * @param qName The QName of the element.
  96    * @param atts The list of attributes on the element.
  97    *
  98    * @see CatalogEntry
  99    */
 100   public void startElement (String namespaceURI,
 101                             String localName,
 102                             String qName,
 103                             Attributes atts)
 104     throws SAXException {
 105 
 106     int entryType = -1;
 107     Vector entryArgs = new Vector();
 108 
 109     if (localName.equals("Base")) {
 110       entryType = Catalog.BASE;
 111       entryArgs.add(atts.getValue("HRef"));
 112 
 113       debug.message(4, "Base", atts.getValue("HRef"));
 114     } else if (localName.equals("Delegate")) {
 115       entryType = Catalog.DELEGATE_PUBLIC;
 116       entryArgs.add(atts.getValue("PublicID"));
 117       entryArgs.add(atts.getValue("HRef"));
 118 
 119       debug.message(4, "Delegate",
 120                     PublicId.normalize(atts.getValue("PublicID")),
 121                     atts.getValue("HRef"));
 122     } else if (localName.equals("Extend")) {
 123       entryType = Catalog.CATALOG;
 124       entryArgs.add(atts.getValue("HRef"));
 125 
 126       debug.message(4, "Extend", atts.getValue("HRef"));
 127     } else if (localName.equals("Map")) {
 128       entryType = Catalog.PUBLIC;
 129       entryArgs.add(atts.getValue("PublicID"));
 130       entryArgs.add(atts.getValue("HRef"));
 131 
 132       debug.message(4, "Map",
 133                     PublicId.normalize(atts.getValue("PublicID")),
 134                     atts.getValue("HRef"));
 135     } else if (localName.equals("Remap")) {
 136       entryType = Catalog.SYSTEM;
 137       entryArgs.add(atts.getValue("SystemID"));
 138       entryArgs.add(atts.getValue("HRef"));
 139 
 140       debug.message(4, "Remap",
 141                     atts.getValue("SystemID"),
 142                     atts.getValue("HRef"));
 143     } else if (localName.equals("XCatalog")) {
 144       // nop, start of catalog
 145     } else {
 146       // This is equivalent to an invalid catalog entry type
 147       debug.message(1, "Invalid catalog entry type", localName);
 148     }
 149 
 150     if (entryType >= 0) {
 151       try {
 152         CatalogEntry ce = new CatalogEntry(entryType, entryArgs);
 153         catalog.addEntry(ce);
 154       } catch (CatalogException cex) {
 155         if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
 156           debug.message(1, "Invalid catalog entry type", localName);
 157         } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
 158           debug.message(1, "Invalid catalog entry", localName);
 159         }
 160       }
 161     }
 162   }
 163 
 164   /** The SAX <code>endElement</code> method does nothing. */
 165   public void endElement (String namespaceURI,
 166                           String localName,
 167                           String qName)
 168     throws SAXException {
 169     return;
 170   }
 171 
 172   /** The SAX <code>characters</code> method does nothing. */
 173   public void characters (char ch[], int start, int length)
 174     throws SAXException {
 175     return;
 176   }
 177 
 178   /** The SAX <code>ignorableWhitespace</code> method does nothing. */
 179   public void ignorableWhitespace (char ch[], int start, int length)
 180     throws SAXException {
 181     return;
 182   }
 183 
 184   /** The SAX <code>processingInstruction</code> method does nothing. */
 185   public void processingInstruction (String target, String data)
 186     throws SAXException {
 187     return;
 188   }
 189 
 190   /** The SAX <code>skippedEntity</code> method does nothing. */
 191   public void skippedEntity (String name)
 192     throws SAXException {
 193     return;
 194   }
 195 
 196   /** The SAX <code>startPrefixMapping</code> method does nothing. */
 197   public void startPrefixMapping(String prefix, String uri)
 198     throws SAXException {
 199     return;
 200   }
 201 
 202   /** The SAX <code>endPrefixMapping</code> method does nothing. */
 203   public void endPrefixMapping(String prefix)
 204     throws SAXException {
 205     return;
 206   }
 207 
 208 }