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