[Yanel-commits] rev 23623 - in
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes:
. translatable
michi at wyona.com
michi at wyona.com
Mon Apr 9 10:44:03 CEST 2007
Author: michi
Date: 2007-04-09 10:44:02 +0200 (Mon, 09 Apr 2007)
New Revision: 23623
Added:
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManagerRegistry.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationException.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManager.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManagerRegistry.java
Log:
translatable stuff added
Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java 2007-04-09 08:43:09 UTC (rev 23622)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManager.java 2007-04-09 08:44:02 UTC (rev 23623)
@@ -0,0 +1,282 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ * 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.attributes.translatable;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.ResourceManager;
+import org.wyona.yanel.core.Yanel;
+import org.wyona.yanel.core.map.Realm;
+import org.wyona.yarep.core.NoSuchNodeException;
+import org.wyona.yarep.core.Node;
+import org.wyona.yarep.core.RepositoryException;
+import org.apache.commons.id.uuid.UUID;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ *
+ */
+public class DefaultTranslationManager implements TranslationManager {
+
+ private static Category log = Category.getInstance(DefaultTranslationManager.class);
+
+ private HashMap languageVersions; // key is path
+ private HashSet pages;
+ private Node node;
+
+ /**
+ *
+ */
+ public DefaultTranslationManager(Realm realm) throws TranslationException {
+ try {
+ this.pages = new HashSet();
+ this.languageVersions = new HashMap();
+ this.node = realm.getRepository().getNode("/translations.xml");
+ load();
+ } catch (NoSuchNodeException e) {
+ log.info("Realm " + realm.getID() + " contains no translations.xml");
+ } catch (Exception e) {
+ throw new TranslationException(e.getMessage(), e);
+ }
+ }
+
+ public synchronized String getLanguage(Resource resource) throws TranslationException {
+ LanguageVersion langVersion = getLanguageVersion(resource.getPath());
+ if (langVersion != null) {
+ return langVersion.language;
+ }
+ return null;
+ }
+
+ public synchronized String[] getLanguages(Resource resource) throws TranslationException {
+ Page langSet = getPage(resource);
+ if (langSet == null) {
+ return new String[0];
+ }
+ String[] languages = new String[langSet.size()];
+ languages = (String[])langSet.keySet().toArray(languages);
+ return languages;
+ }
+
+ public synchronized Resource getTranslation(Resource resource, String language) throws TranslationException {
+ Page langSet = getPage(resource);
+ if (langSet != null && langSet.containsKey(language)) {
+ LanguageVersion target = (LanguageVersion)langSet.get(language);
+ ResourceManager resourceManager;
+ try {
+ resourceManager = Yanel.getInstance().getResourceManager();
+ return resourceManager.getResource(resource.getRequest(), resource.getResponse(), resource.getRealm(), target.path);
+ } catch (Exception e) {
+ throw new TranslationException(e.getMessage(), e);
+ }
+ }
+ return null;
+ }
+
+ public synchronized void addTranslation(Resource resource, Resource newResource, String language) throws TranslationException {
+ Page langSet = getPage(resource);
+
+ if (langSet == null) {
+ langSet = new Page();
+ this.pages.add(langSet);
+ }
+
+ LanguageVersion newLangVersion = new LanguageVersion(newResource.getPath(), "", language, langSet);
+
+ this.languageVersions.put(newResource.getPath(), newLangVersion);
+ langSet.put(language, newLangVersion);
+
+ save();
+ }
+
+ public synchronized void removeTranslation(Resource resource, String language) throws TranslationException {
+ Page langSet = getPage(resource);
+ langSet.remove(language);
+
+ this.languageVersions.remove(resource.getPath());
+
+ if (langSet.isEmpty()) {
+ this.pages.remove(langSet);
+ }
+
+ save();
+ }
+
+ public synchronized boolean hasTranslation(Resource resource, String language) throws TranslationException {
+ Page langSet = getPage(resource);
+ if (langSet == null) {
+ return false;
+ }
+ return langSet.containsKey(language);
+ }
+
+
+
+ private synchronized LanguageVersion getLanguageVersion(String path) throws TranslationException {
+ return (LanguageVersion)this.languageVersions.get(path);
+ }
+
+ private synchronized Page getPage(Resource resource) throws TranslationException {
+ LanguageVersion langVersion = getLanguageVersion(resource.getPath());
+ if (langVersion == null) {
+ return null;
+ }
+ return langVersion.page;
+ }
+
+ private synchronized void load() throws TranslationException {
+ try {
+ SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
+ InputStream is = this.node.getInputStream();
+ saxParser.parse(is, new TranslationSAXHandler());
+ //debug();
+ } catch (Exception e) {
+ log.error(e, e);
+ throw new TranslationException(e.getMessage(), e);
+ }
+ }
+
+ private synchronized void save() throws TranslationException {
+ try {
+ OutputStream os = this.node.getOutputStream();
+ Writer out = new OutputStreamWriter(os);
+ out.write("<?xml version='1.0'?>\n");
+ out.write("<pages>\n");
+ Iterator pageIter = this.pages.iterator();
+ while (pageIter.hasNext()) {
+ out.write("<page>\n");
+ Page page = (Page)pageIter.next();
+ Iterator iter = page.keySet().iterator();
+ while (iter.hasNext()) {
+ String key = (String)iter.next();
+ LanguageVersion langVersion = (LanguageVersion) page.get(key);
+ out.write("<translation path=\"" + langVersion.path + "\" uuid=\"\" lang=\"" +
+ langVersion.language + "\"/>\n");
+ }
+ out.write("</page>\n");
+ }
+ out.write("</pages>\n");
+ out.close();
+ } catch (Exception e) {
+ log.error(e, e);
+ throw new TranslationException(e.getMessage(), e);
+ }
+ }
+
+ private synchronized void debug() throws TranslationException {
+ try {
+ OutputStream os = System.out;
+ Writer out = new OutputStreamWriter(os);
+ out.write("<?xml version='1.0'?>\n");
+ out.write("<pages>\n");
+ Iterator pageIter = this.pages.iterator();
+ while (pageIter.hasNext()) {
+ out.write("<page>\n");
+ Page page = (Page)pageIter.next();
+ Iterator iter = page.keySet().iterator();
+ while (iter.hasNext()) {
+ String key = (String)iter.next();
+ LanguageVersion langVersion = (LanguageVersion) page.get(key);
+ out.write("<translation path=\"" + langVersion.path + "\" uuid=\"\" lang=\"" +
+ langVersion.language + "\"/>\n");
+ }
+ out.write("</page>\n");
+ }
+ out.write("</pages>\n");
+ out.close();
+ } catch (Exception e) {
+ log.error(e, e);
+ throw new TranslationException(e.getMessage(), e);
+ }
+ }
+
+ private synchronized Page registerTranslation(String path, String uuid, String language, Page page) {
+ if (page == null) {
+ page = new Page();
+ this.pages.add(page);
+ }
+ LanguageVersion langVersion = new LanguageVersion(path, uuid, language, page);
+ page.put(language, langVersion);
+ this.languageVersions.put(path, langVersion);
+ return page;
+ }
+
+ private class LanguageVersion {
+ public LanguageVersion(String path, String uuid, String language, Page page) {
+ this.path = path;
+ this.uuid = uuid;
+ this.language = language;
+ this.page = page;
+ }
+ public String path;
+ public String uuid;
+ public String language;
+ public Page page;
+ }
+
+ /**
+ * A page is a set of language versions.
+ */
+ private class Page extends HashMap {
+ // key is language
+ }
+
+
+ /**
+ * Fills the hashmap with the data provided by the SAX stream.
+ */
+ public class TranslationSAXHandler extends DefaultHandler {
+
+ private Page page;
+ /**
+ * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
+ */
+ public void startElement(String uri, String loc, String raw, Attributes attr)
+ throws SAXException {
+ String name = loc;
+ if (name == null || name.length()==0) {
+ name = raw;
+ }
+ if (name.equals("page")) {
+ this.page = null;
+ } else if (name.equals("translation")) {
+ int pathIndex = attr.getIndex("path");
+ int uuidIndex = attr.getIndex("uuid");
+ int langIndex = attr.getIndex("language");
+ String path = attr.getValue(pathIndex);
+ String uuid = attr.getValue(uuidIndex);
+ String lang = attr.getValue(langIndex);
+
+ this.page = DefaultTranslationManager.this.registerTranslation(path, uuid, lang, this.page);
+ }
+ }
+ }
+}
Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManagerRegistry.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManagerRegistry.java 2007-04-09 08:43:09 UTC (rev 23622)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/DefaultTranslationManagerRegistry.java 2007-04-09 08:44:02 UTC (rev 23623)
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ * 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.attributes.translatable;
+
+import java.util.HashMap;
+
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.map.Realm;
+
+/**
+ *
+ */
+public class DefaultTranslationManagerRegistry implements TranslationManagerRegistry {
+
+ private static Category log = Category.getInstance(DefaultTranslationManagerRegistry.class);
+
+ private HashMap translationManagers; // realm id is the key
+
+ /**
+ *
+ */
+ public DefaultTranslationManagerRegistry() {
+ this.translationManagers = new HashMap();
+ }
+
+ public TranslationManager getTranslationManager(Realm realm) throws TranslationException {
+ if (this.translationManagers.containsKey(realm.getID())) {
+ return (TranslationManager)this.translationManagers.get(realm.getID());
+ }
+ TranslationManager manager = new DefaultTranslationManager(realm);
+ this.translationManagers.put(realm.getID(), manager);
+ return manager;
+ }
+}
Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationException.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationException.java 2007-04-09 08:43:09 UTC (rev 23622)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationException.java 2007-04-09 08:44:02 UTC (rev 23623)
@@ -0,0 +1,36 @@
+package org.wyona.yanel.core.attributes.translatable;
+
+/**
+ *
+ */
+public class TranslationException extends Exception {
+
+ /**
+ *
+ */
+ public TranslationException() {
+ super();
+ }
+
+ /**
+ *
+ */
+ public TranslationException(Throwable t) {
+ super(t);
+ }
+
+ /**
+ *
+ */
+ public TranslationException(String s) {
+ super(s);
+ }
+
+ /**
+ *
+ */
+ public TranslationException(String s, Throwable t) {
+ super(s, t);
+ }
+
+}
Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManager.java 2007-04-09 08:43:09 UTC (rev 23622)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManager.java 2007-04-09 08:44:02 UTC (rev 23623)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ * 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.attributes.translatable;
+
+import org.wyona.yanel.core.Resource;
+
+/**
+ * This is a delegate for the translatable interface.
+ * @see org.wyona.yanel.core.api.attributes.TranslatableV1
+ */
+public interface TranslationManager {
+
+ String getLanguage(Resource resource) throws TranslationException;
+
+ String[] getLanguages(Resource resource) throws TranslationException;
+
+ Resource getTranslation(Resource resource, String language) throws TranslationException;
+
+ void addTranslation(Resource resource, Resource newResource, String language) throws TranslationException;
+
+ void removeTranslation(Resource resource, String language) throws TranslationException;
+
+ boolean hasTranslation(Resource resource, String language) throws TranslationException;
+
+}
Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManagerRegistry.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManagerRegistry.java 2007-04-09 08:43:09 UTC (rev 23622)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/attributes/translatable/TranslationManagerRegistry.java 2007-04-09 08:44:02 UTC (rev 23623)
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ * 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.attributes.translatable;
+
+import org.wyona.yanel.core.map.Realm;
+
+/**
+ *
+ */
+public interface TranslationManagerRegistry {
+
+ public TranslationManager getTranslationManager(Realm realm) throws TranslationException;
+
+}
More information about the Yanel-commits
mailing list