| 1 |
/** |
| 2 |
* Copyright (c) 2002-2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
|
| 12 |
package org.eclipse.emf.test.xml.xmi; |
| 13 |
|
| 14 |
|
| 15 |
import static org.junit.Assert.assertEquals; |
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
import static org.junit.Assert.fail; |
| 18 |
|
| 19 |
import java.io.FileNotFoundException; |
| 20 |
import java.io.StringReader; |
| 21 |
import java.io.StringWriter; |
| 22 |
import java.util.Collections; |
| 23 |
|
| 24 |
import javax.xml.parsers.DocumentBuilder; |
| 25 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 26 |
import javax.xml.transform.Transformer; |
| 27 |
import javax.xml.transform.TransformerFactory; |
| 28 |
import javax.xml.transform.dom.DOMSource; |
| 29 |
import javax.xml.transform.stream.StreamResult; |
| 30 |
|
| 31 |
import org.eclipse.emf.common.util.URI; |
| 32 |
import org.eclipse.emf.ecore.resource.Resource; |
| 33 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 34 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 35 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 36 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 37 |
import org.eclipse.emf.ecore.xmi.XMLResource.ResourceEntityHandler; |
| 38 |
import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl; |
| 39 |
import org.eclipse.emf.ecore.xmi.impl.ResourceEntityHandlerImpl; |
| 40 |
import org.eclipse.emf.test.common.TestUtil; |
| 41 |
import org.eclipse.emf.test.models.library.Book; |
| 42 |
import org.eclipse.emf.test.models.library.Library; |
| 43 |
import org.eclipse.emf.test.models.library.LibraryFactory; |
| 44 |
import org.eclipse.emf.test.models.library.LibraryPackage; |
| 45 |
import org.eclipse.emf.test.models.library.Writer; |
| 46 |
import org.eclipse.emf.test.xml.AllSuites; |
| 47 |
import org.junit.After; |
| 48 |
import org.junit.Before; |
| 49 |
import org.junit.Test; |
| 50 |
import org.w3c.dom.Document; |
| 51 |
import org.xml.sax.InputSource; |
| 52 |
|
| 53 |
|
| 54 |
public class URIHandlerTest |
| 55 |
{ |
| 56 |
final static String BASE_XMI_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xmi/"; |
| 57 |
final static String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/xml/"; |
| 58 |
final static String LF = System.getProperty("line.separator"); |
| 59 |
|
| 60 |
protected ResourceSet resourceSet; |
| 61 |
|
| 62 |
@Before |
| 63 |
public void setUp() throws Exception |
| 64 |
{ |
| 65 |
resourceSet = new ResourceSetImpl(); |
| 66 |
LibraryPackage.eINSTANCE.eClass(); |
| 67 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put |
| 68 |
("*", |
| 69 |
new GenericXMLResourceFactoryImpl() |
| 70 |
{ |
| 71 |
@Override |
| 72 |
public Resource createResource(URI uri) |
| 73 |
{ |
| 74 |
XMLResource result = (XMLResource)super.createResource(uri); |
| 75 |
ResourceEntityHandlerImpl resourceEntityHandler = new ResourceEntityHandlerImpl("_"); |
| 76 |
result.getDefaultSaveOptions().put(XMLResource.OPTION_RESOURCE_ENTITY_HANDLER, resourceEntityHandler); |
| 77 |
result.getDefaultLoadOptions().put(XMLResource.OPTION_RESOURCE_ENTITY_HANDLER, resourceEntityHandler); |
| 78 |
return result; |
| 79 |
} |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
@After |
| 84 |
public void tearDown() throws Exception |
| 85 |
{ |
| 86 |
resourceSet = null; |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void testEntityHandler() throws Exception |
| 91 |
{ |
| 92 |
Resource firstLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "first.library")); |
| 93 |
Library firstLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 94 |
firstLibrary.setName("first"); |
| 95 |
Book firstBook = LibraryFactory.eINSTANCE.createBook(); |
| 96 |
firstBook.setTitle("First Book"); |
| 97 |
firstLibrary.getBooks().add(firstBook); |
| 98 |
firstLibraryResource.getContents().add(firstLibrary); |
| 99 |
StringWriter firstWriter = new StringWriter(); |
| 100 |
firstLibraryResource.save(new URIConverter.WriteableOutputStream(firstWriter, "UTF-8"), null); |
| 101 |
|
| 102 |
Resource secondLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "second.library")); |
| 103 |
Library secondLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 104 |
secondLibrary.setName("second"); |
| 105 |
Book secondBook = LibraryFactory.eINSTANCE.createBook(); |
| 106 |
secondBook.setTitle("Second Book"); |
| 107 |
secondLibrary.getBooks().add(secondBook); |
| 108 |
secondLibraryResource.getContents().add(secondLibrary); |
| 109 |
StringWriter secondWriter = new StringWriter(); |
| 110 |
secondLibraryResource.save(new URIConverter.WriteableOutputStream(secondWriter, "UTF-8"), null); |
| 111 |
|
| 112 |
XMLResource mainLibraryResource = (XMLResource)resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "main.library")); |
| 113 |
Library mainLibrary = LibraryFactory.eINSTANCE.createLibrary(); |
| 114 |
mainLibrary.setName("main"); |
| 115 |
Writer firstAuthor = LibraryFactory.eINSTANCE.createWriter(); |
| 116 |
firstAuthor.setName("First Author"); |
| 117 |
firstAuthor.getBooks().add(firstBook); |
| 118 |
mainLibrary.getWriters().add(firstAuthor); |
| 119 |
Writer secondAuthor = LibraryFactory.eINSTANCE.createWriter(); |
| 120 |
secondAuthor.setName("Second Author"); |
| 121 |
secondAuthor.getBooks().add(secondBook); |
| 122 |
mainLibrary.getWriters().add(secondAuthor); |
| 123 |
mainLibraryResource.getContents().add(mainLibrary); |
| 124 |
|
| 125 |
ResourceEntityHandler resourceEntityHandler = |
| 126 |
(ResourceEntityHandler)(mainLibraryResource).getDefaultLoadOptions().get(XMLResource.OPTION_RESOURCE_ENTITY_HANDLER); |
| 127 |
resourceEntityHandler.handleEntity("first", firstLibraryResource.getURI().toString()); |
| 128 |
resourceEntityHandler.handleEntity("second", secondLibraryResource.getURI().toString()); |
| 129 |
|
| 130 |
StringWriter mainWriter = new StringWriter(); |
| 131 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 132 |
|
| 133 |
mainLibraryResource.setURI(URI.createFileURI(BASE_XML_URI + "/vault/main.library")); |
| 134 |
mainWriter = new StringWriter(); |
| 135 |
mainLibraryResource.save(new URIConverter.WriteableOutputStream(mainWriter, "UTF-8"), null); |
| 136 |
assertEquals |
| 137 |
("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF + |
| 138 |
"<!DOCTYPE org.eclipse.emf.test.models.library:Library [" + LF + |
| 139 |
"<!ENTITY first \"../first.library\">" + LF + |
| 140 |
"<!ENTITY second \"../second.library\">" + LF + |
| 141 |
"]>" + LF + |
| 142 |
"<org.eclipse.emf.test.models.library:Library xmlns:org.eclipse.emf.test.models.library=\"http:///org.eclipse.emf.test.models/Library\"" + LF + |
| 143 |
" name=\"main\">" + LF + |
| 144 |
" <writers name=\"First Author\" books=\"&first;#//@books.0\"/>" + LF + |
| 145 |
" <writers name=\"Second Author\" books=\"&second;#//@books.0\"/>" + LF + |
| 146 |
"</org.eclipse.emf.test.models.library:Library>" + LF, |
| 147 |
mainWriter.toString()); |
| 148 |
|
| 149 |
Document document = mainLibraryResource.save(null, null, null); |
| 150 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 151 |
Transformer transformer = transformerFactory.newTransformer(); |
| 152 |
mainWriter = new StringWriter(); |
| 153 |
transformer.transform(new DOMSource(document), new StreamResult(mainWriter)); |
| 154 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 155 |
factory.setNamespaceAware(true); |
| 156 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 157 |
StringReader stringReader = |
| 158 |
new StringReader |
| 159 |
("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| 160 |
"<org.eclipse.emf.test.models.library:Library xmlns:org.eclipse.emf.test.models.library=\"http:///org.eclipse.emf.test.models/Library\" name=\"main\">" + |
| 161 |
"<writers books=\"../first.library#//@books.0\" name=\"First Author\"/>" + |
| 162 |
"<writers books=\"../second.library#//@books.0\" name=\"Second Author\"/>" + |
| 163 |
"</org.eclipse.emf.test.models.library:Library>"); |
| 164 |
Document document2 = builder.parse(new InputSource(stringReader)); |
| 165 |
CompareXML.compareDocuments |
| 166 |
("", |
| 167 |
document2, |
| 168 |
document); |
| 169 |
|
| 170 |
Resource reloadedMainLibraryResource = resourceSet.createResource(URI.createFileURI(BASE_XML_URI + "/vault/main2.library")); |
| 171 |
reloadedMainLibraryResource.load(new URIConverter.ReadableInputStream(mainWriter.toString()), null); |
| 172 |
Library reloadedMainLibrary = ((Library)reloadedMainLibraryResource.getContents().get(0)); |
| 173 |
Writer reloadedFirstAuthor = reloadedMainLibrary.getWriters().get(0); |
| 174 |
Book reloadedFirstBook = reloadedFirstAuthor.getBooks().get(0); |
| 175 |
assertEquals(firstLibraryResource.getURI(), reloadedFirstBook.eResource().getURI()); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void testEntityResolver() throws Exception |
| 180 |
{ |
| 181 |
// Expecting external entities not to be resolved by default. |
| 182 |
// So the redirection of that external reference to a non-existent location should not result in a file not found exception. |
| 183 |
// |
| 184 |
resourceSet.getURIConverter().getURIMap().put(URI.createFileURI(BASE_XMI_URI + "/root.dtd"), URI.createFileURI(BASE_XMI_URI + "/not-found.dtd")); |
| 185 |
Resource resource = resourceSet.getResource(URI.createFileURI(BASE_XMI_URI + "/test-with-external-entity.xmi"), true); |
| 186 |
|
| 187 |
try |
| 188 |
{ |
| 189 |
// Load it again, but resolving external entities. |
| 190 |
// |
| 191 |
resource.unload(); |
| 192 |
resource.load(Collections.singletonMap(XMLResource.OPTION_RESOLVE_ENTITIES, Boolean.TRUE)); |
| 193 |
|
| 194 |
// We should not get here, we should get an exception. |
| 195 |
// |
| 196 |
fail("Expecting the URI to be loaded to the redirected bogus location, causing a FileNotFoundException"); |
| 197 |
} |
| 198 |
catch (FileNotFoundException ex) |
| 199 |
{ |
| 200 |
// The exception should refer to the redirected URI. |
| 201 |
// |
| 202 |
assertTrue("Expecting the entity reference to be loaded to the redirected bogus location", ex.toString().contains("not-found.dtd")); |
| 203 |
} |
| 204 |
} |
| 205 |
} |