/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

/**
 * 
 */
package org.javarosa.core.reference;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.javarosa.core.services.locale.LocaleDataSource;
import org.javarosa.core.services.locale.LocalizationUtils;
import org.javarosa.core.util.OrderedHashtable;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.PrototypeFactory;

/**
 * The ReferenceDataSource is a source of locale data which
 * is located at a location which is defined by a ReferenceURI.
 * 
 * @author Clayton Sims
 * @date Jun 1, 2009 
 *
 */
public class ReferenceDataSource implements LocaleDataSource {
	
	String referenceURI;
	
	/**
	 * NOTE: FOR SERIALIZATION ONLY!
	 */
	public ReferenceDataSource() {
		
	}
	
	/**
	 * Creates a new Data Source for Locale data with the given resource URI.
	 * 
	 * @param resourceURI a URI to the resource file from which data should be loaded
	 * @throws NullPointerException if resourceURI is null
	 */
	public ReferenceDataSource(String referenceURI) {
		if(referenceURI == null) {
			throw new NullPointerException("Reference URI cannot be null when creating a Resource File Data Source");
		}
		this.referenceURI = referenceURI;
	}

	/* (non-Javadoc)
	 * @see org.javarosa.core.services.locale.LocaleDataSource#getLocalizedText()
	 */
	public OrderedHashtable getLocalizedText() {
		try {
			InputStream is = ReferenceManager._().DeriveReference(referenceURI).getStream();
			return LocalizationUtils.parseLocaleInput(is);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("IOException while getting localized text at reference " + referenceURI);
		} catch (InvalidReferenceException e) {
			e.printStackTrace();
			throw new RuntimeException("Invalid Reference! " + referenceURI);
		}
	}

	/* (non-Javadoc)
	 * @see org.javarosa.core.util.externalizable.Externalizable#readExternal(java.io.DataInputStream, org.javarosa.core.util.externalizable.PrototypeFactory)
	 */
	public void readExternal(DataInputStream in, PrototypeFactory pf)
			throws IOException, DeserializationException {
		referenceURI = in.readUTF();
	}

	/* (non-Javadoc)
	 * @see org.javarosa.core.util.externalizable.Externalizable#writeExternal(java.io.DataOutputStream)
	 */
	public void writeExternal(DataOutputStream out) throws IOException {
		out.writeUTF(referenceURI);
	}
}

@@ -763,16 +763,58 @@
 tream;%0A%0A
+import org.javarosa.core.services.Logger;%0A
 import o
@@ -2111,19 +2111,9 @@
 () %7B
-%0D%0A%09%09try %7B%0A%09
+%0A
 %09%09In
@@ -2126,16 +2126,39 @@
 eam is =
+ null;%0D%0A%09%09try %7B%0A%09%09%09is =
  Referen
@@ -2413,24 +2413,48 @@
 referenceURI
+ + %22%5Cn%22 + e.getMessage()
 );%0A%09%09%7D catch
@@ -2579,16 +2579,104 @@
 ceURI);%0A
+%09%09%7D finally %7B%0A%09%09%09if(is != null) %7B%0A%09%09%09%09try %7B is.close(); %7D catch(IOException e) %7B %7D%0A%09%09%09%7D%0A
 %09%09%7D%0D%0A%09%7D%0D

Fix for new Nokia Phones. Was failing to close input stream on reference localization sources.