1 /* 2 * Copyright (c) 1997, 2010, 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 26 package com.sun.tools.internal.ws.wsdl.framework; 27 28 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder; 29 import com.sun.tools.internal.ws.wsdl.parser.DOMForest; 30 import com.sun.tools.internal.ws.wscompile.ErrorReceiver; 31 import com.sun.tools.internal.ws.wscompile.AbortException; 32 import com.sun.tools.internal.ws.resources.WsdlMessages; 33 34 import javax.xml.namespace.QName; 35 import java.util.*; 36 37 import org.xml.sax.helpers.LocatorImpl; 38 39 /** 40 * An abstract class for documents containing entities. 41 * 42 * @author WS Development Team 43 */ 44 public abstract class AbstractDocument { 45 46 protected final DOMForest forest; 47 protected final ErrorReceiver errReceiver; 48 49 protected AbstractDocument(MetadataFinder forest, ErrorReceiver errReceiver) { 50 this.forest = forest; 51 this.errReceiver = errReceiver; 52 kinds = new HashMap(); 53 importedEntities = new ArrayList(); 54 importedDocuments = new HashSet(); 55 includedEntities = new ArrayList(); 56 includedDocuments = new HashSet(); 57 } 58 59 public String getSystemId() { 60 return _systemId; 61 } 62 63 public void setSystemId(String s) { 64 if (_systemId != null && !_systemId.equals(s)) { 65 // avoid redefinition of a system identifier 66 throw new IllegalArgumentException(); 67 } 68 69 _systemId = s; 70 if (s != null) { 71 importedDocuments.add(s); 72 } 73 } 74 75 public void addIncludedDocument(String systemId) { 76 includedDocuments.add(systemId); 77 } 78 79 public boolean isIncludedDocument(String systemId) { 80 return includedDocuments.contains(systemId); 81 } 82 83 public void addIncludedEntity(Entity entity) { 84 includedEntities.add(entity); 85 } 86 87 public void addImportedDocument(String systemId) { 88 importedDocuments.add(systemId); 89 } 90 91 public boolean isImportedDocument(String systemId) { 92 return importedDocuments.contains(systemId); 93 } 94 95 public void addImportedEntity(Entity entity) { 96 importedEntities.add(entity); 97 } 98 99 public void withAllSubEntitiesDo(EntityAction action) { 100 if (getRoot() != null) { 101 action.perform(getRoot()); 102 } 103 104 for (Iterator iter = importedEntities.iterator(); iter.hasNext();) { 105 action.perform((Entity) iter.next()); 106 } 107 108 for (Iterator iter = includedEntities.iterator(); iter.hasNext();) { 109 action.perform((Entity) iter.next()); 110 } 111 } 112 113 public Map getMap(Kind k) { 114 Map m = (Map) kinds.get(k.getName()); 115 if (m == null) { 116 m = new HashMap(); 117 kinds.put(k.getName(), m); 118 } 119 return m; 120 } 121 122 public void define(GloballyKnown e) { 123 Map map = getMap(e.getKind()); 124 if (e.getName() == null) 125 return; 126 QName name = 127 new QName(e.getDefining().getTargetNamespaceURI(), e.getName()); 128 129 if (map.containsKey(name)){ 130 errReceiver.error(e.getLocator(), WsdlMessages.ENTITY_DUPLICATE_WITH_TYPE(e.getElementName().getLocalPart(), e.getName())); 131 throw new AbortException(); 132 }else{ 133 map.put(name, e); 134 } 135 } 136 137 public GloballyKnown find(Kind k, QName name) { 138 Map map = getMap(k); 139 Object result = map.get(name); 140 if (result == null){ 141 errReceiver.error(null, WsdlMessages.ENTITY_NOT_FOUND_BY_Q_NAME(k.getName(), name, _systemId)); 142 throw new AbortException(); 143 } 144 return (GloballyKnown) result; 145 } 146 147 public void validateLocally() { 148 LocallyValidatingAction action = new LocallyValidatingAction(); 149 withAllSubEntitiesDo(action); 150 if (action.getException() != null) { 151 throw action.getException(); 152 } 153 } 154 155 public abstract void validate(EntityReferenceValidator validator); 156 157 protected abstract Entity getRoot(); 158 159 private final Map kinds; 160 private String _systemId; 161 private final Set importedDocuments; 162 private final List importedEntities; 163 private final Set includedDocuments; 164 private final List includedEntities; 165 166 private class LocallyValidatingAction implements EntityAction { 167 public LocallyValidatingAction() { 168 } 169 170 public void perform(Entity entity) { 171 try { 172 entity.validateThis(); 173 entity.withAllSubEntitiesDo(this); 174 } catch (ValidationException e) { 175 if (_exception == null) { 176 _exception = e; 177 } 178 } 179 } 180 181 public ValidationException getException() { 182 return _exception; 183 } 184 185 private ValidationException _exception; 186 } 187 }