1 package org.codehaus.mojo.jaxb2.javageneration;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import com.sun.tools.xjc.XJCListener;
23 import org.apache.maven.plugin.logging.Log;
24 import org.codehaus.mojo.jaxb2.shared.Validate;
25 import org.xml.sax.SAXParseException;
26
27 /**
28 * Adapter implementation emitting XJC events to a Maven Log.
29 *
30 * @author <a href="mailto:lj@jguru.se">Lennart Jörelid</a>, jGuru Europe AB
31 * @since 2.0
32 */
33 public class XjcLogAdapter extends XJCListener {
34
35 // Internal state
36 private Log log;
37
38 /**
39 * Creates an XjcLogAdapter which emits all XJC events onto the supplied Maven Log.
40 *
41 * @param log A non-null Log logging all inbound XJC events.
42 */
43 public XjcLogAdapter(final Log log) {
44
45 // Check sanity
46 Validate.notNull(log, "log");
47
48 // Assign internal state
49 this.log = log;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void generatedFile(final String fileName, final int current, final int total) {
57 if (log.isDebugEnabled()) {
58 log.debug("Processing file [" + current + "/" + total + "]: " + fileName);
59 }
60 }
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 public void error(final SAXParseException exception) {
67 log.error(getLocation(exception), exception);
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 @Override
74 public void fatalError(final SAXParseException exception) {
75 log.error(getLocation(exception), exception);
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public void warning(final SAXParseException exception) {
83 log.warn(getLocation(exception), exception);
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 public void info(final SAXParseException exception) {
91 log.info(getLocation(exception), exception);
92 }
93
94 //
95 // Private helpers
96 //
97
98 private String getLocation(final SAXParseException e) {
99
100 final String exceptionId = e.getPublicId() == null ? e.getSystemId() : e.getPublicId();
101 return exceptionId + " [" + e.getLineNumber() + "," + e.getColumnNumber() + "] ";
102 }
103 }