1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.Writer ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 import com.sun.enterprise.util.LocalStringManager; 29 import java.util.ArrayList ; 30 import java.io.IOException ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.Attributes ; 33 import java.util.logging.Logger ; 34 35 class Localiser extends DefaultHandler 36 { 37 private final LocalStringManager lsm; 38 private final Writer out; 39 private final Logger logger; 40 private final String prefix; 41 42 43 Localiser(final LocalStringManager lsm, final Writer out, final String prefix){ 44 this(lsm, out, null, prefix); 45 } 46 47 Localiser(final LocalStringManager lsm, final Writer out, final Logger logger){ 48 this(lsm, out, logger, null); 49 } 50 51 Localiser(final LocalStringManager lsm, final Writer out){ 52 this(lsm, out, null, null); 53 } 54 55 Localiser(final LocalStringManager lsm, final Writer out, final Logger logger, final String prefix){ 56 this.lsm = lsm; 57 this.out = out; 58 this.logger = logger; 59 this.prefix = (prefix == null ? "" : prefix +"."); 60 } 61 62 public void startElement(final String namespaceURI, 63 final String localName, 64 final String qName, 65 final Attributes atts) 66 throws SAXException { 67 if (localName.equals("location")){ 68 startLocation(); 69 } else if (localName.equals("message")){ 70 startMessage(atts); 71 } else if (localName.equals("param")) { 72 startParam(atts); 73 } 74 } 75 public void characters(char[] ch, 76 int start, 77 int length) 78 throws SAXException { 79 if (null != textBuffer){ 80 textBuffer.append(ch, start, length); 81 } 82 } 83 84 public void endElement(String namespaceURI, 85 String localName, 86 String qName) 87 throws SAXException { 88 if (localName.equals("location")){ 89 endLocation(); 90 } else if (localName.equals("message")){ 91 endMessage(); 92 } else if (localName.equals("param")) { 93 endParam(); 94 } 95 } 96 97 private String message_id; 98 private ArrayList params = new ArrayList (5); 99 private StringBuffer textBuffer; 100 101 private void startLocation(){ 102 textBuffer = new StringBuffer (); 103 } 104 105 private void endLocation() throws SAXException { 106 if (null != textBuffer && textBuffer.length() > 0){ 107 try { 108 textBuffer.append(" "); 109 out.write(textBuffer.toString()); 110 } 111 catch (IOException e){ 112 throw new SAXException (e); 113 } 114 } 115 textBuffer = null; 116 } 117 118 private void startMessage(final Attributes attr) throws SAXException { 119 params.clear(); 120 final String id = attr.getValue("", "id"); 121 if (null == id || id.length() == 0){ 122 throw new SAXException ("id attribute is either null or empty"); 123 } 124 message_id = prefix + id; 125 } 126 127 private void startParam(final Attributes attr) { 128 textBuffer = new StringBuffer (); 129 } 130 131 private void endParam(){ 132 params.add(textBuffer.toString()); 133 textBuffer = null; 134 } 135 136 private void endMessage() throws SAXException { 137 final String msg = lsm.getLocalString(null, message_id, 138 "Internal Error, message id \""+message_id+ "\" not present in localisation file", 139 params.toArray()); 140 message_id = null; 141 142 if (null != logger && msg.startsWith("Internal Error")){ 143 logger.severe(msg); 144 return; 145 } 146 147 try { 148 out.write(msg+"\n"); 149 out.flush(); 150 } 151 catch (IOException e){ 152 throw new SAXException (e); 153 } 154 155 156 } 157 158 } 159 | Popular Tags |