001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.dataAccess.file; 019 020import org.pegdown.PegDownProcessor; 021 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027 028public class StElementParser { 029 public static HashMap <String, Pattern> elementIdToPattern = new HashMap <String, Pattern>(); 030 031 public static Pattern stElementPattern = Pattern.compile("<st\\-(?<id>[\\w\\-]+)(?<attrs>[^>]*)>(?<innerContent>[\\s\\S]*?)</st\\-\\k<id>>"); 032 public static Pattern dqAttributePattern = Pattern.compile("\\s(?<key>\\w+)=\"(?<value>[^\"]*)\""); 033 public static Pattern sqAttributePattern = Pattern.compile("\\s(?<key>\\w+)='(?<value>[^']*)'"); 034 public static PegDownProcessor pegdownProcessor = new PegDownProcessor(); 035 036 public static String removeTags(String rawContent) { 037 return stElementPattern.matcher(rawContent).replaceAll(""); 038 } 039 040 public static Pattern getPatternForId(String id) 041 { 042 if (elementIdToPattern.containsKey(id)) { 043 Pattern pattern = elementIdToPattern.get(id); 044 if (pattern != null) { 045 return pattern; 046 } 047 } 048 Pattern pattern = Pattern.compile("<st\\-" + id + "[^>]*>[\\s\\S]*</st\\-" + id + ">"); 049 elementIdToPattern.put(id, pattern); 050 return pattern; 051 } 052 053 public static ArrayList<StElement> parseElements(String content, Boolean isMarkdown) { 054 //println("MATCH CONTENT " + content); 055 Matcher matcher = stElementPattern.matcher(content + " \n \n \nabc\n"); 056 ArrayList <StElement> elements = new ArrayList <StElement>(); 057 while (matcher.find()) { 058 StElement ele = new StElement(); 059 elements.add(ele); 060 061 ele.setRawInnerContent(matcher.group("innerContent")); 062 ele.setTagAttributesString(matcher.group("attrs")); 063 ele.setId(matcher.group("id")); 064 if (isMarkdown) { 065 ele.setContent(pegdownProcessor.markdownToHtml(ele.getRawInnerContent())); 066 } else { 067 ele.setContent(ele.getRawInnerContent()); 068 } 069 ArrayList< Matcher > attrMatchers = new ArrayList< Matcher >(); 070 attrMatchers.add(sqAttributePattern.matcher(ele.getTagAttributesString())); 071 attrMatchers.add(dqAttributePattern.matcher(ele.getTagAttributesString())); 072 for (Matcher attrMatcher: attrMatchers) { 073 while (attrMatcher.find()) { 074 String key = attrMatcher.group("key"); 075 String value = attrMatcher.group("value"); 076 ele.getAttributes().put(key, value); 077 if (key == "tag") { 078 ele.setTag(value); 079 080 } 081 } 082 } 083 //println("FOUND ELE " + ele.id); 084 //println("ATTRIBUTES " + ele.attributes.size() + " OBJ " + ele.attributes); 085 //println("TAG " + ele.tag) ; 086 //println("CONTENT " + ele.content); 087 088 } 089 return elements; 090 } 091}