@Test public void test() throws ParserConfigurationException, SAXException, IOException { MapTemplate template = Xml.load(MapTemplate.class); assertTrue(template.getId() == 10000); assertTrue("中文".equals(template.getName())); GameConfig config = Xml.load(GameConfig.class); assertTrue(config.getSid() == 1); assertTrue("192.168.0.92".equals(config.getRedisIp())); }
public static <T> T load(Class<T> klass) { TplFile file = klass.getAnnotation(TplFile.class); if (file == null) { throw new TplConfigurationException("这不是XML格式的配置文件类:" + klass.getName()); } try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file.value())) { ObjectXmlHandler<T> handler = new ObjectXmlHandler<>(klass, file.value()); SAXParserFactory.newInstance().newSAXParser().parse(is, handler); return handler.getResult(); } catch (IOException e) { throw new FileNotFoundException("XML配置文件未找到." + file.value(), e); } catch (SAXException | ParserConfigurationException e) { throw new TplConfigurationException("格式异常:" + klass.getName()); } }
Xml { public static <T> T load(Class<T> klass) { TplFile file = klass.getAnnotation(TplFile.class); if (file == null) { throw new TplConfigurationException("这不是XML格式的配置文件类:" + klass.getName()); } try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file.value())) { ObjectXmlHandler<T> handler = new ObjectXmlHandler<>(klass, file.value()); SAXParserFactory.newInstance().newSAXParser().parse(is, handler); return handler.getResult(); } catch (IOException e) { throw new FileNotFoundException("XML配置文件未找到." + file.value(), e); } catch (SAXException | ParserConfigurationException e) { throw new TplConfigurationException("格式异常:" + klass.getName()); } } }
Xml { public static <T> T load(Class<T> klass) { TplFile file = klass.getAnnotation(TplFile.class); if (file == null) { throw new TplConfigurationException("这不是XML格式的配置文件类:" + klass.getName()); } try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file.value())) { ObjectXmlHandler<T> handler = new ObjectXmlHandler<>(klass, file.value()); SAXParserFactory.newInstance().newSAXParser().parse(is, handler); return handler.getResult(); } catch (IOException e) { throw new FileNotFoundException("XML配置文件未找到." + file.value(), e); } catch (SAXException | ParserConfigurationException e) { throw new TplConfigurationException("格式异常:" + klass.getName()); } }  }
Xml { public static <T> T load(Class<T> klass) { TplFile file = klass.getAnnotation(TplFile.class); if (file == null) { throw new TplConfigurationException("这不是XML格式的配置文件类:" + klass.getName()); } try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file.value())) { ObjectXmlHandler<T> handler = new ObjectXmlHandler<>(klass, file.value()); SAXParserFactory.newInstance().newSAXParser().parse(is, handler); return handler.getResult(); } catch (IOException e) { throw new FileNotFoundException("XML配置文件未找到." + file.value(), e); } catch (SAXException | ParserConfigurationException e) { throw new TplConfigurationException("格式异常:" + klass.getName()); } }  static T load(Class<T> klass); static T load(Class<T> klass, Path path); static List<T> loadAll(String templatePath, Class<T> klass); static List<T> loadAll(String templatePath, String zone, Class<T> klass); }
Xml { public static <T> T load(Class<T> klass) { TplFile file = klass.getAnnotation(TplFile.class); if (file == null) { throw new TplConfigurationException("这不是XML格式的配置文件类:" + klass.getName()); } try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file.value())) { ObjectXmlHandler<T> handler = new ObjectXmlHandler<>(klass, file.value()); SAXParserFactory.newInstance().newSAXParser().parse(is, handler); return handler.getResult(); } catch (IOException e) { throw new FileNotFoundException("XML配置文件未找到." + file.value(), e); } catch (SAXException | ParserConfigurationException e) { throw new TplConfigurationException("格式异常:" + klass.getName()); } }  static T load(Class<T> klass); static T load(Class<T> klass, Path path); static List<T> loadAll(String templatePath, Class<T> klass); static List<T> loadAll(String templatePath, String zone, Class<T> klass);  }