All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jlibs.util.logging.LogHeaderDefinition Maven / Gradle / Ivy

/**
 * Copyright 2015 Santhosh Kumar Tekuri
 *
 * The JLibs authors license this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package jlibs.util.logging;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @author Santhosh Kumar T
 */
public class LogHeaderDefinition{
    public final Pattern pattern;
    public final String groupNames[];

    public LogHeaderDefinition(Pattern pattern, String[] groupNames){
        this.pattern = pattern;
        this.groupNames = groupNames;
    }

    public static LogHeaderDefinition parse(File file) throws Exception{
        Element root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file).getDocumentElement();
        NodeList nodeList = root.getElementsByTagName("pattern");
        if(nodeList.getLength()==0)
            throw new IllegalArgumentException("[LogHeader] pattern element is missing");
        String p = nodeList.item(0).getTextContent();
        Pattern pattern;
        try{
            pattern = Pattern.compile(p);
        }catch(PatternSyntaxException ex){
            throw new RuntimeException("[LogHeader] invalid regex in pattern element", ex);
        }
        String groupNames[] = new String[pattern.matcher("").groupCount()+1];
        groupNames[0] = "header";
        NodeList groupList = root.getElementsByTagName("field");
        for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy