-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ [New freemarker configuration model, compatibility mode](https://github.com/fugerit-org/fj-bom/issues/38)
- Loading branch information
Showing
34 changed files
with
973 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ain/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfigValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.fugerit.java.doc.freemarker.process; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
import java.io.PrintStream; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.util.Properties; | ||
|
||
import javax.xml.parsers.SAXParser; | ||
import javax.xml.parsers.SAXParserFactory; | ||
import javax.xml.transform.sax.SAXSource; | ||
|
||
import org.fugerit.java.core.io.StreamIO; | ||
import org.fugerit.java.core.lang.helpers.ClassHelper; | ||
import org.fugerit.java.core.lang.helpers.StringUtils; | ||
import org.fugerit.java.core.xml.XMLException; | ||
import org.fugerit.java.core.xml.config.XMLSchemaCatalogConfig; | ||
import org.fugerit.java.core.xml.sax.SAXParseResult; | ||
import org.fugerit.java.core.xml.sax.eh.ResultErrorHandler; | ||
import org.fugerit.java.doc.base.model.DocBase; | ||
import org.fugerit.java.doc.base.parser.DocParserContext; | ||
import org.fugerit.java.doc.base.xml.DocContentHandler; | ||
import org.xml.sax.Attributes; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
import org.xml.sax.helpers.DefaultHandler; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class FreemarkerDocProcessConfigValidator { | ||
|
||
private FreemarkerDocProcessConfigValidator() {} | ||
|
||
private static XMLSchemaCatalogConfig init() { | ||
XMLSchemaCatalogConfig catalog = null; | ||
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "config_fm_xsd/schema-validator-config-freemarker.xml" ) ) { | ||
catalog = XMLSchemaCatalogConfig.loadConfigSchema( is ); | ||
} catch (Exception e) { | ||
throw new RuntimeException( e ); | ||
} | ||
return catalog; | ||
} | ||
|
||
private static final XMLSchemaCatalogConfig SCHEMA_CATALOG = init(); | ||
|
||
public static SAXParseResult validateVersion( Reader xmlData ) throws XMLException { | ||
SAXParseResult result = new SAXParseResult(); | ||
try { | ||
String buffer = StreamIO.readString( xmlData ); | ||
String xsdVersion = getXsdVersion( new StringReader( buffer ) ); | ||
log.info( "xsdVersion -> '{}'", xsdVersion ); | ||
String validateVersion = "current"; | ||
if ( StringUtils.isNotEmpty( xsdVersion ) ) { | ||
validateVersion = "version-"+xsdVersion; | ||
} | ||
log.info( "validateVersion -> '{}'", validateVersion ); | ||
SCHEMA_CATALOG.validateCacheSchema( new ResultErrorHandler( result ) , new SAXSource( new InputSource( new StringReader( buffer ) ) ), validateVersion ); | ||
} catch (Exception e) { | ||
throw new XMLException( e ); | ||
} | ||
return result; | ||
} | ||
|
||
private static String getXsdVersion( Reader xmlReader ) throws XMLException { | ||
String version = null; | ||
try { | ||
SAXParserFactory spf = SAXParserFactory.newInstance(); | ||
spf.setNamespaceAware( true ); | ||
final Properties infos = new Properties(); | ||
SAXParser parser = spf.newSAXParser(); | ||
DefaultHandler versionHandler = new DefaultHandler() { | ||
@Override | ||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { | ||
if ( DocBase.TAG_NAME.equalsIgnoreCase( qName ) || DocBase.TAG_NAME.equalsIgnoreCase( qName ) ) { | ||
Properties props = DocContentHandler.attsToProperties(attributes); | ||
String xsdVersion = DocParserContext.findXsdVersion(props); | ||
infos.setProperty( "xsdVersion" , xsdVersion ); | ||
} | ||
} | ||
}; | ||
parser.parse( new InputSource( xmlReader ) , versionHandler ); | ||
version = infos.getProperty( "xsdVersion" ); | ||
} catch (Exception e) { | ||
throw new XMLException( e ); | ||
} | ||
return version; | ||
} | ||
|
||
public static SAXParseResult validate( Reader xmlData ) throws XMLException { | ||
SAXParseResult result = new SAXParseResult(); | ||
try { | ||
SCHEMA_CATALOG.validateCacheSchema( new ResultErrorHandler( result ) , new SAXSource( new InputSource( xmlData ) ), "current" ); | ||
} catch (Exception e) { | ||
throw new XMLException( e ); | ||
} | ||
return result; | ||
} | ||
|
||
public static void logResult( SAXParseResult result ) throws XMLException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
try ( PrintStream ps = new PrintStream( baos ) ) { | ||
result.printErrorReport( ps ); | ||
log.info( "Validation issues : \n{}", new String( baos.toByteArray() ) ); | ||
} | ||
} | ||
|
||
public static boolean logValidation( Reader xmlData ) throws XMLException { | ||
SAXParseResult result = new SAXParseResult(); | ||
try { | ||
SCHEMA_CATALOG.validateCacheSchema( new ResultErrorHandler( result ) , new SAXSource( new InputSource( xmlData ) ), "current" ); | ||
} catch (Exception e) { | ||
throw new XMLException( e ); | ||
} | ||
if ( result.isTotalSuccess() ) { | ||
log.info( "Validation completed without errors or warning" ); | ||
} else { | ||
logResult( result ); | ||
} | ||
return result.isPartialSuccess(); | ||
} | ||
|
||
} |
Oops, something went wrong.