Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 9d085be4d60529b24b25cde27b41299176d4e529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.dtd.core.internal.emf.util;

import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.wst.dtd.core.internal.DTDCoreMessages;
import org.eclipse.wst.dtd.core.internal.emf.DTDFile;
import org.eclipse.wst.dtd.core.internal.emf.DTDObject;


public class DTDResourceImpl extends ResourceImpl {

	public DTDResourceImpl() {
		super();
	}

	public DTDResourceImpl(String filename) {
		super(URI.createURI(filename));
		// System.out.println(">>>> DTDResourceImpl ctor 1");
	}

	public DTDResourceImpl(URI uri) {
		super(uri);
		// System.out.println(">>>> DTDResourceImpl ctor 2");
	}

	/**
	 * Returns the resolved object for the given URI
	 * {@link URI#fragment fragment}.
	 * <p>
	 * The fragment encoding will typically be that produced by
	 * {@link #getURIFragment getURIFragment}.
	 * </p>
	 * 
	 * @param uriFragment
	 *            the fragment to resolve.
	 * @return the resolved object for the given fragment.
	 * @see #getURIFragment(EObject)
	 * @see org.eclipse.emf.ecore.resource.ResourceSet#getEObject(URI, boolean)
	 * @see org.eclipse.emf.ecore.util.EcoreUtil#resolve(EObject, org.eclipse.emf.ecore.resource.ResourceSet)
	 * @see org.eclipse.emf.ecore.InternalEObject#eObjectForURIFragmentSegment(String)
	 * @throws org.eclipse.emf.common.util.WrappedException
	 *             if a problem occurs navigating the fragment.
	 */
	public EObject getEObject(java.lang.String uriFragment) {
		EList contents = getContents();
		if (contents != null) {
			Iterator i = contents.iterator();
			while (i.hasNext()) {
				Object obj = i.next();
				if (obj instanceof DTDFile) {
					// there should only be one DTDFile in a DTD extent
					EObject result = ((DTDFile) obj).findObject(uriFragment);
					return result;
				}
			}
		}
		System.out.println(">>> DTDKey Error: cannot find object " + uriFragment); //$NON-NLS-1$
		return super.getEObject(uriFragment);
	}

	/**
	 * Returns the URI {@link URI#fragment fragment} that, when passed to
	 * {@link #getEObject getEObject} will return the given object.
	 * <p>
	 * In other words, the following is <code>true</code> for any object
	 * contained by a resource:
	 * 
	 * <pre>
	 * 
	 *    Resource resource = eObject.eResource();
	 *    eObject == resource.getEObject(resource.getURIFragment(eObject))
	 * 
	 * </pre>
	 * 
	 * An implementation may choose to use IDs or to use structured URI
	 * fragments, as supported by
	 * {@link org.eclipse.emf.ecore.InternalEObject#eURIFragmentSegment eURIFragmentSegment}.
	 * </p>
	 * 
	 * @param eObject
	 *            the object to identify.
	 * @return the URI {@link URI#fragment fragment} for the object.
	 * @see #getEObject(String)
	 * @see org.eclipse.emf.ecore.InternalEObject#eURIFragmentSegment(org.eclipse.emf.core.EStructuralFeature,
	 *      EObject)
	 */
	public java.lang.String getURIFragment(EObject eObject) {
		if (eObject instanceof DTDObject) {
			return ((DTDObject) eObject).getPathname();
		}
		return super.getURIFragment(eObject);
	}

	private DTDUtil dtdUtil;

	public DTDUtil getDTDUtil() {
		return dtdUtil;
	}

	public void setDTDUtil(DTDUtil dtdUtil) {
		this.dtdUtil = dtdUtil;
	}

	public void load(Map options) {

		String uriString = getURI().toString();
		try {
			DTDUtil dtdUtil = new DTDUtil();
			dtdUtil.parse(getResourceSet(), uriString);
			dtdUtil.setResource(this);
			setDTDUtil(dtdUtil);
			getContents().add(dtdUtil.getDTDFile());

			// Reset the dirty flag after the resource is loaded
			setModified(false);
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void save(OutputStream os, Object options) throws Exception {
		if (contents != null) {
			Iterator i = contents.iterator();
			while (i.hasNext()) {
				Object obj = i.next();
				if (obj instanceof DTDFile) {
					try {
						DTDPrinter printer = new DTDPrinter(true);
						printer.visitDTDFile((DTDFile) obj);

						String s = printer.getBuffer().toString();
						os.write(s.getBytes());
						// Reset the dirty flag after the resource is saved
						setModified(false);
					}
					catch (Exception e) {
					}
					break;
				}
			}
		}
	}

	public void save(ZipOutputStream zos, String entry, String metaModelName) throws Exception {
		throw new Exception(DTDCoreMessages._EXC_OPERATION_NOT_SUPPORTED); //$NON-NLS-1$
	}
}

Back to the top