001/*
002 * (C) Copyright 2006-2016 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 */
021package org.nuxeo.runtime.deployment.preprocessor.template;
022
023import java.io.BufferedInputStream;
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.IOException;
027import java.io.InputStream;
028import java.net.URL;
029import java.nio.charset.StandardCharsets;
030
031import org.apache.commons.io.IOUtils;
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        try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
044            return parse(in);
045        }
046    }
047
048    public static Template parse(URL url) throws IOException {
049        try (InputStream in = new BufferedInputStream(url.openStream())) {
050            return parse(in);
051        }
052    }
053
054    public static Template parse(InputStream in) throws IOException {
055        String s = IOUtils.toString(in, StandardCharsets.UTF_8);
056        return parse(s.toCharArray());
057    }
058
059    public static Template parse(char[] chars) {
060        Template tpl = new Template();
061        StringBuilder sb = new StringBuilder();
062        StringBuilder name = new StringBuilder();
063
064        // add the begin part
065        tpl.addPart(Template.BEGIN, null);
066
067        boolean marker = false;
068        for (int i = 0; i < chars.length; i++) {
069            char ch = chars[i];
070            switch (ch) {
071            case '%':
072                if (i < chars.length && chars[i + 1] == '{') {
073                    marker = true;
074                    i++;
075                } else {
076                    if (marker) {
077                        name.append(ch);
078                    } else {
079                        sb.append(ch);
080                    }
081                }
082                break;
083            case '}':
084                if (i < chars.length && chars[i + 1] == '%') {
085                    marker = false;
086                    i++;
087                    // create a new Part:
088                    tpl.addPart(name.toString(), sb.toString());
089                    name.setLength(0);
090                    sb.setLength(0);
091                } else {
092                    if (marker) {
093                        name.append(ch);
094                    } else {
095                        sb.append(ch);
096                    }
097                }
098                break;
099            default:
100                if (marker) {
101                    name.append(ch);
102                } else {
103                    sb.append(ch);
104                }
105                break;
106            }
107        }
108
109        // create the END part
110        tpl.addPart(Template.END, sb.toString());
111
112        return tpl;
113    }
114
115}