325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.wsdl.framework;
325N/A
325N/Aimport com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
325N/Aimport com.sun.tools.internal.ws.wsdl.parser.DOMForest;
325N/Aimport com.sun.tools.internal.ws.wscompile.ErrorReceiver;
325N/Aimport com.sun.tools.internal.ws.wscompile.AbortException;
325N/Aimport com.sun.tools.internal.ws.resources.WsdlMessages;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/Aimport java.util.*;
325N/A
325N/Aimport org.xml.sax.helpers.LocatorImpl;
325N/A
325N/A/**
325N/A * An abstract class for documents containing entities.
325N/A *
325N/A * @author WS Development Team
325N/A */
325N/Apublic abstract class AbstractDocument {
325N/A
325N/A protected final DOMForest forest;
325N/A protected final ErrorReceiver errReceiver;
325N/A
325N/A protected AbstractDocument(MetadataFinder forest, ErrorReceiver errReceiver) {
325N/A this.forest = forest;
325N/A this.errReceiver = errReceiver;
325N/A kinds = new HashMap();
325N/A importedEntities = new ArrayList();
325N/A importedDocuments = new HashSet();
325N/A includedEntities = new ArrayList();
325N/A includedDocuments = new HashSet();
325N/A }
325N/A
325N/A public String getSystemId() {
325N/A return _systemId;
325N/A }
325N/A
325N/A public void setSystemId(String s) {
325N/A if (_systemId != null && !_systemId.equals(s)) {
325N/A // avoid redefinition of a system identifier
325N/A throw new IllegalArgumentException();
325N/A }
325N/A
325N/A _systemId = s;
325N/A if (s != null) {
325N/A importedDocuments.add(s);
325N/A }
325N/A }
325N/A
325N/A public void addIncludedDocument(String systemId) {
325N/A includedDocuments.add(systemId);
325N/A }
325N/A
325N/A public boolean isIncludedDocument(String systemId) {
325N/A return includedDocuments.contains(systemId);
325N/A }
325N/A
325N/A public void addIncludedEntity(Entity entity) {
325N/A includedEntities.add(entity);
325N/A }
325N/A
325N/A public void addImportedDocument(String systemId) {
325N/A importedDocuments.add(systemId);
325N/A }
325N/A
325N/A public boolean isImportedDocument(String systemId) {
325N/A return importedDocuments.contains(systemId);
325N/A }
325N/A
325N/A public void addImportedEntity(Entity entity) {
325N/A importedEntities.add(entity);
325N/A }
325N/A
325N/A public void withAllSubEntitiesDo(EntityAction action) {
325N/A if (getRoot() != null) {
325N/A action.perform(getRoot());
325N/A }
325N/A
325N/A for (Iterator iter = importedEntities.iterator(); iter.hasNext();) {
325N/A action.perform((Entity) iter.next());
325N/A }
325N/A
325N/A for (Iterator iter = includedEntities.iterator(); iter.hasNext();) {
325N/A action.perform((Entity) iter.next());
325N/A }
325N/A }
325N/A
325N/A public Map getMap(Kind k) {
325N/A Map m = (Map) kinds.get(k.getName());
325N/A if (m == null) {
325N/A m = new HashMap();
325N/A kinds.put(k.getName(), m);
325N/A }
325N/A return m;
325N/A }
325N/A
325N/A public void define(GloballyKnown e) {
325N/A Map map = getMap(e.getKind());
325N/A if (e.getName() == null)
325N/A return;
325N/A QName name =
325N/A new QName(e.getDefining().getTargetNamespaceURI(), e.getName());
325N/A
325N/A if (map.containsKey(name)){
325N/A errReceiver.error(e.getLocator(), WsdlMessages.ENTITY_DUPLICATE_WITH_TYPE(e.getElementName().getLocalPart(), e.getName()));
325N/A throw new AbortException();
325N/A }else{
325N/A map.put(name, e);
325N/A }
325N/A }
325N/A
325N/A public GloballyKnown find(Kind k, QName name) {
325N/A Map map = getMap(k);
325N/A Object result = map.get(name);
325N/A if (result == null){
325N/A errReceiver.error(null, WsdlMessages.ENTITY_NOT_FOUND_BY_Q_NAME(k.getName(), name, _systemId));
325N/A throw new AbortException();
325N/A }
325N/A return (GloballyKnown) result;
325N/A }
325N/A
325N/A public void validateLocally() {
325N/A LocallyValidatingAction action = new LocallyValidatingAction();
325N/A withAllSubEntitiesDo(action);
325N/A if (action.getException() != null) {
325N/A throw action.getException();
325N/A }
325N/A }
325N/A
325N/A public abstract void validate(EntityReferenceValidator validator);
325N/A
325N/A protected abstract Entity getRoot();
325N/A
325N/A private final Map kinds;
325N/A private String _systemId;
325N/A private final Set importedDocuments;
325N/A private final List importedEntities;
325N/A private final Set includedDocuments;
325N/A private final List includedEntities;
325N/A
325N/A private class LocallyValidatingAction implements EntityAction {
325N/A public LocallyValidatingAction() {
325N/A }
325N/A
325N/A public void perform(Entity entity) {
325N/A try {
325N/A entity.validateThis();
325N/A entity.withAllSubEntitiesDo(this);
325N/A } catch (ValidationException e) {
325N/A if (_exception == null) {
325N/A _exception = e;
325N/A }
325N/A }
325N/A }
325N/A
325N/A public ValidationException getException() {
325N/A return _exception;
325N/A }
325N/A
325N/A private ValidationException _exception;
325N/A }
325N/A}