1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.impl.xs.util;
  23 
  24 import com.sun.org.apache.xerces.internal.impl.Constants;
  25 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  26 import com.sun.org.apache.xerces.internal.impl.xs.XSModelImpl;
  27 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
  28 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
  29 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  30 import com.sun.org.apache.xerces.internal.xs.XSModel;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 
  34 
  35 /**
  36  * Add a method that return an <code>XSModel</code> that represents components in
  37  * the schema grammars in this pool implementation.
  38  *
  39  * @xerces.internal
  40  *
  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 = (SchemaGrammar[])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