/* * Copyright (c) 2006 Adam Taft * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be included in all copies * or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ package com.adamtaft; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.apache.commons.codec.binary.Base64; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * Simple class which extracts media files bundled in Microsoft's MPF format. * Requires Apache Common's CODEC library, commons-codec.jar, to be in your classpath. * http://jakarta.apache.org/commons/codec/ (uses Base64 decoding) * * Usage: java com.adamtaft.MpfExtract [mpf file] * * Media files will be extracted into the same directory as the original MPF file. */ public class MpfExtractor { public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java MpfExtract [mpf file]"); System.exit(1); } File f = new File(args[0]); InputStream is = new BufferedInputStream(new FileInputStream(f)); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); xpath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { if (prefix.equals("C")) return "urn:schemas-microsoft-com:office:clipgallery"; if (prefix.equals("D")) return "DAV:"; if (prefix.equals("dt")) return "urn:schemas-microsoft-com:datatypes"; return XMLConstants.NULL_NS_URI; } public String getPrefix(String namespaceURI) { throw new UnsupportedOperationException(); } public Iterator getPrefixes(String namespaceURI) { throw new UnsupportedOperationException(); } }); XPathExpression filepathExpression = xpath.compile("./C:filepath/text()"); XPathExpression dataExpression = xpath.compile("./C:contents/text()"); NodeList list = (NodeList) xpath.evaluate("//C:resource", new InputSource(is), XPathConstants.NODESET); Base64 base64 = new Base64(); for (int i=0, n=list.getLength(); i < n; i++) { String filename = filepathExpression.evaluate(list.item(i)).trim(); if (filename.length() < 1) continue; String data = dataExpression.evaluate(list.item(i)).trim(); File clip = new File(f.getParentFile(), filename); OutputStream out = new BufferedOutputStream(new FileOutputStream(clip)); out.write(base64.decode(data.getBytes())); out.close(); System.out.println("Extracting: " + clip.toString()); } is.close(); } }