1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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 | *******************************************************************************/ |
11 | package org.eclipse.pde.api.tools.internal.search; |
12 | |
13 | import java.io.BufferedWriter; |
14 | import java.io.File; |
15 | import java.io.FileNotFoundException; |
16 | import java.io.FileWriter; |
17 | import java.io.IOException; |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import javax.xml.parsers.DocumentBuilder; |
22 | import javax.xml.parsers.DocumentBuilderFactory; |
23 | import javax.xml.parsers.FactoryConfigurationError; |
24 | import javax.xml.parsers.ParserConfigurationException; |
25 | |
26 | import org.eclipse.core.runtime.CoreException; |
27 | import org.eclipse.pde.api.tools.internal.IApiXmlConstants; |
28 | import org.eclipse.pde.api.tools.internal.builder.Reference; |
29 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
30 | import org.eclipse.pde.api.tools.internal.provisional.builder.IReference; |
31 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
32 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
33 | import org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchReporter; |
34 | import org.eclipse.pde.api.tools.internal.provisional.search.IMetadata; |
35 | import org.eclipse.pde.api.tools.internal.util.Util; |
36 | import org.w3c.dom.Document; |
37 | import org.w3c.dom.Element; |
38 | import org.xml.sax.helpers.DefaultHandler; |
39 | |
40 | /** |
41 | * Search reporter that outputs results to an XML file |
42 | * |
43 | * @since 1.0.1 |
44 | */ |
45 | public class XmlSearchReporter implements IApiSearchReporter { |
46 | |
47 | private String fLocation = null; |
48 | private DocumentBuilder parser = null; |
49 | private boolean debug = false; |
50 | |
51 | /** |
52 | * Constructor |
53 | * |
54 | * @param location the absolute path in the local file system to the folder to write the reports to |
55 | * @param debug if debugging infos should be written out to the console |
56 | */ |
57 | public XmlSearchReporter(String location, boolean debug) { |
58 | fLocation = location; |
59 | this.debug = debug; |
60 | try { |
61 | parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
62 | parser.setErrorHandler(new DefaultHandler()); |
63 | } |
64 | catch(FactoryConfigurationError fce) { |
65 | ApiPlugin.log(fce); |
66 | } |
67 | catch (ParserConfigurationException pce) { |
68 | ApiPlugin.log(pce); |
69 | } |
70 | } |
71 | |
72 | /* (non-Javadoc) |
73 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchReporter#reportResults(org.eclipse.pde.api.tools.internal.provisional.builder.IReference[]) |
74 | */ |
75 | public void reportResults(IApiElement element, final IReference[] references) { |
76 | if(fLocation != null) { |
77 | XmlReferenceDescriptorWriter writer = new XmlReferenceDescriptorWriter(fLocation); |
78 | List descriptors = new ArrayList(references.length + 1); |
79 | for (int i = 0; i < references.length; i++) { |
80 | Reference reference = (Reference) references[i]; |
81 | try { |
82 | descriptors.add(reference.getReferenceDescriptor()); |
83 | } catch (CoreException e) { |
84 | ApiPlugin.log(e.getStatus()); |
85 | } |
86 | } |
87 | writer.writeReferences((IReferenceDescriptor[]) descriptors.toArray(new IReferenceDescriptor[descriptors.size()])); |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * Resolves the id to use for the component in the mapping |
93 | * @param component |
94 | * @return the id to use for the component in the mapping, includes the version information as well |
95 | * @throws CoreException |
96 | */ |
97 | String getId(IApiComponent component) throws CoreException { |
98 | StringBuffer buffer = new StringBuffer(); |
99 | buffer.append(component.getId()).append(" ").append('(').append(component.getVersion()).append(')'); //$NON-NLS-1$ |
100 | return buffer.toString(); |
101 | } |
102 | |
103 | /** |
104 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchReporter#reportNotSearched(org.eclipse.pde.api.tools.internal.provisional.model.IApiElement[]) |
105 | */ |
106 | public void reportNotSearched(IApiElement[] elements) { |
107 | if(elements == null) { |
108 | return; |
109 | } |
110 | BufferedWriter writer = null; |
111 | try { |
112 | if(this.debug) { |
113 | System.out.println("Writing file for projects that were not searched..."); //$NON-NLS-1$ |
114 | } |
115 | File rootfile = new File(fLocation); |
116 | if(!rootfile.exists()) { |
117 | rootfile.mkdirs(); |
118 | } |
119 | File file = new File(rootfile, "not_searched.xml"); //$NON-NLS-1$ |
120 | if(!file.exists()) { |
121 | file.createNewFile(); |
122 | } |
123 | Document doc = Util.newDocument(); |
124 | Element root = doc.createElement(IApiXmlConstants.ELEMENT_COMPONENTS); |
125 | doc.appendChild(root); |
126 | Element comp = null; |
127 | SkippedComponent component = null; |
128 | for(int i = 0; i < elements.length; i++) { |
129 | component = (SkippedComponent)elements[i]; |
130 | comp = doc.createElement(IApiXmlConstants.ELEMENT_COMPONENT); |
131 | comp.setAttribute(IApiXmlConstants.ATTR_ID, component.getComponentId()); |
132 | comp.setAttribute(IApiXmlConstants.ATTR_VERSION, component.getVersion()); |
133 | comp.setAttribute(IApiXmlConstants.SKIPPED_DETAILS, component.getErrorDetails()); |
134 | root.appendChild(comp); |
135 | } |
136 | writer = new BufferedWriter(new FileWriter(file)); |
137 | writer.write(Util.serializeDocument(doc)); |
138 | writer.flush(); |
139 | } |
140 | catch(FileNotFoundException fnfe) {} |
141 | catch(IOException ioe) {} |
142 | catch(CoreException ce) {} |
143 | finally { |
144 | try { |
145 | if(writer != null) { |
146 | writer.close(); |
147 | } |
148 | } |
149 | catch (IOException e) {} |
150 | } |
151 | } |
152 | |
153 | /* (non-Javadoc) |
154 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchReporter#reportMetadata(org.eclipse.pde.api.tools.internal.provisional.search.IMetadata) |
155 | */ |
156 | public void reportMetadata(IMetadata data) { |
157 | if(data == null) { |
158 | return; |
159 | } |
160 | try { |
161 | if(this.debug) { |
162 | System.out.println("Writing file for projects that were not searched..."); //$NON-NLS-1$ |
163 | } |
164 | File rootfile = new File(fLocation); |
165 | if(!rootfile.exists()) { |
166 | rootfile.mkdirs(); |
167 | } |
168 | File file = new File(rootfile, "meta.xml"); //$NON-NLS-1$ |
169 | if(!file.exists()) { |
170 | file.createNewFile(); |
171 | } |
172 | data.serializeToFile(file); |
173 | } |
174 | catch(FileNotFoundException fnfe) { |
175 | ApiPlugin.log(fnfe); |
176 | } |
177 | catch(IOException ioe) { |
178 | ApiPlugin.log(ioe); |
179 | } |
180 | catch (CoreException ce) { |
181 | ApiPlugin.log(ce); |
182 | } |
183 | } |
184 | } |