1/* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.xml.internal.xsom.impl; 27 28import com.sun.xml.internal.xsom.SCD; 29import com.sun.xml.internal.xsom.XSAnnotation; 30import com.sun.xml.internal.xsom.XSComponent; 31import com.sun.xml.internal.xsom.XSSchemaSet; 32import com.sun.xml.internal.xsom.util.ComponentNameFunction; 33import com.sun.xml.internal.xsom.impl.parser.SchemaDocumentImpl; 34import com.sun.xml.internal.xsom.parser.SchemaDocument; 35import org.xml.sax.Locator; 36 37import javax.xml.namespace.NamespaceContext; 38import java.text.ParseException; 39import java.util.ArrayList; 40import java.util.Collection; 41import java.util.Collections; 42import java.util.List; 43 44public abstract class ComponentImpl implements XSComponent 45{ 46 protected ComponentImpl( SchemaDocumentImpl _owner, AnnotationImpl _annon, Locator _loc, ForeignAttributesImpl fa ) { 47 this.ownerDocument = _owner; 48 this.annotation = _annon; 49 this.locator = _loc; 50 this.foreignAttributes = fa; 51 } 52 53 protected final SchemaDocumentImpl ownerDocument; 54 public SchemaImpl getOwnerSchema() { 55 if(ownerDocument==null) 56 return null; 57 else 58 return ownerDocument.getSchema(); 59 } 60 61 public XSSchemaSet getRoot() { 62 if(ownerDocument==null) 63 return null; 64 else 65 return getOwnerSchema().getRoot(); 66 } 67 68 public SchemaDocument getSourceDocument() { 69 return ownerDocument; 70 } 71 72 private AnnotationImpl annotation; 73 public final XSAnnotation getAnnotation() { return annotation; } 74 75 public XSAnnotation getAnnotation(boolean createIfNotExist) { 76 if(createIfNotExist && annotation==null) { 77 annotation = new AnnotationImpl(); 78 } 79 return annotation; 80 } 81 82 private final Locator locator; 83 public final Locator getLocator() { return locator; } 84 85 /** 86 * Either {@link ForeignAttributesImpl} or {@link List}. 87 * 88 * Initially it's {@link ForeignAttributesImpl}, but it's lazily turned into 89 * a list when necessary. 90 */ 91 private Object foreignAttributes; 92 93 public List<ForeignAttributesImpl> getForeignAttributes() { 94 Object t = foreignAttributes; 95 96 if(t==null) 97 return Collections.EMPTY_LIST; 98 99 if(t instanceof List) 100 return (List)t; 101 102 t = foreignAttributes = convertToList((ForeignAttributesImpl)t); 103 return (List)t; 104 } 105 106 public String getForeignAttribute(String nsUri, String localName) { 107 for( ForeignAttributesImpl fa : getForeignAttributes() ) { 108 String v = fa.getValue(nsUri,localName); 109 if(v!=null) return v; 110 } 111 return null; 112 } 113 114 private List<ForeignAttributesImpl> convertToList(ForeignAttributesImpl fa) { 115 List<ForeignAttributesImpl> lst = new ArrayList<ForeignAttributesImpl>(); 116 while(fa!=null) { 117 lst.add(fa); 118 fa = fa.next; 119 } 120 return Collections.unmodifiableList(lst); 121 } 122 123 public Collection<XSComponent> select(String scd, NamespaceContext nsContext) { 124 try { 125 return SCD.create(scd,nsContext).select(this); 126 } catch (ParseException e) { 127 throw new IllegalArgumentException(e); 128 } 129 } 130 131 public XSComponent selectSingle(String scd, NamespaceContext nsContext) { 132 try { 133 return SCD.create(scd,nsContext).selectSingle(this); 134 } catch (ParseException e) { 135 throw new IllegalArgumentException(e); 136 } 137 } 138 139 @Override 140 public String toString() { 141 return apply(new ComponentNameFunction()); 142 } 143} 144