| 1 |
/** |
| 2 |
* Copyright (c) 2006 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 |
* Peter Nehrer IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.test.xml; |
| 12 |
|
| 13 |
import static org.junit.Assert.assertEquals; |
| 14 |
|
| 15 |
import java.io.StringWriter; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.Map; |
| 18 |
|
| 19 |
import org.eclipse.emf.common.util.URI; |
| 20 |
import org.eclipse.emf.ecore.resource.Resource; |
| 21 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 22 |
import org.eclipse.emf.ecore.resource.URIConverter; |
| 23 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 24 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 25 |
import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl; |
| 26 |
import org.eclipse.emf.test.common.TestUtil; |
| 27 |
import org.junit.Before; |
| 28 |
import org.junit.Test; |
| 29 |
|
| 30 |
public class DTDTest |
| 31 |
{ |
| 32 |
static final String BASE_XML_URI = TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/dtd/"; |
| 33 |
static final String LF = System.getProperty("line.separator"); |
| 34 |
|
| 35 |
static final String XML = |
| 36 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LF + |
| 37 |
"<!DOCTYPE Library SYSTEM \"library.dtd\">" + LF + |
| 38 |
"<Library>" + LF + |
| 39 |
" <Book ISBN=\"8763-343-2343\"><Title>Professional JINI</Title><Author>Sing Li</Author><Publisher>Wrox Publications</Publisher><Date_Published>22/10/1999</Date_Published></Book>" + LF + |
| 40 |
"</Library>"; |
| 41 |
|
| 42 |
ResourceSet resourceSet; |
| 43 |
|
| 44 |
@Before |
| 45 |
public void setUp() throws Exception |
| 46 |
{ |
| 47 |
resourceSet = new ResourceSetImpl(); |
| 48 |
resourceSet.getLoadOptions().put(XMLResource.OPTION_RESOLVE_ENTITIES, Boolean.TRUE); |
| 49 |
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put |
| 50 |
(Resource.Factory.Registry.DEFAULT_EXTENSION, new GenericXMLResourceFactoryImpl()); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void testLoadDocumentWithDTD() throws Exception |
| 55 |
{ |
| 56 |
Resource resource = resourceSet.getResource(URI.createFileURI(BASE_XML_URI + "library.xml"), true); |
| 57 |
Map<Object, Object> options = new HashMap<Object, Object>(); |
| 58 |
options.put(XMLResource.OPTION_SAVE_DOCTYPE, Boolean.TRUE); |
| 59 |
StringWriter out = new StringWriter(); |
| 60 |
resource.save(new URIConverter.WriteableOutputStream(out, "UTF-8"), options); |
| 61 |
out.close(); |
| 62 |
assertEquals(XML, out.toString()); |
| 63 |
} |
| 64 |
} |