View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.io.Serializable;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.htmlunit.cssparser.parser.CSSErrorHandler;
22  import org.htmlunit.cssparser.parser.CSSParseException;
23  
24  /**
25   * HtmlUnit's default implementation of {@link CSSErrorHandler}, which logs all CSS problems.
26   *
27   * @author Daniel Gredler
28   * @author Ronald Brill
29   * @see SilentCssErrorHandler
30   */
31  public class DefaultCssErrorHandler implements CSSErrorHandler, Serializable {
32      /** Logging support. */
33      private static final Log LOG = LogFactory.getLog(DefaultCssErrorHandler.class);
34  
35      /**
36       * {@inheritDoc}
37       */
38      @Override
39      public void error(final CSSParseException exception) {
40          if (LOG.isWarnEnabled()) {
41              LOG.warn("CSS error: " + buildMessage(exception));
42          }
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void fatalError(final CSSParseException exception) {
50          if (LOG.isWarnEnabled()) {
51              LOG.warn("CSS fatal error: " + buildMessage(exception));
52          }
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void warning(final CSSParseException exception) {
60          if (LOG.isWarnEnabled()) {
61              LOG.warn("CSS warning: " + buildMessage(exception));
62          }
63      }
64  
65      /**
66       * Builds a message for the specified CSS parsing exception.
67       * @param exception the CSS parsing exception to build a message for
68       * @return a message for the specified CSS parsing exception
69       */
70      private static String buildMessage(final CSSParseException exception) {
71          final String uri = exception.getURI();
72          final int line = exception.getLineNumber();
73          final int col = exception.getColumnNumber();
74  
75          if (null == uri) {
76              return "[" + line + ":" + col + "] " + exception.getMessage();
77          }
78          return "'" + uri + "' [" + line + ":" + col + "] " + exception.getMessage();
79      }
80  }