[Yanel-commits] rev 22848 - public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation

josias at wyona.com josias at wyona.com
Wed Feb 21 22:08:34 CET 2007


Author: josias
Date: 2007-02-21 22:08:33 +0100 (Wed, 21 Feb 2007)
New Revision: 22848

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/AbstractTransformer.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java
Log:
added an abstract sax transformer class and a new implementation of the I18nTransformer which extends the abstract class. this allows to use the i18n transformer in a sax pipeline.

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/AbstractTransformer.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/AbstractTransformer.java	2007-02-21 21:06:56 UTC (rev 22847)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/AbstractTransformer.java	2007-02-21 21:08:33 UTC (rev 22848)
@@ -0,0 +1,121 @@
+package org.wyona.yanel.core.transformation;
+
+import java.io.IOException;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.TransformerHandler;
+
+import org.apache.log4j.Category;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.DefaultHandler;
+
+public abstract class AbstractTransformer extends DefaultHandler implements TransformerHandler {
+
+    private static Category log = Category.getInstance(AbstractTransformer.class);
+    protected EntityResolver entityResolver;
+    protected SAXResult result;
+    protected ContentHandler resultHandler;
+    protected LexicalHandler lexicalHandler;
+
+    public AbstractTransformer() {
+    }
+
+    public void startDocument() throws SAXException {
+        this.resultHandler.startDocument();
+    }
+
+    public void endDocument() throws SAXException {
+        this.resultHandler.endDocument();
+    }
+
+    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
+        this.resultHandler.startElement(namespaceURI, localName, qName, attrs);
+    }
+
+    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
+        this.resultHandler.endElement(namespaceURI, localName, qName);
+    }
+    
+    public void characters(char[] buf, int offset, int len) throws SAXException {
+        this.resultHandler.characters(buf, offset, len);
+    }
+
+    public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
+        try {
+            if (this.entityResolver != null) {
+                    return this.entityResolver.resolveEntity(publicId, systemId);
+            } else {
+                return super.resolveEntity(publicId, systemId);
+            }
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+            throw new SAXException(e);
+        }
+    }
+
+    public void setEntityResolver(EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+    }
+
+    public String getSystemId() {
+        return this.result.getSystemId();
+    }
+
+    public Transformer getTransformer() {
+        // TODO ?
+        return null;
+    }
+
+    public void setResult(Result result) throws IllegalArgumentException {
+        if (!(result instanceof SAXResult)) {
+            throw new IllegalArgumentException("result type not supported: " 
+                    + result.getClass().getName());
+        }
+        this.result = (SAXResult)result;
+        this.resultHandler = this.result.getHandler();
+        if (this.result.getLexicalHandler() != null) {
+            this.lexicalHandler = this.result.getLexicalHandler(); 
+        } else {
+            this.lexicalHandler = (LexicalHandler)this.result.getHandler();
+        }
+    }
+
+    public void setSystemId(String systemId) {
+        this.result.setSystemId(systemId);
+    }
+
+    public void comment(char[] ch, int start, int length) throws SAXException {
+        this.lexicalHandler.comment(ch, start, length);
+    }
+
+    public void endCDATA() throws SAXException {
+        this.lexicalHandler.endCDATA();
+    }
+
+    public void endDTD() throws SAXException {
+        this.lexicalHandler.endDTD();
+    }
+
+    public void endEntity(String name) throws SAXException {
+        this.lexicalHandler.endEntity(name);
+    }
+
+    public void startCDATA() throws SAXException {
+        this.lexicalHandler.startCDATA();
+    }
+
+    public void startDTD(String name, String publicId, String systemId) throws SAXException {
+        this.lexicalHandler.startDTD(name, publicId, systemId);
+    }
+
+    public void startEntity(String name) throws SAXException {
+        this.lexicalHandler.startEntity(name);
+    }
+}


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/AbstractTransformer.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java	2007-02-21 21:06:56 UTC (rev 22847)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java	2007-02-21 21:08:33 UTC (rev 22848)
@@ -0,0 +1,130 @@
+package org.wyona.yanel.core.transformation;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import org.apache.log4j.Category;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+public class I18nTransformer2 extends AbstractTransformer {
+
+    private static Category log = Category.getInstance(I18nTransformer2.class);
+    private Locale currentLocale = null;
+    private ResourceBundle messageBundle = null;
+
+    public static final String NS_URI = "http://apache.org/cocoon/i18n/2.1";
+    
+    public I18nTransformer2(String messages, String language) {
+        currentLocale = new Locale(language);
+        messageBundle = ResourceBundle.getBundle(messages, currentLocale);
+    }
+
+    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
+        String eName = ("".equals(localName)) ? qName : localName;
+        
+        if (isI18nElement(namespaceURI, localName, qName)) {
+            String key = attrs.getValue("key");
+            //String i18nText = messageBundle.getString(key);
+            
+            String i18nText;
+            try {
+                i18nText = messageBundle.getString(key);
+            } catch (Exception e) {
+                log.error("cannot find message for key: " + key);
+                i18nText = key;
+            }
+            
+            log.debug("TAG [key] " + key + " [message]" + i18nText);
+            char[] i18nMessage = i18nText.toCharArray(); 
+            characters(i18nMessage, 0, i18nMessage.length);
+            
+        } else {
+            // translate attributes:
+            
+            AttributesImpl newAttrs = new AttributesImpl();
+            for(int i = 0; i < attrs.getLength(); i++) {
+                String attrUri = attrs.getURI(i);
+                String attrLocalName = attrs.getLocalName(i);
+                String attrQName = attrs.getQName(i);
+                String attrValue = attrs.getValue(i);
+                String attrType = attrs.getType(i);
+                
+                if (attrValue.indexOf("i18n:attr key=") != -1) {
+                    String key = attrValue.substring(14);
+
+                    String i18nValue;
+                    try {
+                        i18nValue = messageBundle.getString(key);
+                    } catch (Exception e) {
+                        log.error("cannot find message for key: " + key);
+                        i18nValue = key;
+                    }
+                    newAttrs.addAttribute(attrUri, attrLocalName, attrQName, attrType, i18nValue);
+                } else {
+                    newAttrs.addAttribute(attrUri, attrLocalName, attrQName, attrType, attrValue);
+                }
+            }
+            super.startElement(namespaceURI, localName, qName, newAttrs);
+            
+            /*
+             // this code would work for cocoon-like i18n attributes:
+            int index = attrs.getIndex(NS_URI, "attr");
+            
+            if (index != -1) {
+                List i18nAttrs = Arrays.asList(attrs.getValue(index).split(" "));
+                AttributesImpl newAttrs = new AttributesImpl();
+                
+                for(int i = 0; i < attrs.getLength(); i++) {
+                    String attrUri = attrs.getURI(i);
+                    String attrLocalName = attrs.getLocalName(i);
+                    String attrQName = attrs.getQName(i);
+                    String attrValue = attrs.getValue(i);
+                    String attrType = attrs.getType(i);
+                    
+                    if (!attrLocalName.equals("attr") || !attrUri.equals(NS_URI)) {
+                        if (i18nAttrs.contains(attrQName)) {
+                            String i18nValue;
+                            try {
+                                i18nValue = messageBundle.getString(attrValue);
+                            } catch (Exception e) {
+                                log.error("cannot find message for key: " + attrValue);
+                                i18nValue = attrValue;
+                            }
+                            newAttrs.addAttribute(attrUri, attrLocalName, attrQName, attrType, i18nValue);
+                        } else {
+                            newAttrs.addAttribute(attrUri, attrLocalName, attrQName, attrType, attrValue);
+                        }
+                    }
+                }
+                super.startElement(namespaceURI, localName, qName, newAttrs);
+            } else {
+                super.startElement(namespaceURI, localName, qName, attrs);
+            }
+            */
+        }
+    }
+
+    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
+        String eName = ("".equals(localName)) ? qName : localName;
+        if (isI18nElement(namespaceURI, localName, qName)) {
+            //ignore these tags
+        } else {
+            super.endElement(namespaceURI, localName, qName);
+        }
+    }
+    
+    protected boolean isI18nElement(String namespaceURI, String localName, String qName) {
+        // TODO: check namespace uri
+        String eName = ("".equals(localName)) ? qName : localName;
+        if ((eName.equals("message")) || (eName.equals("i18n:message"))) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+}


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/I18nTransformer2.java
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the Yanel-commits mailing list