001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.document.schema;
027:
028: import java.util.Set;
029:
030: import javax.xml.namespace.QName;
031:
032: import com.sun.tools.internal.ws.wsdl.framework.AbstractDocument;
033: import com.sun.tools.internal.ws.wsdl.framework.Entity;
034: import com.sun.tools.internal.ws.wsdl.framework.EntityAction;
035: import com.sun.tools.internal.ws.wsdl.framework.EntityReferenceAction;
036: import com.sun.tools.internal.ws.wsdl.framework.EntityReferenceValidator;
037: import com.sun.tools.internal.ws.wsdl.framework.GloballyKnown;
038: import com.sun.tools.internal.ws.wsdl.framework.Kind;
039: import com.sun.tools.internal.ws.wsdl.framework.NoSuchEntityException;
040: import com.sun.tools.internal.ws.wsdl.framework.ValidationException;
041:
042: /**
043: * A XML Schema document.
044: *
045: * @author WS Development Team
046: */
047: public class SchemaDocument extends AbstractDocument {
048:
049: public SchemaDocument() {
050: }
051:
052: public Schema getSchema() {
053: return _schema;
054: }
055:
056: public void setSchema(Schema s) {
057: _schema = s;
058: }
059:
060: public Set collectAllNamespaces() {
061: Set result = super .collectAllNamespaces();
062: if (_schema.getTargetNamespaceURI() != null) {
063: result.add(_schema.getTargetNamespaceURI());
064: }
065: return result;
066: }
067:
068: public void validate(EntityReferenceValidator validator) {
069: GloballyValidatingAction action = new GloballyValidatingAction(
070: this , validator);
071: withAllSubEntitiesDo(action);
072: if (action.getException() != null) {
073: throw action.getException();
074: }
075: }
076:
077: protected Entity getRoot() {
078: return _schema;
079: }
080:
081: private Schema _schema;
082:
083: private class GloballyValidatingAction implements EntityAction,
084: EntityReferenceAction {
085: public GloballyValidatingAction(AbstractDocument document,
086: EntityReferenceValidator validator) {
087: _document = document;
088: _validator = validator;
089: }
090:
091: public void perform(Entity entity) {
092: try {
093: entity.validateThis();
094: entity.withAllEntityReferencesDo(this );
095: entity.withAllSubEntitiesDo(this );
096: } catch (ValidationException e) {
097: if (_exception == null) {
098: _exception = e;
099: }
100: }
101: }
102:
103: public void perform(Kind kind, QName name) {
104: try {
105: GloballyKnown entity = _document.find(kind, name);
106: } catch (NoSuchEntityException e) {
107: // failed to resolve, check with the validator
108: if (_exception == null) {
109: if (_validator == null
110: || !_validator.isValid(kind, name)) {
111: _exception = e;
112: }
113: }
114: }
115: }
116:
117: public ValidationException getException() {
118: return _exception;
119: }
120:
121: private ValidationException _exception;
122: private AbstractDocument _document;
123: private EntityReferenceValidator _validator;
124: }
125: }
|