1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.dv.dtd.DTDDVFactoryImpl;
  24 import com.sun.org.apache.xerces.internal.impl.dv.dtd.XML11DTDDVFactoryImpl;
  25 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
  26 import java.util.Map;
  27 
  28 /**
  29  * The factory to create and return DTD types. The implementation should
  30  * store the created datatypes in static data, so that they can be shared by
  31  * multiple parser instance, and multiple threads.
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Sandy Gao, IBM
  36  *
  37  */
  38 public abstract class DTDDVFactory {
  39 
  40     private static final String DEFAULT_FACTORY_CLASS =
  41             "com.sun.org.apache.xerces.internal.impl.dv.dtd.DTDDVFactoryImpl";
  42 
  43     private static final String XML11_DATATYPE_VALIDATOR_FACTORY =
  44         "com.sun.org.apache.xerces.internal.impl.dv.dtd.XML11DTDDVFactoryImpl";
  45 
  46     /**
  47      * Get an instance of the default DTDDVFactory implementation.
  48      *
  49      * @return  an instance of DTDDVFactory implementation
  50      * @exception DVFactoryException  cannot create an instance of the specified
  51      *                                class name or the default class name
  52      */
  53     public static final DTDDVFactory getInstance() throws DVFactoryException {
  54         return getInstance(DEFAULT_FACTORY_CLASS);
  55     }
  56 
  57     /**
  58      * Get an instance of DTDDVFactory implementation.
  59      *
  60      * @param factoryClass  name of the implementation to load.
  61      * @return  an instance of DTDDVFactory implementation
  62      * @exception DVFactoryException  cannot create an instance of the specified
  63      *                                class name or the default class name
  64      */
  65     public static final DTDDVFactory getInstance(String factoryClass) throws DVFactoryException {
  66         try {
  67             if (DEFAULT_FACTORY_CLASS.equals(factoryClass)) {
  68                 return new DTDDVFactoryImpl();
  69             } else if (XML11_DATATYPE_VALIDATOR_FACTORY.equals(factoryClass)) {
  70                 return new XML11DTDDVFactoryImpl();
  71             } else {
  72                 //fall back for compatibility
  73                 return (DTDDVFactory)
  74                     (ObjectFactory.newInstance(factoryClass, true));
  75             }
  76         }
  77         catch (ClassCastException e) {
  78             throw new DVFactoryException("DTD factory class " + factoryClass + " does not extend from DTDDVFactory.");
  79         }
  80     }
  81 
  82     // can't create a new object of this class
  83     protected DTDDVFactory() {}
  84 
  85     /**
  86      * return a dtd type of the given name
  87      *
  88      * @param name  the name of the datatype
  89      * @return      the datatype validator of the given name
  90      */
  91     public abstract DatatypeValidator getBuiltInDV(String name);
  92 
  93     /**
  94      * get all built-in DVs, which are stored in a map keyed by the name
  95      *
  96      * @return      a map which contains all datatypes
  97      */
  98     public abstract Map<String, DatatypeValidator> getBuiltInTypes();
  99 
 100 }