Skip to main content
summaryrefslogtreecommitdiffstats
blob: a50f07e31e163a62df551f74a3ea4fcd0b2be0e5 (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
/*******************************************************************************
 * Copyright (c) 2002-2005 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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.xml;

import java.net.URL;

import org.eclipse.wst.wsi.internal.core.WSIException;
import org.w3c.dom.Document;

/**
 * This is an abstract class that takes advantage of cached XML documents.
 * @author Peter  Brittenham (peterbr@us.ibm.com)
 * @version 1.0.1
 */

public abstract class XMLDocumentCacheUser
{
  /**
   * Get document from cache.
   * @see #setDocument
   *
   * @param key  a String. 
   * @return a Document object corresponding to the key.
   */
  protected Document getDocument(String key)
  {
    return (Document) XMLDocumentCache.instance().get(key);
  }

  /**
   * Put document with corresponding key in cache.
   * @see #getDocument
   *
   * @param key       a String used as a key to identify specified document.
   * @param document  a document.
   */
  protected void setDocument(String key, Object document)
  {
	  XMLDocumentCache.instance().put(key, document);
  }

  /**
   * Get document list.
   * @return a XMLDocumentCache object representing the document list value.
   */
  public XMLDocumentCache getDocumentList()
  {
    return XMLDocumentCache.instance();
  }

  /**
   * Parse XML document.
   * @param urlString  a String locating the XML document.
   * @param baseURI    a base url to assist in locating the XML document.
   * @return a Document object.
   * @throws WSIException if document cannot be parsed.
   */
  public Document parseXMLDocumentURL(String urlString, String baseURI)
    throws WSIException
  {
    return parseXMLDocumentURL(urlString, baseURI, null);
  }

  /**
   * Parse XML document and validate with a schema document.
   * @param urlString  a String locating the XML document.
   * @param baseURI    a base url to assist in locating the XML document.
   * @param schema     a String identifying related schema document.
   * @return a Document object.
   * @throws WSIException if document cannot be parsed.
   */
  public Document parseXMLDocumentURL(
    String urlString,
    String baseURI,
    String schema)
    throws WSIException
  {
    Document document = null;

    try
    {
      // Create URL reference to document
      URL url = XMLUtils.createURL(urlString, baseURI);
               
      // TODO: Add schema to the xml document cache, so that you can detect when it has already 
      //  been parsed using a specific schema definition 

      // If the document is not in the cache, then read and parse it
   	  if ((document = getDocument(urlString)) == null)
      {
        document = XMLUtils.parseXMLDocumentURL(url, schema);
        // Add document to cache
        setDocument(urlString, document);
      }
    }

    catch (WSIException e)
    {
      throw e;
    }

    catch (Exception e)
    {
      throw new WSIException(e.getMessage(), e);
    }

    return document;
  }
}

Back to the top