View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.webdav.util;
14  
15  import org.abstracthorizon.danube.webdav.xml.XMLParserHandler;
16  
17  /**
18   * Simple name space provider implementation
19   *
20   * @author Daniel Sendula
21   */
22  public class SimpleNamespacesProvider implements NamespacesProvider {
23  
24      /** Array of namespaces */
25      protected NamespaceSlot[] namespaces = new NamespaceSlot[5];
26  
27      /** Number of namespaces in the array */
28      protected int numberOfNamespaces = 0;
29  
30      /**
31       * Returns an array of defined URLS
32       * @return an array of defined URLS
33       */
34      public String[] getDefinedURLs() {
35          String[] result = new String[numberOfNamespaces];
36          for (int i = 0; i < numberOfNamespaces; i++) {
37              result[i] = namespaces[i].url;
38          }
39          return result;
40      }
41  
42      /**
43       * Returns assigned prefix for given URL
44       * @param url URL
45       * @return assigned prefix for given URL
46       */
47      public String getAssignedPrefix(String url) {
48          for (int i = 0; i < numberOfNamespaces; i++) {
49              if (namespaces[i].url == url) {
50                  return namespaces[i].prefix;
51              }
52          }
53          for (int i = 0; i < numberOfNamespaces; i++) {
54              if (namespaces[i].url.equals(url)) {
55                  return namespaces[i].prefix;
56              }
57          }
58          return null;
59      }
60  
61      /**
62       * Returns {@link XMLParserHandler} for given URL
63       * @param url URL
64       * @return {@link XMLParserHandler} for given URL
65       */
66      public XMLParserHandler getParserHandler(String url) {
67          for (int i = 0; i < numberOfNamespaces; i++) {
68              if (namespaces[i].url == url) {
69                  return namespaces[i].parserHandler;
70              }
71          }
72          for (int i = 0; i < numberOfNamespaces; i++) {
73              if (namespaces[i].url.equals(url)) {
74                  return namespaces[i].parserHandler;
75              }
76          }
77  
78          return null;
79      }
80  
81      /**
82       * Class that defines namespace
83       *
84       * @author Daniel Sendula
85       */
86      protected static class NamespaceSlot {
87          /** Prefix */
88          String prefix;
89  
90          /** URL */
91          String url;
92  
93          /** Parser handler */
94          XMLParserHandler parserHandler;
95      }
96  
97      /**
98       * Adds new namespace to the provider
99       * @param url namespace's URL
100      * @param preferredPrefix preferred prefix
101      * @param parserHandler parser handler
102      */
103     public synchronized void addNamespace(String url, String preferredPrefix, XMLParserHandler parserHandler) {
104         if (namespaces.length == numberOfNamespaces) {
105             NamespaceSlot[] newNamespaces = new NamespaceSlot[namespaces.length + (namespaces.length / 2)];
106             System.arraycopy(namespaces, 0, newNamespaces, 0, numberOfNamespaces);
107             namespaces = newNamespaces;
108         }
109         NamespaceSlot newSlot = new NamespaceSlot();
110         newSlot.parserHandler = parserHandler;
111         newSlot.url = url;
112 
113         if (getAssignedPrefix(preferredPrefix) != null) {
114             newSlot.prefix = new String(new char[]{(char)('a' + numberOfNamespaces)});
115         } else {
116             newSlot.prefix = preferredPrefix;
117         }
118         namespaces[numberOfNamespaces] = newSlot;
119         numberOfNamespaces = numberOfNamespaces + 1;
120     }
121 
122 }