001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.runtime.deployment.preprocessor.template; 023 024import java.io.BufferedInputStream; 025import java.io.File; 026import java.io.FileInputStream; 027import java.io.IOException; 028import java.io.InputStream; 029import java.net.URL; 030 031import org.nuxeo.common.utils.FileUtils; 032 033/** 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036public class TemplateParser { 037 038 // Utility class. 039 private TemplateParser() { 040 } 041 042 public static Template parse(File file) throws IOException { 043 InputStream in = null; 044 try { 045 in = new BufferedInputStream(new FileInputStream(file)); 046 return parse(in); 047 } finally { 048 if (in != null) { 049 in.close(); 050 } 051 } 052 } 053 054 public static Template parse(URL url) throws IOException { 055 InputStream in = null; 056 try { 057 in = new BufferedInputStream(url.openStream()); 058 return parse(in); 059 } finally { 060 if (in != null) { 061 in.close(); 062 } 063 } 064 } 065 066 public static Template parse(InputStream in) throws IOException { 067 String s = FileUtils.read(in); 068 return parse(s.toCharArray()); 069 } 070 071 public static Template parse(char[] chars) { 072 Template tpl = new Template(); 073 StringBuilder buf = new StringBuilder(); 074 StringBuilder name = new StringBuilder(); 075 076 // add the begin part 077 tpl.addPart(Template.BEGIN, null); 078 079 boolean marker = false; 080 for (int i = 0; i < chars.length; i++) { 081 char ch = chars[i]; 082 switch (ch) { 083 case '%': 084 if (i < chars.length && chars[i + 1] == '{') { 085 marker = true; 086 i++; 087 } else { 088 if (marker) { 089 name.append(ch); 090 } else { 091 buf.append(ch); 092 } 093 } 094 break; 095 case '}': 096 if (i < chars.length && chars[i + 1] == '%') { 097 marker = false; 098 i++; 099 // create a new Part: 100 tpl.addPart(name.toString(), buf.toString()); 101 name.setLength(0); 102 buf.setLength(0); 103 } else { 104 if (marker) { 105 name.append(ch); 106 } else { 107 buf.append(ch); 108 } 109 } 110 break; 111 default: 112 if (marker) { 113 name.append(ch); 114 } else { 115 buf.append(ch); 116 } 117 break; 118 } 119 } 120 121 // create the END part 122 tpl.addPart(Template.END, buf.toString()); 123 124 return tpl; 125 } 126 127}