Added rebuildXMLFile() to repair non-append xml in mods during patching

This commit is contained in:
Vhati 2013-09-07 04:26:37 -04:00
parent 661efc2dc6
commit 975fe00071
2 changed files with 50 additions and 6 deletions

View file

@ -237,7 +237,21 @@ public class ModPatchThread extends Thread {
moddedItems.add( innerPath );
}
}
else if ( fileName.endsWith( ".xml" ) || fileName.endsWith( ".txt" ) ) {
if ( fileName.endsWith( ".xml" ) ) {
innerPath = checkCase( innerPath, knownPaths, knownPathsLower );
InputStream fixedStream = ModUtilities.rebuildXMLFile( zis, "windows-1252", modFile.getName()+":"+parentPath+fileName );
if ( !moddedItems.contains(innerPath) )
moddedItems.add( innerPath );
else
log.warn( String.format( "Clobbering earlier mods: %s", innerPath ) );
if ( ftlP.contains( innerPath ) )
ftlP.remove( innerPath );
ftlP.add( innerPath, fixedStream );
}
else if ( fileName.endsWith( ".txt" ) ) {
innerPath = checkCase( innerPath, knownPaths, knownPathsLower );
// Normalize line endings for other text files to CR-LF.
@ -245,9 +259,6 @@ public class ModPatchThread extends Thread {
String fixedText = ModUtilities.decodeText( zis, modFile.getName()+":"+parentPath+fileName ).text;
fixedText = Pattern.compile("\n").matcher( fixedText ).replaceAll( "\r\n" );
if ( fileName.endsWith( ".xml" ) )
fixedText = fixedText.replaceAll( "<[?]xml [^>]*?[?]>\n*", "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n" );
InputStream fixedStream = ModUtilities.encodeText( fixedText, "windows-1252", modFile.getName()+":"+parentPath+fileName+" (with new EOL)" );
if ( !moddedItems.contains(innerPath) )

View file

@ -199,8 +199,8 @@ public class ModUtilities {
* which doesn't need closing.
*
* The result will have CR-LF line endings and the desired encoding.
* FTL stubbornly assumes all XML is in windows-1252 encoding, even
* on Linux.
* Note: FTL stubbornly assumes all XML is in windows-1252 encoding,
* even on Linux.
*
* The description arguments identify the streams for log messages.
*
@ -231,6 +231,39 @@ public class ModUtilities {
return result;
}
/**
* Decodes, parses, sloppy prints, and reencodes an XML stream.
* This effectively repairs any XML problems that the sloppy parser
* can tolerate.
*
* The returned stream is a ByteArrayInputStream
* which doesn't need closing.
*
* The result will have CR-LF line endings and the desired encoding.
* Note: FTL stubbornly assumes all XML is in windows-1252 encoding,
* even on Linux.
*
* The description argument identifies the stream for log messages.
*
* @see net.vhati.modmanager.core.XMLPatcher
* @see net.vhati.modmanager.core.SloppyXMLOutputProcessor
*/
public static InputStream rebuildXMLFile( InputStream srcStream, String encoding, String srcDescription ) throws IOException, JDOMException {
Pattern xmlDeclPtn = Pattern.compile( "<[?]xml [^>]*?[?]>\n*" );
String srcText = decodeText( srcStream, srcDescription ).text;
srcText = xmlDeclPtn.matcher(srcText).replaceFirst( "" );
srcText = "<wrapper xmlns:mod='mod' xmlns:mod-append='mod-append' xmlns:mod-overwrite='mod-overwrite'>"+ srcText +"</wrapper>";
Document doc = parseStrictOrSloppyXML( srcText, srcDescription+" (wrapped)" );
StringWriter writer = new StringWriter();
SloppyXMLOutputProcessor.sloppyPrint( doc, writer, encoding );
String resultString = writer.toString();
InputStream result = encodeText( resultString, encoding, srcDescription+" (cleaned)" );
return result;
}
/**
* Returns an XML Document, parsed strictly if possible, or sloppily.