001/*
002 * Copyright (c) 2024 QOS.ch Sarl (Switzerland)
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 *
025 *
026 */
027package ch.qos.logback.tyler.base;
028
029import ch.qos.logback.classic.joran.JoranConfigurator;
030import ch.qos.logback.classic.model.ConfigurationModel;
031import ch.qos.logback.classic.model.ContextNameModel;
032import ch.qos.logback.classic.model.LevelModel;
033import ch.qos.logback.classic.model.LoggerContextListenerModel;
034import ch.qos.logback.classic.model.LoggerModel;
035import ch.qos.logback.classic.model.RootLoggerModel;
036import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
037import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
038import ch.qos.logback.core.Context;
039import ch.qos.logback.core.joran.event.SaxEventRecorder;
040import ch.qos.logback.core.joran.spi.JoranException;
041import ch.qos.logback.core.model.*;
042import ch.qos.logback.core.model.conditional.ElseModel;
043import ch.qos.logback.core.model.conditional.IfModel;
044import ch.qos.logback.core.model.conditional.ThenModel;
045import ch.qos.logback.core.model.processor.DefaultProcessor;
046import ch.qos.logback.core.model.processor.ImportModelHandler;
047import ch.qos.logback.core.util.StatusPrinter2;
048import ch.qos.logback.tyler.base.handler.*;
049import ch.qos.logback.tyler.base.util.StringPrintStream;
050import com.squareup.javapoet.JavaFile;
051import com.squareup.javapoet.MethodSpec;
052import com.squareup.javapoet.TypeSpec;
053import org.xml.sax.InputSource;
054
055import java.io.ByteArrayInputStream;
056import java.io.IOException;
057import java.io.InputStream;
058import java.util.ArrayList;
059import java.util.Arrays;
060import java.util.List;
061
062public class ModelToJava {
063
064
065    final Context context;
066
067    public ModelToJava(Context context) {
068        this.context = context;
069    }
070
071
072    public Model extractModel(String input) throws JoranException {
073        InputStream inputStream = new ByteArrayInputStream(input.getBytes());
074        InputSource inputSource = new InputSource(inputStream);
075        inputSource.setSystemId("UNKNOWN");
076
077        JoranConfigurator joranConfigurator = new JoranConfigurator();
078        joranConfigurator.setContext(context);
079
080        SaxEventRecorder recorder = joranConfigurator.populateSaxEventRecorder(inputSource);
081        Model top = joranConfigurator.buildModelFromSaxEventList(recorder.getSaxEventList());
082        return top;
083    }
084
085    public StringBuffer toJavaAsStringBuffer(Model topModel) throws IOException {
086        TylerModelInterpretationContext tmic = new TylerModelInterpretationContext(context);
087        tmic.setTopModel(topModel);
088
089        LogbackClassicDefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(tmic.getDefaultNestedComponentRegistry());
090
091        DefaultProcessor defaultProcessor = new DefaultProcessor(context, tmic);
092        addModelHandlerAssociations(defaultProcessor);
093
094        defaultProcessor.process(topModel);
095
096
097        tmic.configureMethodSpecBuilder.addStatement("return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY");
098        MethodSpec configureMethodSpec = tmic.configureMethodSpecBuilder.build();
099
100        tmic.tylerConfiguratorTSB.methodSpecs.addFirst(configureMethodSpec);
101
102        for(String methodName: tmic.mapOfMethodSpecBuilders.keySet()) {
103            MethodSpec.Builder methodSpecBuilder = tmic.mapOfMethodSpecBuilders.get(methodName);
104            MethodSpec methodSpec = methodSpecBuilder.build();
105            tmic.tylerConfiguratorTSB.methodSpecs.add(methodSpec);
106        }
107
108        TypeSpec tylerConfiguratorTypeSpec = tmic.tylerConfiguratorTSB.build();
109
110        JavaFile.Builder javaFileBuilder = JavaFile.builder("com.example", tylerConfiguratorTypeSpec);
111
112        tmic.staticImportsList.forEach(sid -> javaFileBuilder.addStaticImport(sid.aClass(), sid.methodName()));
113
114        JavaFile javaFile = javaFileBuilder.indent("  ").build();
115
116        StringBuffer sb = new StringBuffer();
117
118        javaFile.writeTo(sb);
119        return sb;
120    }
121
122    public String toJava(Model topModel) throws IOException {
123        StringBuffer buf = toJavaAsStringBuffer(topModel);
124        return buf.toString();
125    }
126
127    public List<String>  statusToStringList() {
128        List<String> resultList = new ArrayList<>();
129        StatusPrinter2 statusPrinter2 = new StatusPrinter2();
130        StringPrintStream sps = new StringPrintStream(System.out, false);
131        statusPrinter2.setPrintStream(sps);
132        statusPrinter2.print(context);
133        for(String s: sps.stringList) {
134            String[] split = s.split("\n");
135            Arrays.stream(split).forEach(n -> resultList.add("// "+n));
136        }
137        return resultList;
138    }
139
140    private void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
141        defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler::makeInstance);
142        defaultProcessor.addHandler(PropertyModel.class, VariableModelHandler::makeInstance);
143
144        defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
145        defaultProcessor.addHandler(ImportModel.class, ImportModelHandler::makeInstance);
146        defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
147        defaultProcessor.addHandler(InsertFromJNDIModel.class, TylerInsertFromJNDIModelHandler::makeInstance);
148
149        defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance);
150        defaultProcessor.addHandler(ShutdownHookModel.class, ShutdownHookModelHandler::makeInstance);
151        defaultProcessor.addHandler(TimestampModel.class, TimestampModelHandler::makeInstance);
152        defaultProcessor.addHandler(PropertiesConfiguratorModel.class, TylerPropertiesConfiguratorModelHandler::makeInstance);
153        defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
154        defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
155        defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler::makeInstance);
156        defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler::makeInstance);
157        defaultProcessor.addHandler(LevelModel.class, LevelModelHandler::makeInstance);
158        defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);
159
160        defaultProcessor.addHandler(IncludeModel.class, TylerIncludeModelHandler::makeInstance);
161
162        defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);
163        defaultProcessor.addHandler(SequenceNumberGeneratorModel.class, SequenceNumberGeneratorModelHandler::makeInstance);
164
165
166        defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
167        defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
168        defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
169
170    }
171
172}