TemplateConfigParser.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
  3. *
  4. * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.jpress.template;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.xml.parsers.SAXParser;
  21. import javax.xml.parsers.SAXParserFactory;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.SAXException;
  24. import org.xml.sax.helpers.DefaultHandler;
  25. import com.jfinal.log.Log;
  26. import io.jpress.utils.FileUtils;
  27. import io.jpress.utils.StringUtils;
  28. public class TemplateConfigParser extends DefaultHandler {
  29. private static final Log log = Log.getLog(TemplateConfigParser.class);
  30. final Template template;
  31. private TplModule cModule;
  32. private TplTaxonomyType cTaxonomy;
  33. private List<TplModule> modules;
  34. private List<TplTaxonomyType> cTaxonomys;
  35. private List<TplMetadata> cModuleMetadatas;
  36. private List<TplMetadata> cTaxonomyMetadatas;
  37. private List<Thumbnail> thumbnails;
  38. private boolean taxonomyStarted = false;
  39. private String value = null;
  40. public TemplateConfigParser() {
  41. template = new Template();
  42. modules = new ArrayList<TplModule>();
  43. thumbnails = new ArrayList<Thumbnail>();
  44. }
  45. public Template parser(File templateFolder) {
  46. File configFile = new File(templateFolder, "tpl_config.xml");
  47. try {
  48. SAXParserFactory factory = SAXParserFactory.newInstance();
  49. SAXParser parser = factory.newSAXParser();
  50. parser.parse(configFile, this);
  51. } catch (Exception e) {
  52. log.warn("ConfigParser parser exception", e);
  53. }
  54. File screenshotFile = new File(templateFolder, "tpl_screenshot.png");
  55. if (screenshotFile.exists()) {
  56. template.setScreenshot(FileUtils.removeRootPath(screenshotFile.getAbsolutePath()));
  57. }
  58. String path = FileUtils.removeRootPath(templateFolder.getAbsolutePath());
  59. template.setPath(path.replace("\\", "/"));
  60. return template;
  61. }
  62. @Override
  63. public void endDocument() throws SAXException {
  64. template.setModules(modules);
  65. template.setThumbnails(thumbnails);
  66. }
  67. @Override
  68. public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
  69. if ("module".equalsIgnoreCase(qName)) {
  70. cModule = new TplModule();
  71. cModule.setAddTitle(attrs.getValue("add"));
  72. cModule.setName(attrs.getValue("name"));
  73. cModule.setListTitle(attrs.getValue("list"));
  74. cModule.setTitle(attrs.getValue("title"));
  75. cModule.setCommentTitle(attrs.getValue("comment"));
  76. cModule.setOrders(attrs.getValue("orders"));
  77. cModule.setIconClass(attrs.getValue("iconClass"));
  78. cTaxonomys = new ArrayList<TplTaxonomyType>();
  79. cModuleMetadatas = new ArrayList<TplMetadata>();
  80. }
  81. if ("taxonomy".equalsIgnoreCase(qName)) {
  82. cTaxonomy = new TplTaxonomyType(cModule);
  83. cTaxonomy.setName(attrs.getValue("name"));
  84. cTaxonomy.setTitle(attrs.getValue("title"));
  85. cTaxonomy.setFormType(attrs.getValue("formType"));
  86. cTaxonomyMetadatas = new ArrayList<TplMetadata>();
  87. taxonomyStarted = true;
  88. }
  89. if ("metadata".equalsIgnoreCase(qName)) {
  90. TplMetadata meta = new TplMetadata();
  91. meta.setName(attrs.getValue("name"));
  92. meta.setTitle(attrs.getValue("title"));
  93. meta.setDescription(attrs.getValue("description"));
  94. meta.setPlaceholder(attrs.getValue("placeholder"));
  95. String dataType = attrs.getValue("placeholder");
  96. if (StringUtils.isNotBlank(dataType)) {
  97. meta.setDataType(dataType);
  98. }
  99. if (taxonomyStarted) {
  100. cTaxonomyMetadatas.add(meta);
  101. } else {
  102. cModuleMetadatas.add(meta);
  103. }
  104. }
  105. if ("thumbnail".equalsIgnoreCase(qName)) {
  106. Thumbnail tb = new Thumbnail();
  107. tb.setName(attrs.getValue("name"));
  108. tb.setSize(attrs.getValue("size"));
  109. thumbnails.add(tb);
  110. }
  111. }
  112. @Override
  113. public void endElement(String uri, String localName, String qName) throws SAXException {
  114. if ("module".equalsIgnoreCase(qName)) {
  115. cModule.setTaxonomyTypes(cTaxonomys);
  116. cModule.setMetadatas(cModuleMetadatas);
  117. modules.add(cModule);
  118. }
  119. else if ("taxonomy".equalsIgnoreCase(qName)) {
  120. cTaxonomy.setMetadatas(cTaxonomyMetadatas);
  121. cTaxonomys.add(cTaxonomy);
  122. taxonomyStarted = false;
  123. }
  124. else if ("title".equalsIgnoreCase(qName)) {
  125. template.setTitle(value);
  126. }
  127. else if ("id".equalsIgnoreCase(qName)) {
  128. template.setId(value);
  129. }
  130. else if ("description".equalsIgnoreCase(qName)) {
  131. template.setDescription(value);
  132. }
  133. else if ("author".equalsIgnoreCase(qName)) {
  134. template.setAuthor(value);
  135. }
  136. else if ("authorWebsite".equalsIgnoreCase(qName)) {
  137. template.setAuthorWebsite(value);
  138. }
  139. else if ("version".equalsIgnoreCase(qName)) {
  140. template.setVersion(value);
  141. }
  142. else if ("renderType".equalsIgnoreCase(qName)) {
  143. template.setRenderType(value);
  144. }
  145. else if ("versionCode".equalsIgnoreCase(qName)) {
  146. int versionCode = 0;
  147. try {
  148. versionCode = Integer.parseInt(value.trim());
  149. } catch (Exception e) {
  150. }
  151. template.setVersionCode(versionCode);
  152. }
  153. else if ("updateUrl".equalsIgnoreCase(qName)) {
  154. template.setUpdateUrl(value);
  155. }
  156. }
  157. @Override
  158. public void characters(char[] ch, int start, int length) throws SAXException {
  159. value = new String(ch, start, length);
  160. }
  161. }