| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
package org.xmlhammer.gui.stylesheetvalidator; |
| 23 |
|
|
| 24 |
|
import java.io.IOException; |
| 25 |
|
import java.net.URI; |
| 26 |
|
import java.util.ArrayList; |
| 27 |
|
|
| 28 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 29 |
|
import javax.xml.transform.TransformerException; |
| 30 |
|
import javax.xml.transform.TransformerFactory; |
| 31 |
|
import javax.xml.transform.stream.StreamSource; |
| 32 |
|
|
| 33 |
|
import org.apache.log4j.Logger; |
| 34 |
|
import org.bounce.util.URIUtils; |
| 35 |
|
import org.xml.sax.SAXException; |
| 36 |
|
import org.xmlhammer.DefaultErrorHandler; |
| 37 |
|
import org.xmlhammer.Module; |
| 38 |
|
import org.xmlhammer.ResultModel; |
| 39 |
|
import org.xmlhammer.DefaultErrorHandler.Error; |
| 40 |
|
import org.xmlhammer.DefaultErrorHandler.Fatal; |
| 41 |
|
import org.xmlhammer.DefaultErrorHandler.Problem; |
| 42 |
|
import org.xmlhammer.DefaultErrorHandler.Warning; |
| 43 |
|
import org.xmlhammer.gui.status.StatusModel; |
| 44 |
|
import org.xmlhammer.gui.status.ValidationStatusModel; |
| 45 |
|
import org.xmlhammer.model.preferences.Preferences; |
| 46 |
|
import org.xmlhammer.model.project.Project; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
public class StylesheetValidatorModule extends Module { |
| 55 |
88 |
private TransformerFactory factory = null; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger) { |
| 66 |
44 |
this(preferences, project, logger, true); |
| 67 |
44 |
} |
| 68 |
|
|
| 69 |
|
public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger, boolean logSettings) { |
| 70 |
88 |
super(preferences, project, logger, logSettings); |
| 71 |
|
|
| 72 |
88 |
factory = getTransformerFactory(); |
| 73 |
88 |
} |
| 74 |
|
|
| 75 |
|
@Override |
| 76 |
|
public void execute(StatusModel status, ResultModel result, URI uri) { |
| 77 |
88 |
if ( uri != null) { |
| 78 |
88 |
getLogger().info( "validate stylesheet: "+URIUtils.toString(uri)); |
| 79 |
|
|
| 80 |
88 |
DefaultErrorHandler errorHandler = new DefaultErrorHandler(uri); |
| 81 |
|
|
| 82 |
|
try { |
| 83 |
88 |
factory.setErrorListener(errorHandler); |
| 84 |
88 |
factory.newTransformer(new StreamSource(uri.toString())); |
| 85 |
44 |
} catch (TransformerException e) { |
| 86 |
|
try { |
| 87 |
44 |
errorHandler.error(e); |
| 88 |
44 |
} catch (Exception x) {} |
| 89 |
44 |
} |
| 90 |
|
|
| 91 |
88 |
ArrayList<Problem> list = errorHandler.getProblems(); |
| 92 |
88 |
if ( list.size() > 0) { |
| 93 |
44 |
if (status instanceof ValidationStatusModel) { |
| 94 |
22 |
((ValidationStatusModel)status).setValid(false); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
44 |
for ( Problem problem : list) { |
| 98 |
176 |
if ( problem instanceof Warning) { |
| 99 |
0 |
logWarning(uri, (TransformerException)problem.getException()); |
| 100 |
|
|
| 101 |
0 |
if (result != null) { |
| 102 |
0 |
result.addWarning( uri, (TransformerException)problem.getException()); |
| 103 |
0 |
} |
| 104 |
176 |
} else if ( problem instanceof Error) { |
| 105 |
132 |
logError(uri, (TransformerException)problem.getException()); |
| 106 |
|
|
| 107 |
132 |
if (result != null) { |
| 108 |
66 |
result.addError( uri, (TransformerException)problem.getException()); |
| 109 |
66 |
} |
| 110 |
44 |
} else if ( problem instanceof Fatal) { |
| 111 |
44 |
logFatal(uri, problem.getException()); |
| 112 |
|
|
| 113 |
44 |
if (result != null) { |
| 114 |
22 |
Exception exception = problem.getException(); |
| 115 |
|
|
| 116 |
22 |
if ( exception instanceof IOException) { |
| 117 |
0 |
result.addFatal( uri, (IOException)problem.getException()); |
| 118 |
0 |
} else { |
| 119 |
22 |
result.addFatal( uri, (TransformerException)problem.getException()); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
176 |
} |
| 124 |
44 |
} else { |
| 125 |
|
|
| 126 |
44 |
getLogger().info( "\t[valid] Valid Stylesheet"); |
| 127 |
|
|
| 128 |
44 |
if (result != null) { |
| 129 |
22 |
result.addValid(uri); |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
|
} |
| 133 |
88 |
} |
| 134 |
|
} |