| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | package org.docx4j.jaxb; |
|---|
| 22 | |
|---|
| 23 | import java.io.IOException; |
|---|
| 24 | |
|---|
| 25 | import javax.xml.bind.ValidationEvent; |
|---|
| 26 | import javax.xml.bind.ValidationEventHandler; |
|---|
| 27 | import javax.xml.bind.ValidationEventLocator; |
|---|
| 28 | import javax.xml.transform.Source; |
|---|
| 29 | import javax.xml.transform.Templates; |
|---|
| 30 | import javax.xml.transform.TransformerConfigurationException; |
|---|
| 31 | import javax.xml.transform.stream.StreamSource; |
|---|
| 32 | |
|---|
| 33 | import org.apache.log4j.Logger; |
|---|
| 34 | import org.docx4j.XmlUtils; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | public class JaxbValidationEventHandler implements |
|---|
| 38 | ValidationEventHandler{ |
|---|
| 39 | |
|---|
| 40 | private static Logger log = Logger.getLogger(JaxbValidationEventHandler.class); |
|---|
| 41 | |
|---|
| 42 | private boolean shouldContinue = true; |
|---|
| 43 | public void setContinue(boolean val) { |
|---|
| 44 | shouldContinue = val; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public final static String UNEXPECTED_MC_ALTERNATE_CONTENT = "unexpected element (uri:\"http://schemas.openxmlformats.org/markup-compatibility/2006\", local:\"AlternateContent\")"; |
|---|
| 48 | |
|---|
| 49 | static Templates mcPreprocessorXslt; |
|---|
| 50 | |
|---|
| 51 | public static Templates getMcPreprocessor() throws IOException, TransformerConfigurationException { |
|---|
| 52 | |
|---|
| 53 | if (mcPreprocessorXslt==null) { |
|---|
| 54 | try { |
|---|
| 55 | Source xsltSource = new StreamSource( |
|---|
| 56 | org.docx4j.utils.ResourceUtils.getResource( |
|---|
| 57 | "org/docx4j/jaxb/mc-preprocessor.xslt")); |
|---|
| 58 | mcPreprocessorXslt = XmlUtils.getTransformerTemplate(xsltSource); |
|---|
| 59 | } catch (IOException e) { |
|---|
| 60 | log.error(e); |
|---|
| 61 | throw(e); |
|---|
| 62 | } catch (TransformerConfigurationException e) { |
|---|
| 63 | log.error(e); |
|---|
| 64 | throw(e); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | return mcPreprocessorXslt; |
|---|
| 69 | |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public boolean handleEvent(ValidationEvent ve) { |
|---|
| 73 | if (ve.getSeverity()==ValidationEvent.FATAL_ERROR |
|---|
| 74 | || ve.getSeverity()==ValidationEvent.ERROR){ |
|---|
| 75 | |
|---|
| 76 | ValidationEventLocator locator = ve.getLocator(); |
|---|
| 77 | |
|---|
| 78 | //print message from validation event |
|---|
| 79 | if (log.isDebugEnabled() || ve.getMessage().length() < 120 ) { |
|---|
| 80 | log.warn( printSeverity(ve) + ": " + ve.getMessage() ); |
|---|
| 81 | } else { |
|---|
| 82 | /* These messages are long, for example: |
|---|
| 83 | * |
|---|
| 84 | * unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"style"). |
|---|
| 85 | * Expected elements are <{http://schemas.openxmlformats.org/wordprocessingml/2006/main}annotationRef>, |
|---|
| 86 | * .... |
|---|
| 87 | * |
|---|
| 88 | * so truncate it. |
|---|
| 89 | */ |
|---|
| 90 | log.warn( printSeverity(ve) + ": " + ve.getMessage().substring(0, 120)); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if (ve.getLinkedException()!=null && log.isDebugEnabled() ) { |
|---|
| 94 | ve.getLinkedException().printStackTrace(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | //output line and column number |
|---|
| 98 | // System.out.println("Column is " + |
|---|
| 99 | // locator.getColumnNumber() + |
|---|
| 100 | // " at line number " + locator.getLineNumber()); |
|---|
| 101 | } else if (ve.getSeverity()==ve.WARNING) { |
|---|
| 102 | log.warn(printSeverity(ve) + "Message is " + ve.getMessage()); |
|---|
| 103 | } |
|---|
| 104 | // JAXB provider should attempt to continue its current operation. |
|---|
| 105 | // (Marshalling, Unmarshalling, Validating) |
|---|
| 106 | log.info("continuing (with possible element/attribute loss)"); |
|---|
| 107 | return shouldContinue; |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public String printSeverity(ValidationEvent ve) { |
|---|
| 112 | |
|---|
| 113 | String errorLevel; |
|---|
| 114 | |
|---|
| 115 | switch (ve.getSeverity()) { |
|---|
| 116 | case ValidationEvent.FATAL_ERROR: { |
|---|
| 117 | // FATAL_ERROR is usually not actually fatal! |
|---|
| 118 | errorLevel="(non)FATAL_ERROR"; |
|---|
| 119 | break; } |
|---|
| 120 | case ValidationEvent.ERROR: { errorLevel="ERROR"; break; } |
|---|
| 121 | case ValidationEvent.WARNING: { errorLevel="WARNING"; break; } |
|---|
| 122 | default: errorLevel = new Integer (ve.getSeverity()).toString() ; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return "[" + errorLevel + "] "; |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | } |
|---|