1 /*
   2  * Copyright (c) 2017, 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.xs.util;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.Constants;
  24 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  25 import com.sun.org.apache.xerces.internal.impl.xs.XSModelImpl;
  26 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
  27 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
  28 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  29 import com.sun.org.apache.xerces.internal.xs.XSModel;
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 
  34 /**
  35  * Add a method that return an <code>XSModel</code> that represents components in
  36  * the schema grammars in this pool implementation.
  37  *
  38  * @xerces.internal
  39  *
  40  * @LastModified: Nov 2017
  41  */
  42 public class XSGrammarPool extends XMLGrammarPoolImpl {
  43 
  44     /**
  45      * Return an <code>XSModel</code> that represents components in
  46      * the schema grammars in this pool implementation.
  47      *
  48      * @return  an <code>XSModel</code> representing this schema grammar
  49      */
  50     public XSModel toXSModel() {
  51         return toXSModel(Constants.SCHEMA_VERSION_1_0);
  52     }
  53 
  54     public XSModel toXSModel(short schemaVersion) {
  55         List<Grammar> list = new ArrayList<>();
  56         for (int i = 0; i < fGrammars.length; i++) {
  57             for (Entry entry = fGrammars[i] ; entry != null ; entry = entry.next) {
  58                 if (entry.desc.getGrammarType().equals(XMLGrammarDescription.XML_SCHEMA)) {
  59                     list.add(entry.grammar);
  60                 }
  61             }
  62         }
  63         int size = list.size();
  64         if (size == 0) {
  65             return toXSModel(new SchemaGrammar[0], schemaVersion);
  66         }
  67         SchemaGrammar[] gs = list.toArray(new SchemaGrammar[size]);
  68         return toXSModel(gs, schemaVersion);
  69     }
  70 
  71     protected XSModel toXSModel(SchemaGrammar[] grammars, short schemaVersion) {
  72         return new XSModelImpl(grammars, schemaVersion);
  73     }
  74 
  75 } // class XSGrammarPool