001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net.server;
018
019import java.io.InputStream;
020import java.nio.charset.Charset;
021
022import org.apache.logging.log4j.core.LogEvent;
023import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
024
025/**
026 * Reads and logs {@link LogEvent}s from an {@link InputStream}.
027 */
028public class XmlInputStreamLogEventBridge extends InputStreamLogEventBridge {
029
030    private static final String EVENT_END = "</Event>";
031    private static final String EVENT_START_NS_N = "<Event>";
032    private static final String EVENT_START_NS_Y = "<Event ";
033
034    public XmlInputStreamLogEventBridge() {
035        this(1024, Charset.defaultCharset());
036    }
037
038    public XmlInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
039        super(new Log4jXmlObjectMapper(), bufferSize, charset, EVENT_END);
040    }
041
042    @Override
043    protected int[] getEventIndices(final String text, final int beginIndex) {
044        int start = text.indexOf(EVENT_START_NS_Y, beginIndex);
045        int startLen = EVENT_START_NS_Y.length();
046        if (start < 0) {
047            start = text.indexOf(EVENT_START_NS_N, beginIndex);
048            startLen = EVENT_START_NS_N.length();
049        }
050        final int end = start < 0 ? -1 : text.indexOf(EVENT_END, start + startLen);
051        return new int[] { start, end };
052    }
053
054}