### Question:
TtmlHelpers { public static void deepCopyDocument(Document ttml, File target) throws IOException { try { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(" NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node backgroundImage = nl.item(i); URI backgroundImageUri = URI.create(backgroundImage.getNodeValue()); if (!backgroundImageUri.isAbsolute()) { copyLarge(new URI(ttml.getDocumentURI()).resolve(backgroundImageUri).toURL().openStream(), new File(target.toURI().resolve(backgroundImageUri).toURL().getFile())); } } copyLarge(new URI(ttml.getDocumentURI()).toURL().openStream(), target); } catch (XPathExpressionException e) { throw new IOException(e); } catch (URISyntaxException e) { throw new IOException(e); } }  static void main(String[] args); static String[] getAllNamespaces(Document doc); static String toTimeExpression(long ms); static String toTimeExpression(long ms, int frames); static long toTime(String expr); static void pretty(Document document, OutputStream outputStream, int indent); static long getStartTime(Node p); static long getEndTime(Node p); static void deepCopyDocument(Document ttml, File target); static final String SMPTE_TT_NAMESPACE; static final String TTML_NAMESPACE; static final NamespaceContext NAMESPACE_CONTEXT; }### Answer:
@Test public void testDeepCopyDocument() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, URISyntaxException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); Document ttml = db.parse(new InputSource(TtmlHelpersTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/org/mp4parser/muxer/tracks/ttml/tos-chapters-en.xml")); File master = File.createTempFile("TtmlHelpersTest", "testDeepCopyDocument"); master.delete(); master.mkdir(); File f = new File(master, "target"); File targetFile = new File(f, "subs.xml"); TtmlHelpers.deepCopyDocument(ttml, targetFile); Document copy = db.parse(new InputSource(targetFile.getAbsolutePath())); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(" NodeList nl = (NodeList) expr.evaluate(copy, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node backgroundImage = nl.item(i); URI backgroundImageUri = URI.create(backgroundImage.getNodeValue()); File bgImg = new File(new URI(copy.getDocumentURI()).resolve(backgroundImageUri)); Assert.assertTrue(bgImg.exists()); Assert.assertTrue(bgImg.delete()); bgImg.getParentFile().delete(); } Assert.assertTrue(targetFile.delete()); Assert.assertTrue(f.delete()); Assert.assertTrue(master.delete()); }