You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/06/17 15:55:30 UTC

svn commit: r955602 - /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java

Author: reto
Date: Thu Jun 17 13:55:29 2010
New Revision: 955602

URL: http://svn.apache.org/viewvc?rev=955602&view=rev
Log:
CLEREZZA-238: added getSupportedFormats method

Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java?rev=955602&r1=955601&r2=955602&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.core/src/main/java/org/apache/clerezza/rdf/core/serializedform/Parser.java Thu Jun 17 13:55:29 2010
@@ -16,168 +16,179 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.rdf.core.serializedform;
-
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.ServiceLoader;
-
-import org.apache.clerezza.rdf.core.Graph;
-
-/**
- * This singleton class provides a method <code>parse</code> to transform 
- * serialized RDF forms into {@link Graph}s.
- * 
- * Functionality is delegated to registered {@link ParsingProvider}s. Such
- * <code>ParsingProvider</code>s can be registered and unregistered, later 
- * registered <code>ParsingProvider</code>s shadow previously registered
- * providers for the same format.
- *
- * Note on synchronization: <code>ParsingProvider</code>s must be able to handle
- * concurrent requests.
- * 
- * @author reto
- * 
- * @scr.component
- * @scr.service interface="org.apache.clerezza.rdf.core.serializedform.Parser"
- * @scr.reference name="parsingProvider"
- *     cardinality="0..n" policy="dynamic"
- *     interface="org.apache.clerezza.rdf.core.serializedform.ParsingProvider"
- */
-public class Parser {
-
-	/**
-	 * The list of providers in the order of registration
-	 */
-	private List<ParsingProvider> providerList = new ArrayList<ParsingProvider>();
-	/**
-	 * A map to quickly locate a provider
-	 */
-	private volatile Map<String, ParsingProvider> providerMap = new HashMap<String, ParsingProvider>();
-	/**
-	 * The singleton instance
-	 */
-	private volatile static Parser instance;
-
-	/**
-	 * the constructor sets the singleton instance to allow instantiation
-	 * by OSGi-DS. This constructor should not be called except by OSGi-DS,
-	 * otherwise the static <code>getInstance</code> method should be used.
-	 */
-	public Parser() {
-		Parser.instance = this;
-	}
-
-	/**
-	 * A constructor for tests, which doesn't set the singleton instance
-	 * 
-	 * @param dummy an ignored argument to distinguish this from the other constructor
-	 */
-	Parser(Object dummy) {
-	}
-
-	/**
-	 * This returns the singleton instance, if an instance has been previously 
-	 * created (e.g. by OSGi declarative services) this instance is returned,
-	 * otherwise a new instance is created and providers are injected using 
-	 * the service provider interface (META-INF/services/)
-	 *
-	 * @return the singleton Parser instance
-	 */
-	public static Parser getInstance() {
-		if (instance == null) {
-			synchronized (Parser.class) {
-				if (instance == null) {
-					new Parser();
-					Iterator<ParsingProvider> parsingProviders =
-							ServiceLoader.load(ParsingProvider.class).iterator();
-					while (parsingProviders.hasNext()) {
-						ParsingProvider parsingProvider = parsingProviders.next();
-						instance.bindParsingProvider(parsingProvider);
-					}
-				}
-			}
-		}
-		return instance;
-
-	}
-
-	/**
-	 * Parses a serialized Graph from an InputStream. This delegates the
-	 * processing to the provider registered for the specified format, if
-	 * the formatIdentifier contains a ';'-character only the section before
-	 * that character is used for choosing the provider.
-	 *
-	 * @param serializedGraph an inputstream with the serialization
-	 * @param formatIdentifier a string identifying the format (usually the MIME-type)
-	 * @return the graph read from the stream
-	 * @throws UnsupportedFormatException
-	 */
-	public Graph parse(InputStream serializedGraph,
-			String formatIdentifier) throws UnsupportedFormatException {
-		String deParameterizedIdentifier;
-		int semicolonPos = formatIdentifier.indexOf(';');
-		if (semicolonPos > -1) {
-			deParameterizedIdentifier = formatIdentifier.substring(0, semicolonPos);
-		} else {
-			deParameterizedIdentifier = formatIdentifier;
-		}
-		ParsingProvider provider = providerMap.get(deParameterizedIdentifier);
-		if (provider == null) {
-			throw new UnsupportedParsingFormatException(formatIdentifier);
-		}
-		return provider.parse(serializedGraph, formatIdentifier);
-	}
-
-	/**
-	 * Registers a parsing provider
-	 *
-	 * @param provider the provider to be registered
-	 */
-	public void bindParsingProvider(ParsingProvider provider) {
-		providerList.add(provider);
-		refreshProviderMap();
-	}
-
-	/**
-	 * Unregister a parsing provider
-	 *
-	 * @param provider the provider to be deregistered
-	 */
-	public void unbindParsingProvider(ParsingProvider provider) {
-		providerList.remove(provider);
-		refreshProviderMap();
-	}
-
-	/**
-	 * Update providerMap with the providers in the providerList
-	 * 
-	 */
-	private void refreshProviderMap() {
-		final Map<String, ParsingProvider> newProviderMap = new HashMap<String, ParsingProvider>();
-		for (ParsingProvider provider : providerList) {
-			String[] formatIdentifiers = getFormatIdentifiers(provider);
-			for (String formatIdentifier : formatIdentifiers) {
-				newProviderMap.put(formatIdentifier, provider);
-			}
-		}
-		providerMap = newProviderMap;
-	}
-
-	/**
-	 * Extract format identifiers for a parsing provider
-	 *
-	 * @param provider the provider to be registered
-	 * @return formatIdentifiers
-	 */
-	private String[] getFormatIdentifiers(ParsingProvider parsingProvider) {
-		Class<? extends ParsingProvider> clazz = parsingProvider.getClass();
-		SupportedFormat supportedFormatAnnotation = clazz.getAnnotation(SupportedFormat.class);
-		String[] formatIdentifiers = supportedFormatAnnotation.value();
-		return formatIdentifiers;
-	}
-}
+package org.apache.clerezza.rdf.core.serializedform;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.Graph;
+
+/**
+ * This singleton class provides a method <code>parse</code> to transform 
+ * serialized RDF forms into {@link Graph}s.
+ * 
+ * Functionality is delegated to registered {@link ParsingProvider}s. Such
+ * <code>ParsingProvider</code>s can be registered and unregistered, later 
+ * registered <code>ParsingProvider</code>s shadow previously registered
+ * providers for the same format.
+ *
+ * Note on synchronization: <code>ParsingProvider</code>s must be able to handle
+ * concurrent requests.
+ * 
+ * @author reto
+ * 
+ * @scr.component
+ * @scr.service interface="org.apache.clerezza.rdf.core.serializedform.Parser"
+ * @scr.reference name="parsingProvider"
+ *     cardinality="0..n" policy="dynamic"
+ *     interface="org.apache.clerezza.rdf.core.serializedform.ParsingProvider"
+ */
+public class Parser {
+
+	/**
+	 * The list of providers in the order of registration
+	 */
+	private List<ParsingProvider> providerList = new ArrayList<ParsingProvider>();
+	/**
+	 * A map to quickly locate a provider
+	 */
+	private volatile Map<String, ParsingProvider> providerMap = new HashMap<String, ParsingProvider>();
+	/**
+	 * The singleton instance
+	 */
+	private volatile static Parser instance;
+
+	/**
+	 * the constructor sets the singleton instance to allow instantiation
+	 * by OSGi-DS. This constructor should not be called except by OSGi-DS,
+	 * otherwise the static <code>getInstance</code> method should be used.
+	 */
+	public Parser() {
+		Parser.instance = this;
+	}
+
+	/**
+	 * A constructor for tests, which doesn't set the singleton instance
+	 * 
+	 * @param dummy an ignored argument to distinguish this from the other constructor
+	 */
+	Parser(Object dummy) {
+	}
+
+	/**
+	 * This returns the singleton instance, if an instance has been previously 
+	 * created (e.g. by OSGi declarative services) this instance is returned,
+	 * otherwise a new instance is created and providers are injected using 
+	 * the service provider interface (META-INF/services/)
+	 *
+	 * @return the singleton Parser instance
+	 */
+	public static Parser getInstance() {
+		if (instance == null) {
+			synchronized (Parser.class) {
+				if (instance == null) {
+					new Parser();
+					Iterator<ParsingProvider> parsingProviders =
+							ServiceLoader.load(ParsingProvider.class).iterator();
+					while (parsingProviders.hasNext()) {
+						ParsingProvider parsingProvider = parsingProviders.next();
+						instance.bindParsingProvider(parsingProvider);
+					}
+				}
+			}
+		}
+		return instance;
+
+	}
+
+	/**
+	 * Parses a serialized Graph from an InputStream. This delegates the
+	 * processing to the provider registered for the specified format, if
+	 * the formatIdentifier contains a ';'-character only the section before
+	 * that character is used for choosing the provider.
+	 *
+	 * @param serializedGraph an inputstream with the serialization
+	 * @param formatIdentifier a string identifying the format (usually the MIME-type)
+	 * @return the graph read from the stream
+	 * @throws UnsupportedFormatException
+	 */
+	public Graph parse(InputStream serializedGraph,
+			String formatIdentifier) throws UnsupportedFormatException {
+		String deParameterizedIdentifier;
+		int semicolonPos = formatIdentifier.indexOf(';');
+		if (semicolonPos > -1) {
+			deParameterizedIdentifier = formatIdentifier.substring(0, semicolonPos);
+		} else {
+			deParameterizedIdentifier = formatIdentifier;
+		}
+		ParsingProvider provider = providerMap.get(deParameterizedIdentifier);
+		if (provider == null) {
+			throw new UnsupportedParsingFormatException(formatIdentifier);
+		}
+		return provider.parse(serializedGraph, formatIdentifier);
+	}
+	
+	/**
+	 * Get a set of supported formats
+	 * @return a set if stings identifying formats (usually the MIME-type)
+	 */
+	public Set<String> getSupportedFormats() {
+		return Collections.unmodifiableSet(providerMap.keySet());
+	}
+
+	/**
+	 * Registers a parsing provider
+	 *
+	 * @param provider the provider to be registered
+	 */
+	public void bindParsingProvider(ParsingProvider provider) {
+		providerList.add(provider);
+		refreshProviderMap();
+	}
+
+	/**
+	 * Unregister a parsing provider
+	 *
+	 * @param provider the provider to be deregistered
+	 */
+	public void unbindParsingProvider(ParsingProvider provider) {
+		providerList.remove(provider);
+		refreshProviderMap();
+	}
+
+	/**
+	 * Update providerMap with the providers in the providerList
+	 * 
+	 */
+	private void refreshProviderMap() {
+		final Map<String, ParsingProvider> newProviderMap = new HashMap<String, ParsingProvider>();
+		for (ParsingProvider provider : providerList) {
+			String[] formatIdentifiers = getFormatIdentifiers(provider);
+			for (String formatIdentifier : formatIdentifiers) {
+				newProviderMap.put(formatIdentifier, provider);
+			}
+		}
+		providerMap = newProviderMap;
+	}
+
+	/**
+	 * Extract format identifiers for a parsing provider
+	 *
+	 * @param provider the provider to be registered
+	 * @return formatIdentifiers
+	 */
+	private String[] getFormatIdentifiers(ParsingProvider parsingProvider) {
+		Class<? extends ParsingProvider> clazz = parsingProvider.getClass();
+		SupportedFormat supportedFormatAnnotation = clazz.getAnnotation(SupportedFormat.class);
+		String[] formatIdentifiers = supportedFormatAnnotation.value();
+		return formatIdentifiers;
+	}
+}