EMMA Coverage Report (generated Thu Nov 26 15:54:18 CST 2009)
[all classes][org.eclipse.pde.api.tools.internal]

COVERAGE SUMMARY FOR SOURCE FILE [SystemApiDescriptionProcessor.java]

nameclass, %method, %block, %line, %
SystemApiDescriptionProcessor.java0%   (0/1)0%   (0/10)0%   (0/441)0%   (0/106)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SystemApiDescriptionProcessor0%   (0/1)0%   (0/10)0%   (0/441)0%   (0/106)
SystemApiDescriptionProcessor (): void 0%   (0/1)0%   (0/3)0%   (0/1)
abort (String, Throwable): void 0%   (0/1)0%   (0/13)0%   (0/2)
annotateApiSettings (IApiDescription, String): void 0%   (0/1)0%   (0/96)0%   (0/26)
annotateDescriptor (IApiDescription, IElementDescriptor, Element): void 0%   (0/1)0%   (0/46)0%   (0/8)
annotateFieldSettings (IApiDescription, IReferenceTypeDescriptor, Element): void 0%   (0/1)0%   (0/41)0%   (0/12)
annotateMethodSettings (IApiDescription, IReferenceTypeDescriptor, Element): ... 0%   (0/1)0%   (0/49)0%   (0/14)
retrieveBooleanElementAttribute (Element, String): boolean 0%   (0/1)0%   (0/14)0%   (0/4)
retrieveElementAttribute (Element, String): int 0%   (0/1)0%   (0/12)0%   (0/4)
retrieveStringElementAttribute (Element, String): String 0%   (0/1)0%   (0/11)0%   (0/4)
serializeComponentXml (File): String 0%   (0/1)0%   (0/156)0%   (0/31)

1/*******************************************************************************
2 * Copyright (c) 2007, 2008 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 v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.pde.api.tools.internal;
12 
13import java.io.File;
14import java.io.FileInputStream;
15import java.io.IOException;
16import java.io.InputStream;
17import java.util.zip.ZipEntry;
18import java.util.zip.ZipFile;
19 
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.Path;
23import org.eclipse.core.runtime.Status;
24import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
25import org.eclipse.pde.api.tools.internal.provisional.Factory;
26import org.eclipse.pde.api.tools.internal.provisional.IApiDescription;
27import org.eclipse.pde.api.tools.internal.provisional.ProfileModifiers;
28import org.eclipse.pde.api.tools.internal.provisional.RestrictionModifiers;
29import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers;
30import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor;
31import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor;
32import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMethodDescriptor;
33import org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor;
34import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor;
35import org.eclipse.pde.api.tools.internal.provisional.scanner.ScannerMessages;
36import org.eclipse.pde.api.tools.internal.util.Util;
37import org.w3c.dom.Element;
38import org.w3c.dom.NodeList;
39 
40/**
41 * Provides tools for scanning/loading/parsing component.xml files.
42 * 
43 * @since 1.0.0
44 */
45public class SystemApiDescriptionProcessor {
46        /**
47         * Constructor
48         * can not be instantiated directly
49         */
50        private SystemApiDescriptionProcessor() {}
51        
52        /**
53         * Parses a component XML into a string. The location may be a jar, directory containing the component.xml file, or 
54         * the component.xml file itself
55         * 
56         * @param location root location of the component.xml file, or the component.xml file itself
57         * @return component XML as a string or <code>null</code> if none
58         * @throws IOException if unable to parse
59         */
60        public static String serializeComponentXml(File location) {
61                if(location.exists()) {
62                        ZipFile jarFile = null;
63                        InputStream stream = null;
64                        try {
65                                String extension = new Path(location.getName()).getFileExtension();
66                                if (extension != null && extension.equals("jar") && location.isFile()) { //$NON-NLS-1$
67                                        jarFile = new ZipFile(location, ZipFile.OPEN_READ);
68                                        ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME);
69                                        if (manifestEntry != null) {
70                                                stream = jarFile.getInputStream(manifestEntry);
71                                        }
72                                } else if(location.isDirectory()) {
73                                        File file = new File(location, IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME);
74                                        if (file.exists()) {
75                                                stream = new FileInputStream(file);
76                                        }
77                                }
78                                else if(location.isFile()) {
79                                        if(location.getName().equals(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME)) {
80                                                stream = new FileInputStream(location);
81                                        }
82                                }
83                                if(stream != null) {
84                                                return new String(Util.getInputStreamAsCharArray(stream, -1, IApiCoreConstants.UTF_8));
85                                }
86                        } catch(IOException e) {
87                                ApiPlugin.log(e);
88                        } finally {
89                                try {
90                                        if (stream != null) {
91                                                stream.close();
92                                        }
93                                } catch (IOException e) {
94                                        ApiPlugin.log(e);
95                                }
96                                try {
97                                        if (jarFile != null) {
98                                                jarFile.close();
99                                        }
100                                } catch (IOException e) {
101                                        ApiPlugin.log(e);
102                                }
103                        }
104                }
105                return null;
106        }
107        /**
108         * Throws an exception with the given message and underlying exception.
109         * 
110         * @param message error message
111         * @param exception underlying exception, or <code>null</code>
112         * @throws CoreException
113         */
114        private static void abort(String message, Throwable exception) throws CoreException {
115                IStatus status = new Status(IStatus.ERROR, ApiPlugin.PLUGIN_ID, message, exception);
116                throw new CoreException(status);
117        }
118 
119        /**
120         * Parses the given xml document (in string format), and annotates the specified 
121         * {@link IApiDescription} with {@link IPackageDescriptor}s, {@link IReferenceTypeDescriptor}s, {@link IMethodDescriptor}s
122         * and {@link IFieldDescriptor}s.
123         * 
124         * @param settings API settings to annotate
125         * @param xml XML used to generate settings
126         * @throws CoreException 
127         */
128        public static void annotateApiSettings(IApiDescription settings, String xml) throws CoreException {
129                Element root = null;
130                try {
131                        root = Util.parseDocument(xml);
132                }
133                catch(CoreException ce) {
134                        abort("Failed to parse API description xml file", ce); //$NON-NLS-1$
135                }
136                if (!root.getNodeName().equals(IApiXmlConstants.ELEMENT_COMPONENT)) {
137                        abort(ScannerMessages.ComponentXMLScanner_0, null); 
138                }
139                NodeList packages = root.getElementsByTagName(IApiXmlConstants.ELEMENT_PACKAGE);
140                NodeList types = null;
141                IPackageDescriptor packdesc = null;
142                Element type = null;
143                for (int i = 0; i < packages.getLength(); i++) {
144                        Element pkg = (Element) packages.item(i);
145                        // package visibility comes from the MANIFEST.MF
146                        String pkgName = pkg.getAttribute(IApiXmlConstants.ATTR_NAME);
147                        packdesc = Factory.packageDescriptor(pkgName);
148                        annotateDescriptor(settings, packdesc, pkg);
149                        types = pkg.getElementsByTagName(IApiXmlConstants.ELEMENT_TYPE);
150                        for (int j = 0; j < types.getLength(); j++) {
151                                type = (Element) types.item(j);
152                                String name = type.getAttribute(IApiXmlConstants.ATTR_NAME);
153                                if (name.length() == 0) {
154                                        abort("Missing type name", null); //$NON-NLS-1$
155                                }
156                                IReferenceTypeDescriptor typedesc = packdesc.getType(name); 
157                                annotateDescriptor(settings, typedesc, type);
158                                annotateMethodSettings(settings, typedesc, type);
159                                annotateFieldSettings(settings, typedesc, type);
160                        }
161                }
162        }
163        
164        /**
165         * Annotates the backing {@link IApiDescription} from the given {@link Element}, by adding the visibility
166         * and restriction attributes to the specified {@link IElementDescriptor}
167         * 
168         * @param settings the settings to annotate
169         * @param descriptor the current descriptor context
170         * @param element the current element to annotate from
171         */
172        private static void annotateDescriptor(IApiDescription settings, IElementDescriptor descriptor, Element element) {
173                settings.setVisibility(descriptor, VisibilityModifiers.API);
174                settings.setRestrictions(descriptor, RestrictionModifiers.NO_RESTRICTIONS);
175                settings.setAddedProfile(descriptor, retrieveElementAttribute(element, IApiXmlConstants.ATTR_ADDED_PROFILE));
176                settings.setRemovedProfile(descriptor, retrieveElementAttribute(element, IApiXmlConstants.ATTR_REMOVED_PROFILE));
177                settings.setSuperclass(descriptor, retrieveStringElementAttribute(element, IApiXmlConstants.ATTR_SUPER_CLASS));
178                settings.setSuperinterfaces(descriptor, retrieveStringElementAttribute(element, IApiXmlConstants.ATTR_SUPER_INTERFACES));
179                settings.setInterface(descriptor, retrieveBooleanElementAttribute(element, IApiXmlConstants.ATTR_INTERFACE));
180        }
181        /**
182         * Tests if the given restriction exists for the given element
183         * and returns an updated restrictions flag.
184         * 
185         * @param element XML element
186         * @param name attribute to test
187         * @param flag bit mask for attribute
188         * @param res flag to combine with 
189         * @return updated flags
190         */
191        private static int retrieveElementAttribute(Element element, String name) {
192                String value = element.getAttribute(name);
193                if (value.length() > 0) {
194                        return Integer.parseInt(value);
195                }
196                return ProfileModifiers.NO_PROFILE_VALUE;
197        }
198        /**
199         * Tests if the given restriction exists for the given element
200         * and returns an updated restrictions flag.
201         * 
202         * @param element XML element
203         * @param name attribute to test
204         * @param flag bit mask for attribute
205         * @param res flag to combine with 
206         * @return updated flags
207         */
208        private static String retrieveStringElementAttribute(Element element, String name) {
209                String value = element.getAttribute(name);
210                if (value.length() > 0) {
211                        return value;
212                }
213                return null;
214        }
215        /**
216         * Tests if the given restriction exists for the given element
217         * and returns an updated restrictions flag.
218         * 
219         * @param element XML element
220         * @param name attribute to test
221         * @param flag bit mask for attribute
222         * @param res flag to combine with 
223         * @return updated flags
224         */
225        private static boolean retrieveBooleanElementAttribute(Element element, String name) {
226                String value = element.getAttribute(name);
227                if (value.length() > 0) {
228                        return Boolean.toString(true).equals(value);
229                }
230                return false;
231        }
232        /**
233         * Annotates the supplied {@link IApiDescription} from all of the field elements
234         * that are direct children of the specified {@link Element}. {@link IFieldDescriptor}s are created
235         * as needed and added as children of the specified {@link IReferenceTypeDescriptor}.
236         * 
237         * @param settings the {@link IApiDescription} to add the new {@link IFieldDescriptor} to
238         * @param typedesc the containing type descriptor for this field
239         * @param type the parent {@link Element}
240         * @throws CoreException
241         */
242        private static void annotateFieldSettings(IApiDescription settings, IReferenceTypeDescriptor typedesc, Element type) throws CoreException {
243                NodeList fields = type.getElementsByTagName(IApiXmlConstants.ELEMENT_FIELD);
244                Element field = null;
245                IFieldDescriptor fielddesc = null;
246                String name = null;
247                for(int i = 0; i < fields.getLength(); i++) {
248                        field = (Element) fields.item(i);
249                        name = field.getAttribute(IApiXmlConstants.ATTR_NAME);
250                        if(name == null) {
251                                abort(ScannerMessages.ComponentXMLScanner_1, null); 
252                        }
253                        fielddesc = typedesc.getField(name);
254                        annotateDescriptor(settings, fielddesc, field);
255                }
256        }
257        
258        /**
259         * Annotates the supplied {@link IApiDescription} from all of the method elements
260         * that are direct children of the specified {@link Element}. {@link IMethodDescriptor}s are created
261         * as needed and added as children of the specified {@link IReferenceTypeDescriptor}.
262         * 
263         * @param settings the {@link IApiDescription} to add the new {@link IMethodDescriptor} to 
264         * @param typedesc the containing type descriptor for this method
265         * @param type the parent {@link Element}
266         * @throws CoreException
267         */
268        private static void annotateMethodSettings(IApiDescription settings, IReferenceTypeDescriptor typedesc, Element type) throws CoreException {
269                NodeList methods = type.getElementsByTagName(IApiXmlConstants.ELEMENT_METHOD);
270                Element method = null;
271                IMethodDescriptor methoddesc = null;
272                String name, signature;
273                for(int i = 0; i < methods.getLength(); i++) {
274                        method = (Element) methods.item(i);
275                        name = method.getAttribute(IApiXmlConstants.ATTR_NAME);
276                        if(name == null) {
277                                abort(ScannerMessages.ComponentXMLScanner_2, null); 
278                        }
279                        signature = method.getAttribute(IApiXmlConstants.ATTR_SIGNATURE);
280                        if(signature == null) {
281                                abort(ScannerMessages.ComponentXMLScanner_3, null); 
282                        }
283                        methoddesc = typedesc.getMethod(name, signature);
284                        annotateDescriptor(settings, methoddesc, method);
285                }
286        }
287}

[all classes][org.eclipse.pde.api.tools.internal]
EMMA 2.0.5312 EclEmma Fix 1 (C) Vladimir Roubtsov