WeixinErrorUtil.java

  1. package com.foxinmy.weixin4j.util;

  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;

  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.InputSource;
  8. import org.xml.sax.SAXException;
  9. import org.xml.sax.XMLReader;
  10. import org.xml.sax.helpers.DefaultHandler;
  11. import org.xml.sax.helpers.XMLReaderFactory;

  12. import com.foxinmy.weixin4j.http.weixin.WeixinResponse;

  13. /**
  14.  * 接口调用错误获取
  15.  *
  16.  * @author jy
  17.  * @className WeixinErrorUtil
  18.  * @author jinyu(foxinmy@gmail.com)
  19.  * @date 2015年5月12日
  20.  * @see
  21.  * @since JDK 1.6
  22.  */
  23. public final class WeixinErrorUtil {

  24.     private static       byte[]              errorXmlByteArray;
  25.     private final static Map<String, String> errorCacheMap;

  26.     static {
  27.         errorCacheMap = new ConcurrentHashMap<String, String>();
  28.         try {
  29.             errorXmlByteArray = IOUtil.toByteArray(WeixinResponse.class.getResourceAsStream("error.xml"));
  30.         } catch (IOException e) {
  31.             ;
  32.         }
  33.     }

  34.     private static class ErrorTextHandler extends DefaultHandler {

  35.         private final String code;

  36.         public ErrorTextHandler(String code) {
  37.             this.code = code;
  38.         }

  39.         private String  text;
  40.         private boolean codeElement;
  41.         private boolean textElement;
  42.         private boolean findElement;

  43.         @Override
  44.         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  45.             codeElement = qName.equalsIgnoreCase("code");
  46.             textElement = qName.equalsIgnoreCase("text");
  47.         }

  48.         @Override
  49.         public void endElement(String uri, String localName, String qName) throws SAXException {
  50.         }

  51.         @Override
  52.         public void characters(char[] ch, int start, int length) throws SAXException {
  53.             String _text = new String(ch, start, length);
  54.             if (codeElement && _text.equalsIgnoreCase(code)) {
  55.                 findElement = true;
  56.             } else if (textElement && findElement) {
  57.                 text = _text;
  58.                 throw new SAXException("ENOUGH");
  59.             }
  60.         }

  61.         public String getText() {
  62.             return StringUtil.isBlank(text) ? "" : text;
  63.         }
  64.     }

  65.     public static String getText(String code) throws RuntimeException {
  66.         if (StringUtil.isBlank(code)) {
  67.             return "";
  68.         }
  69.         String text = errorCacheMap.get(code);
  70.         if (StringUtil.isBlank(text)) {
  71.             ErrorTextHandler textHandler = new ErrorTextHandler(code);
  72.             try {
  73.                 XMLReader xmlReader = XMLReaderFactory.createXMLReader();
  74.                 xmlReader.setContentHandler(textHandler);
  75.                 xmlReader.parse(new InputSource(new ByteArrayInputStream(errorXmlByteArray)));
  76.                 text = textHandler.getText();
  77.                 errorCacheMap.put(code, text);
  78.             } catch (IOException e) {
  79.                 throw new RuntimeException(e);
  80.             } catch (SAXException e) {
  81.                 text = textHandler.getText();
  82.                 errorCacheMap.put(code, text);
  83.             }
  84.         }
  85.         return text;
  86.     }


  87.     public static void main(String[] args) {
  88.         System.out.println(getText("30002"));
  89.         System.out.println(getText("30002"));
  90.         System.out.println(getText("88010"));
  91.     }
  92. }