1 11 package org.eclipse.jdt.internal.ui.javadocexport; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 import javax.xml.transform.OutputKeys ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.TransformerException ; 27 import javax.xml.transform.TransformerFactory ; 28 import javax.xml.transform.dom.DOMSource ; 29 import javax.xml.transform.stream.StreamResult ; 30 31 import org.w3c.dom.DOMException ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 35 import org.eclipse.core.resources.IProject; 36 import org.eclipse.core.resources.ResourcesPlugin; 37 38 import org.eclipse.core.runtime.Assert; 39 import org.eclipse.core.runtime.IPath; 40 import org.eclipse.core.runtime.Path; 41 42 43 import org.eclipse.jdt.core.ICompilationUnit; 44 import org.eclipse.jdt.core.IJavaElement; 45 import org.eclipse.jdt.core.IJavaProject; 46 import org.eclipse.jdt.core.IPackageFragment; 47 48 public class JavadocWriter { 49 50 private static final char PATH_SEPARATOR= '/'; 52 private final OutputStream fOutputStream; 53 private final IJavaProject[] fJavaProjects; 54 private final IPath fBasePath; 55 private final String fEncoding; 56 57 63 public JavadocWriter(OutputStream outputStream, String encoding, IPath basePath, IJavaProject[] projects) { 64 Assert.isNotNull(outputStream); 65 Assert.isNotNull(encoding); 66 fOutputStream= new BufferedOutputStream (outputStream); 67 fEncoding= encoding; 68 fBasePath= basePath; 69 fJavaProjects= projects; 70 } 71 72 public void writeXML(JavadocOptionsManager store) throws ParserConfigurationException , TransformerException { 73 74 DocumentBuilder docBuilder= null; 75 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 76 factory.setValidating(false); 77 docBuilder= factory.newDocumentBuilder(); 78 Document document= docBuilder.newDocument(); 79 80 Element project= document.createElement("project"); document.appendChild(project); 83 84 project.setAttribute("default", "javadoc"); 86 Element javadocTarget= document.createElement("target"); project.appendChild(javadocTarget); 88 javadocTarget.setAttribute("name", "javadoc"); 90 Element xmlJavadocDesc= document.createElement("javadoc"); javadocTarget.appendChild(xmlJavadocDesc); 92 93 if (!store.isFromStandard()) 94 xmlWriteDoclet(store, document, xmlJavadocDesc); 95 else 96 xmlWriteJavadocStandardParams(store, document, xmlJavadocDesc); 97 98 99 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 101 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, fEncoding); 103 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); DOMSource source = new DOMSource (document); 106 StreamResult result = new StreamResult (fOutputStream); 107 transformer.transform(source, result); 108 109 } 110 111 private void xmlWriteJavadocStandardParams(JavadocOptionsManager store, Document document, Element xmlJavadocDesc) throws DOMException { 113 114 String destination= getPathString(Path.fromOSString(store.getDestination())); 115 116 xmlJavadocDesc.setAttribute(store.DESTINATION, destination); 117 xmlJavadocDesc.setAttribute(store.VISIBILITY, store.getAccess()); 118 String source= store.getSource(); 119 if (source.length() > 0 && !source.equals("-")) { xmlJavadocDesc.setAttribute(store.SOURCE, store.getSource()); 121 } 122 xmlJavadocDesc.setAttribute(store.USE, booleanToString(store.getBoolean("use"))); xmlJavadocDesc.setAttribute(store.NOTREE, booleanToString(store.getBoolean("notree"))); xmlJavadocDesc.setAttribute(store.NONAVBAR, booleanToString(store.getBoolean("nonavbar"))); xmlJavadocDesc.setAttribute(store.NOINDEX, booleanToString(store.getBoolean("noindex"))); xmlJavadocDesc.setAttribute(store.SPLITINDEX, booleanToString(store.getBoolean("splitindex"))); xmlJavadocDesc.setAttribute(store.AUTHOR, booleanToString(store.getBoolean("author"))); xmlJavadocDesc.setAttribute(store.VERSION, booleanToString(store.getBoolean("version"))); xmlJavadocDesc.setAttribute(store.NODEPRECATEDLIST, booleanToString(store.getBoolean("nodeprecatedlist"))); xmlJavadocDesc.setAttribute(store.NODEPRECATED, booleanToString(store.getBoolean("nodeprecated"))); 132 133 List packages= new ArrayList (); 135 List sourcefiles= new ArrayList (); 136 sortSourceElement(store.getSourceElements(), sourcefiles, packages); 137 if (!packages.isEmpty()) 138 xmlJavadocDesc.setAttribute(store.PACKAGENAMES, toSeparatedList(packages)); 139 140 if (!sourcefiles.isEmpty()) 141 xmlJavadocDesc.setAttribute(store.SOURCEFILES, toSeparatedList(sourcefiles)); 142 143 xmlJavadocDesc.setAttribute(store.SOURCEPATH, getPathString(store.getSourcepath())); 144 xmlJavadocDesc.setAttribute(store.CLASSPATH, getPathString(store.getClasspath())); 145 146 String overview= store.getOverview(); 147 if (overview.length() > 0) 148 xmlJavadocDesc.setAttribute(store.OVERVIEW, overview); 149 150 String styleSheet= store.getStyleSheet(); 151 if (styleSheet.length() > 0) 152 xmlJavadocDesc.setAttribute(store.STYLESHEETFILE, styleSheet); 153 154 String title= store.getTitle(); 155 if (title.length() > 0) 156 xmlJavadocDesc.setAttribute(store.TITLE, title); 157 158 159 String vmArgs= store.getVMParams(); 160 String additionalArgs= store.getAdditionalParams(); 161 if (vmArgs.length() + additionalArgs.length() > 0) { 162 String str= vmArgs + ' ' + additionalArgs; 163 xmlJavadocDesc.setAttribute(store.EXTRAOPTIONS, str); 164 } 165 166 String [] hrefs= store.getHRefs(); 167 for (int i= 0; i < hrefs.length; i++) { 168 Element links= document.createElement("link"); xmlJavadocDesc.appendChild(links); 170 links.setAttribute(store.HREF, hrefs[i]); 171 } 172 } 173 174 private void sortSourceElement(IJavaElement[] iJavaElements, List sourcefiles, List packages) { 175 for (int i= 0; i < iJavaElements.length; i++) { 176 IJavaElement element= iJavaElements[i]; 177 IPath p= element.getResource().getLocation(); 178 if (p == null) 179 continue; 180 181 if (element instanceof ICompilationUnit) { 182 String relative= getPathString(p); 183 sourcefiles.add(relative); 184 } else if (element instanceof IPackageFragment) { 185 packages.add(element.getElementName()); 186 } 187 } 188 } 189 190 private String getPathString(IPath[] paths) { 191 StringBuffer buf= new StringBuffer (); 192 193 for (int i= 0; i < paths.length; i++) { 194 if (buf.length() != 0) { 195 buf.append(File.pathSeparatorChar); 196 } 197 buf.append(getPathString(paths[i])); 198 } 199 200 if (buf.length() == 0) { 201 buf.append('.'); 202 } 203 return buf.toString(); 204 } 205 206 private boolean hasSameDevice(IPath p1, IPath p2) { 207 String dev= p1.getDevice(); 208 if (dev == null) { 209 return p2.getDevice() == null; 210 } 211 return dev.equals(p2.getDevice()); 212 } 213 214 private String getPathString(IPath fullPath) { 216 if (fBasePath == null || !hasSameDevice(fullPath, fBasePath)) { 217 return fullPath.toOSString(); 218 } 219 int matchingSegments= fBasePath.matchingFirstSegments(fullPath); 220 if (fBasePath.segmentCount() == matchingSegments) { 221 return getRelativePath(fullPath, matchingSegments); 222 } 223 for (int i= 0; i < fJavaProjects.length; i++) { 224 IProject proj= fJavaProjects[i].getProject(); 225 IPath projLoc= proj.getLocation(); 226 if (projLoc != null && projLoc.segmentCount() <= matchingSegments && projLoc.isPrefixOf(fullPath)) { 227 return getRelativePath(fullPath, matchingSegments); 228 } 229 } 230 IPath workspaceLoc= ResourcesPlugin.getWorkspace().getRoot().getLocation(); 231 if (workspaceLoc.segmentCount() <= matchingSegments && workspaceLoc.isPrefixOf(fullPath)) { 232 return getRelativePath(fullPath, matchingSegments); 233 } 234 return fullPath.toOSString(); 235 } 236 237 private String getRelativePath(IPath fullPath, int matchingSegments) { 238 StringBuffer res= new StringBuffer (); 239 int backSegments= fBasePath.segmentCount() - matchingSegments; 240 while (backSegments > 0) { 241 res.append(".."); res.append(PATH_SEPARATOR); 243 backSegments--; 244 } 245 int segCount= fullPath.segmentCount(); 246 for (int i= matchingSegments; i < segCount; i++) { 247 if (i > matchingSegments) { 248 res.append(PATH_SEPARATOR); 249 } 250 res.append(fullPath.segment(i)); 251 } 252 return res.toString(); 253 } 254 255 private void xmlWriteDoclet(JavadocOptionsManager store, Document document, Element xmlJavadocDesc) throws DOMException { 256 257 List packages= new ArrayList (); 259 List sourcefiles= new ArrayList (); 260 sortSourceElement(store.getSourceElements(), sourcefiles, packages); 261 if (!packages.isEmpty()) 262 xmlJavadocDesc.setAttribute(store.PACKAGENAMES, toSeparatedList(packages)); 263 264 if (!sourcefiles.isEmpty()) 265 xmlJavadocDesc.setAttribute(store.SOURCEFILES, toSeparatedList(sourcefiles)); 266 267 xmlJavadocDesc.setAttribute(store.SOURCEPATH, getPathString(store.getSourcepath())); 268 xmlJavadocDesc.setAttribute(store.CLASSPATH, getPathString(store.getClasspath())); 269 xmlJavadocDesc.setAttribute(store.VISIBILITY, store.getAccess()); 270 271 Element doclet= document.createElement("doclet"); xmlJavadocDesc.appendChild(doclet); 273 doclet.setAttribute(store.NAME, store.getDocletName()); 274 doclet.setAttribute(store.PATH, store.getDocletPath()); 275 276 String str= store.getOverview(); 277 if (str.length() > 0) 278 xmlJavadocDesc.setAttribute(store.OVERVIEW, str); 279 280 str= store.getAdditionalParams(); 281 if (str.length() > 0) 282 xmlJavadocDesc.setAttribute(store.EXTRAOPTIONS, str); 283 284 } 285 286 private String toSeparatedList(List packages) { 287 StringBuffer buf= new StringBuffer (); 288 Iterator iter= packages.iterator(); 289 int nAdded= 0; 290 while (iter.hasNext()) { 291 if (nAdded > 0) { 292 buf.append(','); 293 } 294 nAdded++; 295 String curr= (String ) iter.next(); 296 buf.append(curr); 297 } 298 return buf.toString(); 299 } 300 301 private String booleanToString(boolean bool) { 302 if (bool) 303 return "true"; else 305 return "false"; } 307 308 public void close() throws IOException { 309 if (fOutputStream != null) { 310 fOutputStream.close(); 311 } 312 } 313 314 } 315 | Popular Tags |