View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.server;
18  
19  import java.io.InputStream;
20  import java.nio.charset.Charset;
21  
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
24  
25  /**
26   * Reads and logs {@link LogEvent}s from an {@link InputStream}.
27   */
28  public class XmlInputStreamLogEventBridge extends InputStreamLogEventBridge {
29  
30      private static final String EVENT_END = "</Event>";
31      private static final String EVENT_START_NS_N = "<Event>";
32      private static final String EVENT_START_NS_Y = "<Event ";
33  
34      public XmlInputStreamLogEventBridge() {
35          this(1024, Charset.defaultCharset());
36      }
37  
38      public XmlInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
39          super(new Log4jXmlObjectMapper(), bufferSize, charset, EVENT_END);
40      }
41  
42      @Override
43      protected int[] getEventIndices(final String text, final int beginIndex) {
44          int start = text.indexOf(EVENT_START_NS_Y, beginIndex);
45          int startLen = EVENT_START_NS_Y.length();
46          if (start < 0) {
47              start = text.indexOf(EVENT_START_NS_N, beginIndex);
48              startLen = EVENT_START_NS_N.length();
49          }
50          final int end = start < 0 ? -1 : text.indexOf(EVENT_END, start + startLen);
51          return new int[] { start, end };
52      }
53  
54  }