Skip to main content
summaryrefslogtreecommitdiffstats
blob: 944b7d76ca6e9cdf5f3bcf94d18108630d34fb1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.wsdl.validation.internal.wsdl11.xsd;

import java.io.IOException;
import java.io.StringReader;
import java.util.Hashtable;
import java.util.Set;

import org.apache.xerces.xni.XMLResourceIdentifier;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.apache.xerces.xni.parser.XMLInputSource;

/**
 * An XMLEntityResolver that allows inline schemas to resolve each other through imports.
 */
public class InlineXSDResolver implements XMLEntityResolver
{
  public static final String INLINE_SCHEMA_ID = "#inlineschema";
  protected final String FILE_PREFIX = "file:";
  protected final String XMLNS = "xmlns";
  protected Hashtable entities = new Hashtable();
//  protected String refLocation = null;
  protected XMLEntityResolver externalResolver = null;
  protected XMLInputSource referringSchemaInputSource = null;
  protected String referringSchemaNamespace = null;
  
  /**
   * Constuctor.
   */
  public InlineXSDResolver()
  {
  }

  /* (non-Javadoc)
   * @see org.apache.xerces.xni.parser.XMLEntityResolver#resolveEntity(org.apache.xerces.xni.XMLResourceIdentifier)
   */
  public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
		 throws XNIException, IOException {
	String systemId = resourceIdentifier.getExpandedSystemId();	 
	String publicId = resourceIdentifier.getPublicId();	
	String namespace = resourceIdentifier.getNamespace();
	XMLInputSource is = null;
	String schema = null;
	if (systemId == null)
	{
	  if(publicId == null)
	  {
	  	if(namespace == null)
	  	{	
	      return null;
	  	}
	  	else
	  	{
	  		systemId = namespace;
	  	}
	  }
	  else
	  {	
	    systemId = publicId;
	  }
	}
	  
	if(referringSchemaNamespace != null && referringSchemaNamespace.equals(systemId))
	{
		if(referringSchemaInputSource!=null)
		{
			return referringSchemaInputSource;
		}
	}
	else if ((schema = (String) entities.get(systemId)) != null && !schema.equals(""))
	{
		is = new XMLInputSource(publicId, referringSchemaInputSource.getSystemId() + INLINE_SCHEMA_ID, null, new StringReader(schema),null);
	}
	
    //if(is == null)
    //{
    //	throw new IOException();
    //}
	return is;
  }

  /**
   * Add an inline schema.
   * 
   * @param targetNamespace - the target namespace of the schema
   * @param schema - a string representation of the schema
   */
  public void add(String targetNamespace, String schema)
  {
    entities.put(targetNamespace, schema);
  }
  
  /**
   * Add the referring inline schema. 
   * 
   * @param inputSource - a representation of the inline schema
   * @param namespace - the namespace of the inline schema
   */
  public void addReferringSchema(XMLInputSource inputSource, String namespace)
  {
    referringSchemaInputSource = inputSource;
    referringSchemaNamespace = namespace;
  }
  
  /**
   * Return true if the namespace corresponds to an inline schema, false otherwise.
   * 
   * @param namespace The namespace of the schema.
   * @return True if the namespace corresponds to an inline schema, false otherwise.
   */
  public boolean isInlineSchema(String namespace)
  {
    return entities.containsKey(namespace);
  }
  
  /** 
   * Get the set of the inline schema namespaces. 
   * 
   * @return The set of the inline schema namespaces. 
   */ 
  public Set getInlineSchemaNSs() 
  { 
        return entities.keySet(); 
  } 

}

Back to the top