The following document contains the results of PMD's CPD 4.3.
File | Line |
---|---|
de\smartics\maven\exceptions\scan\SdocCodeBuilder.java | 50 |
de\smartics\maven\exceptions\sdoc\SdocCodeBuilder.java | 50 |
public class SdocCodeBuilder { // NOPMD // ********************************* Fields ********************************* // --- constants ------------------------------------------------------------ /** * Reference to the logger for this class. */ private static final Log LOG = LogFactory.getLog(SdocCodeBuilder.class); /** * The URI of the XML schema instance. * <p> * The value of this constant is {@value}. * </p> */ private static final String XML_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance"; /** * The URI of the code doctype. * <p> * The value of this constant is {@value}. * </p> */ private static final String CODE_URI = "http://www.smartics.de/project/process/implementation/appcode"; // --- members -------------------------------------------------------------- /** * The renderer to use for processing Javadoc comments. */ private final JavadocRenderer renderer; /** * The mapping of a code instance to a bundle. */ private final BundleMapper mapper; /** * The resource bundle with labels to render. */ private final ResourceBundle bundle; /** * The empty document to write to. */ private final Document document; /** * The code representation of the runtime environement. */ private final String codeRepresentation; /** * Static information about the code to write to the document. */ private final ExceptionCodeReportItem codeContainer; // ****************************** Initializer ******************************* // ****************************** Constructors ****************************** /** * Default constructor. * * @param renderer the renderer to use for processing Javadoc comments. * @param mapper the mapping of a code instance to a bundle. * @param bundle the resource bundle with labels to render. * @param document the empty document to write to. * @param codeRepresentation the code representation of the runtime * environment. * @param codeContainer the value for codeContainer. */ SdocCodeBuilder(final JavadocRenderer renderer, final BundleMapper mapper, final ResourceBundle bundle, final Document document, final String codeRepresentation, final ExceptionCodeReportItem codeContainer) { this.renderer = renderer; this.mapper = mapper; this.bundle = bundle; this.document = document; this.codeRepresentation = codeRepresentation; this.codeContainer = codeContainer; } // ****************************** Inner Classes ***************************** // ********************************* Methods ******************************** // --- init ----------------------------------------------------------------- // --- get&set -------------------------------------------------------------- // --- business ------------------------------------------------------------- /** * Writes the content to the document. */ public Document writeDocumentContent() { final JavaField fieldDoc = codeContainer.getJavadoc(); final JavaClass classDoc = codeContainer.getTypeJavadoc(); final TagInfo tagInfo = new TagInfo(classDoc, fieldDoc); final Element docRoot = createDocRoot(); createContentElement("id", codeRepresentation, docRoot); createContentElement("name", tagInfo.getName(), docRoot); // NOPMD createContentElement("type", "error", docRoot); final String componentId = codeContainer.getCode().getComponentId(); createContentElement("component", componentId, docRoot); createImplementationElement(classDoc.getFullyQualifiedName(), fieldDoc.getName(), docRoot); createContentElement("category", tagInfo.getCategory(), docRoot); createContentElement("subcategory", tagInfo.getSubcategory(), docRoot); createContentElement("sort-key", tagInfo.getSortKey(), docRoot); createContentElement("parents", "parent", tagInfo.getParents(), docRoot); createContentElement("tags", "tag", tagInfo.getTags(), docRoot); renderShortDescription(tagInfo, fieldDoc, docRoot); renderJavadocContent("package-description", docRoot, createJavadoc(classDoc)); renderJavadocContent("description", docRoot, createJavadoc(fieldDoc)); // NOPMD createContentElement("notes", tagInfo.getNotes(), docRoot); renderMessages(docRoot); return document; } private void renderShortDescription(final TagInfo tagInfo, final JavaField fieldDoc, final Element docRoot) throws DOMException { String text = tagInfo.getShortDescription(); if (StringUtils.isBlank(text)) { final String comment = fieldDoc.getComment(); if (comment != null) { final int index = comment.indexOf('.'); // FIXME: Too crude, dot my be // escaped. final String firstSentence = comment.substring(0, index); text = firstSentence; } } if (StringUtils.isNotBlank(text)) { renderJavadocContent("short-description", docRoot, text); } } private void createImplementationElement(final String implementingClassName, final String implementingFieldName, final Element docRoot) { final Element implementation = document.createElement("implementation"); docRoot.appendChild(implementation); createContentElement("class", implementingClassName, implementation); createContentElement("field", implementingFieldName, implementation); } private void createContentElement(final String groupGi, final String gi, final List<String> contents, final Element docRoot) { if (!contents.isEmpty()) { final Element group = document.createElement(groupGi); docRoot.appendChild(group); for (final String content : contents) { createContentElement(gi, content, group); } } } private void renderJavadocContent(final String gi, final Element parentElement, final String content) throws DOMException { if (StringUtils.isNotBlank(content)) { final Element element = document.createElement(gi); parentElement.appendChild(element); // TODO: Is there a way to write non-validating??? final DomCopyReader copy = new DomCopyReader(document, element); try { copy.copy(content); } catch (final XMLStreamException e) { if (LOG.isWarnEnabled()) { LOG.warn("Cannot parse Javdoc content as XML. Using it verbatim: " + content); } createContentElement("description", content, parentElement); } } } private Element createContentElement(final String gi, final String content, final Element parent) { if (content != null) { final Element element = document.createElement(gi); final Text text = document.createTextNode(content); element.appendChild(text); parent.appendChild(element); return element; } return null; } private String createJavadoc(final JavaAnnotatedElement doc) { final String javadoc = this.renderer.render(doc); return StringUtils.isNotBlank(javadoc) ? javadoc : " "; } private Element createDocRoot() throws DOMException { final Element docRoot = document.createElement("appcode"); docRoot.setAttribute("xmlns:xsi", XML_SCHEMA_INSTANCE); docRoot.setAttribute("xmlns", CODE_URI); docRoot.setAttribute("xsi:schemaLocation", CODE_URI + ' ' + CODE_URI); document.appendChild(docRoot); return docRoot; } private void renderMessages(final Element parent) { if (mapper != null) { final Code code = codeContainer.getCode(); final Class<?> codeClass = code.getClass(); try { final ResourceBundle messageBundle = mapper.getBundle(codeClass, this.bundle.getLocale()); if (messageBundle != null) { addMessages(parent, codeContainer, messageBundle); } } catch (final MissingResourceException e) { if (LOG.isWarnEnabled()) { LOG.warn("Cannot load resource bundle for code class '" + codeClass + "'.", e); } } } } private void addMessages(final Element parent, final ExceptionCodeReportItem codeContainer, final ResourceBundle bundle) throws DOMException { final Element messages = document.createElement("messages"); addMessages(codeContainer, bundle, messages); if (messages.hasChildNodes()) { parent.appendChild(messages); } } private void addMessages(final ExceptionCodeReportItem codeContainer, final ResourceBundle bundle, final Element messages) throws DOMException { for (MessageType type : MessageType.values()) { final String keyPrefix = codeContainer.getCode().getCode(); final String key = type.createKey(keyPrefix); addText(codeContainer, bundle, messages, type, key); } } private void addText(final ExceptionCodeReportItem codeContainer, final ResourceBundle bundle, final Element messages, final MessageType messageType, final String key) throws DOMException { try { final String text = bundle.getString(key); if (StringUtils.isNotBlank(text)) { final String typeId = getTypeId(messageType); final Element message = document.createElement("message"); createContentElement("name", renderTypeId(typeId), message); final Element textElement = createContentElement("text", text, message); final Map<String, String> placeHolderMap = addPlaceHolders(messageType, codeContainer, message, bundle, text); if (!placeHolderMap.isEmpty()) { replaceText(placeHolderMap, textElement); } messages.appendChild(message); } } catch (final MissingResourceException e) { // ok, skip } } private String renderTypeId(final String typeId) { try { final String message = bundle.getString("label." + typeId); return message; } catch (final Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("No translation for '" + typeId + "' found in resource bundle."); } return typeId; } } private void replaceText(final Map<String, String> placeHolderMap, final Element textElement) { final Text textNode = (Text) textElement.getFirstChild(); String text = textNode.getTextContent(); for (Map.Entry<String, String> entry : placeHolderMap.entrySet()) { final String key = entry.getKey(); final String value = '{' + entry.getValue() + '}'; text = StringUtils.replace(text, key, value); } text = StringUtils.replace(text, "''", "'"); textNode.setTextContent(text); } private Map<String, String> addPlaceHolders(final MessageType messageType, final ExceptionCodeReportItem codeContainer, final Element parent, final ResourceBundle bundle, final String messageText) throws DOMException { final Element placeholders = document.createElement("placeholders"); final Map<String, String> placeHolderMap = addPlaceholder(messageType, codeContainer.getPlaceHolderInfo(), bundle, placeholders, messageText); if (placeholders.hasChildNodes()) { parent.appendChild(placeholders); } return placeHolderMap; } private Map<String, String> addPlaceholder(final MessageType messageType, final PlaceHolderInfo placeHolderInfo, final ResourceBundle bundle, final Element placeholders, final String messageText) { final Map<String, String> placeHolderMap = new HashMap<String, String>(); for (PlaceHolderDesc desc : placeHolderInfo .getPlaceHolderDescs(messageType)) { final Element placeholder = document.createElement("placeholder"); final int index = desc.getPlaceHolderIndex(); final String placeHolderString = '{' + String.valueOf(index) + '}'; if (messageText.contains(placeHolderString)) { final String path = desc.getOgnlPath(); final String name = desc.getParamName() + (path != null ? ':' + desc.getOgnlPath() : ""); final String description = desc.getDescription(); createContentElement("name", name, placeholder); createContentElement("description", description, placeholder); placeholders.appendChild(placeholder); placeHolderMap.put(placeHolderString, name); } } return placeHolderMap; } private String getTypeId(final MessageType type) { final String id = type.getMessageKeySuffix(); if (StringUtils.EMPTY.equals(id)) { return "summary"; } else { return id.substring(1); } } // --- object basics -------------------------------------------------------- } |
File | Line |
---|---|
de\smartics\maven\exceptions\scan\TagInfo.java | 31 |
de\smartics\maven\exceptions\sdoc\TagInfo.java | 31 |
class TagInfo { // ********************************* Fields ********************************* // --- constants ------------------------------------------------------------ // --- members -------------------------------------------------------------- /** * The information extracted from the all level Javadoc tag * <code>sdoc.category</code>. */ private final String category; /** * The information extracted from the all level Javadoc tag * <code>sdoc.subcategory</code>. */ private final String subcategory; /** * The information extracted from the all level Javadoc tag * <code>sdoc.tag</code> that may appear multiple times. */ private final List<String> tags = new ArrayList<String>(); /** * The information extracted from the all level Javadoc tag * <code>sdoc.parent</code> that may appear multiple times. */ private final List<String> parents = new ArrayList<String>(); /** * The information extracted from the field level Javadoc tag * <code>sdoc.name</code>. */ private final String name; /** * The information extracted from the field level Javadoc tag * <code>sdoc.sort-key</code>. */ private final String sortKey; /** * The information extracted from the field level Javadoc tag * <code>sdoc.short-description</code>. */ private final String shortDescription; /** * The information extracted from the field level Javadoc tag * <code>sdoc.notes</code>. */ private final String notes; // ****************************** Initializer ******************************* // ****************************** Constructors ****************************** /** * Default constructor. */ public TagInfo(final JavaClass classDoc, final JavaField fieldDoc) { this.category = extractSingleValue(classDoc, fieldDoc, "sdoc.category"); this.subcategory = extractSingleValue(classDoc, fieldDoc, "sdoc.subcategory"); extractMultiValue(classDoc, "sdoc.tag", tags); extractMultiValue(fieldDoc, "sdoc.tag", tags); extractMultiValue(classDoc, "sdoc.parent", parents); extractMultiValue(fieldDoc, "sdoc.parent", parents); this.name = extractSingleValue(fieldDoc, "sdoc.name"); this.sortKey = extractSingleValue(fieldDoc, "sdoc.sort-key"); this.shortDescription = extractSingleValue(fieldDoc, "sdoc.short-description"); this.notes = extractSingleValue(fieldDoc, "sdoc.notes"); } // ****************************** Inner Classes ***************************** // ********************************* Methods ******************************** // --- init ----------------------------------------------------------------- private String extractSingleValue(final JavaAnnotatedElement doc1, final JavaAnnotatedElement doc2, final String name) { String value = extractSingleValue(doc2, name); if (StringUtils.isBlank(value)) { value = extractSingleValue(doc1, name); } return value; } private String extractSingleValue(final JavaAnnotatedElement doc, final String name) { final List<DocletTag> tags = doc.getTagsByName(name); if (!tags.isEmpty()) { final String text = tags.get(0).getValue(); if (StringUtils.isNotBlank(text)) { return text; } } return null; } private void extractMultiValue(final JavaAnnotatedElement doc, final String name, final List<String> list) { final List<DocletTag> tags = doc.getTagsByName(name); for (final DocletTag tag : tags) { final String text = tag.getValue(); if (StringUtils.isNotBlank(text)) { list.add(text); } } } // --- get&set -------------------------------------------------------------- /** * Returns the information extracted from the type level Javadoc tag * <code>sdoc.category</code>. * * @return the information extracted from the type level Javadoc tag * <code>sdoc</code>. */ public String getCategory() { return category; } /** * Returns the information extracted from the type level Javadoc tag * <code>sdoc.subcategory</code>. * * @return the information extracted from the type level Javadoc tag * <code>sdoc</code>. */ public String getSubcategory() { return subcategory; } /** * Returns the information extracted from the all level Javadoc tag * <code>sdoc.tag</code> that may appear multiple times. * * @return the information extracted from the all level Javadoc tag * <code>sdoc</code>. */ public List<String> getTags() { return tags; } /** * Returns the information extracted from the all level Javadoc tag * <code>sdoc.parent</code> that may appear multiple times. * * @return the information extracted from the all level Javadoc tag * <code>sdoc</code>. */ public List<String> getParents() { return parents; } /** * Returns the information extracted from the field level Javadoc tag * <code>sdoc.name</code>. * * @return the information extracted from the field level Javadoc tag * <code>sdoc</code>. */ public String getName() { return name; } /** * Returns the information extracted from the field level Javadoc tag * <code>sdoc.sort-key</code>. * * @return the information extracted from the field level Javadoc tag * <code>sdoc</code>. */ public String getSortKey() { return sortKey; } /** * Returns the information extracted from the field level Javadoc tag * <code>sdoc.short-description</code>. * * @return the information extracted from the field level Javadoc tag * <code>sdoc</code>. */ public String getShortDescription() { return shortDescription; } /** * Returns the information extracted from the field level Javadoc tag * <code>sdoc.notes</code>. * * @return the information extracted from the field level Javadoc tag * <code>sdoc</code>. */ public String getNotes() { return notes; } // --- business ------------------------------------------------------------- // --- object basics -------------------------------------------------------- } |
File | Line |
---|---|
de\smartics\maven\exceptions\scan\SdocCodeReportGenerator.java | 47 |
de\smartics\maven\exceptions\sdoc\SdocCodeReportGenerator.java | 47 |
public class SdocCodeReportGenerator extends AbstractReportGenerator<File> { // ********************************* Fields ********************************* // --- constants ------------------------------------------------------------ /** * Reference to the logger for this class. */ private static final Log LOG = LogFactory .getLog(SdocCodeReportGenerator.class); // --- members -------------------------------------------------------------- /** * The builder for XML documents. */ private final DocumentBuilder builder; // ****************************** Initializer ******************************* // ****************************** Constructors ****************************** /** * Default constructor. * * @param renderer the renderer used to map Javadoc comments to the report's * output format. * @throws ParserConfigurationException if creating XML infrstructure fails. */ public SdocCodeReportGenerator() throws ParserConfigurationException { super(new JavadocRenderer(new HtmlRendererFactory())); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); } // ****************************** Inner Classes ***************************** // ********************************* Methods ******************************** // --- init ----------------------------------------------------------------- // --- get&set -------------------------------------------------------------- // --- business ------------------------------------------------------------- @Override protected void writeContent(final File output, final ProjectConfiguration<File> config, final StoredExceptionCodesReport reportData) throws Exception { final List<ExceptionCodeReportItem> items = reportData.getItems(); for (final ExceptionCodeReportItem codeContainer : items) { writeReportToFile(output, config, codeContainer); } } private void writeReportToFile(final File output, final ProjectConfiguration<File> config, final ExceptionCodeReportItem codeContainer) throws MojoExecutionException, FileNotFoundException, Exception { final File file = createFile(output, codeContainer); if (LOG.isDebugEnabled()) { LOG.debug("Creating report file '" + file.getAbsolutePath() + "'."); } OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); writeReportElementInfo(out, config, codeContainer); } finally { IOUtils.closeQuietly(out); } } private static File createFile(final File baseDir, final ExceptionCodeReportItem codeContainer) throws MojoExecutionException { final Code code = codeContainer.getCode(); final String pathName = MojoIoUtils.normalizeFileName(code.getComponentId()); final String fileName = MojoIoUtils.normalizeFileName(code.getCode()) + ".xml"; final File dir = new File(baseDir, pathName); MojoIoUtils.provideMojoDirectory(dir); final File file = new File(dir, fileName); return file; } protected void writeReportElementInfo(final OutputStream reportOut, final ProjectConfiguration<File> config, final ExceptionCodeReportItem codeContainer) throws Exception { final Code codeInstance = codeContainer.getCode(); final String codeRepresentation = String.valueOf(codeInstance); final Document document = createDocument(); final SdocCodeBuilder builder = new SdocCodeBuilder(this.renderer, config.getBundleMapper(), config.getBundle(), document, codeRepresentation, codeContainer); builder.writeDocumentContent(); MojoIoUtils.serialize(document, reportOut); } private Document createDocument() { final Document document = builder.newDocument(); return document; } // --- object basics -------------------------------------------------------- } |
File | Line |
---|---|
de\smartics\maven\exceptions\report\CodeSortedSinkReportGenerator.java | 71 |
de\smartics\maven\exceptions\report\ComponentCodeSortedSinkReportGenerator.java | 72 |
Collections.sort(items, new MixedCodeComparator()); return items; } @Override protected String getCurrentSelection(final ExceptionCodeReportItem item) { final Code code = item.getCode(); if (code instanceof NumberCode) { final NumberCode numberCode = (NumberCode) code; final String codeMajorNumber = String.valueOf(numberCode.getMajorNumber()); return codeMajorNumber; } return code.getComponentId(); } @Override protected boolean hasSectionChanged(String currentSection, final ExceptionCodeReportItem item) { final Code code = item.getCode(); boolean headerChanged; if (code instanceof NumberCode) { final NumberCode numberCode = (NumberCode) code; final String codeMajorNumber = String.valueOf(numberCode.getMajorNumber()); headerChanged = !ObjectUtils.equals(currentSection, codeMajorNumber); } else { final String componentId = code.getComponentId(); headerChanged = !ObjectUtils.equals(currentSection, componentId); } return headerChanged; } @Override protected String getCodeTitle(final String headLine, final ExceptionCodeReportItem item) { final String title; if (headLine != null) { title = headLine; } else { final String code = item.getCode().getCode(); |
File | Line |
---|---|
de\smartics\maven\exceptions\conf\DefaultJavadocProjectConfiguration.java | 177 |
de\smartics\maven\exceptions\conf\DefaultProjectConfiguration.java | 287 |
public Builder(final String projectName) { this.projectName = projectName; } // ***************************** Inner Classes **************************** // ******************************** Methods ******************************* // --- init --------------------------------------------------------------- // --- get&set ------------------------------------------------------------ /** * Sets the classpath used by the tool. * * @param toolClassPath the classpath used by the tool. */ public void setToolClassPath(final List<String> toolClassPath) { this.toolClassPath = toolClassPath; } /** * Sets the list of class path root elements of this project. * * @param classRootDirectoryNames the list of class path root elements of * this project. */ public void setClassRootDirectoryNames( final Collection<String> classRootDirectoryNames) { this.classRootDirectoryNames = classRootDirectoryNames; } /** * Sets the list of source path root elements of this project. * * @param sourceRootDirectoryNames the list of source path root elements of * this project. */ public void setSourceRootDirectoryNames( final Collection<String> sourceRootDirectoryNames) { this.sourceRootDirectoryNames = sourceRootDirectoryNames; } /** * Sets the includes for the scanner. * * @param includes the includes for the scanner. */ public void setIncludes(final List<String> includes) { this.includes = includes; } /** * Sets the excludes for the scanner. * * @param excludes the excludes for the scanner. */ public void setExcludes(final List<String> excludes) { this.excludes = excludes; } /** * Sets the encoding of the source files in this project. * * @param sourceEncoding the encoding of the source files in this project. */ public void setSourceEncoding(final String sourceEncoding) { this.sourceEncoding = sourceEncoding; } /** * Sets the encoding of the source files in this project. Defaults to * <code>1.5</code>. * * @param sourceVersion the encoding of the source files in this project. */ public void setSourceVersion(final String sourceVersion) { this.sourceVersion = sourceVersion; } // --- business ----------------------------------------------------------- /** * Creates the project configuration. * * @return the project configuration. * @throws IllegalArgumentException if any property is not valid. */ public JavadocProjectConfiguration build() throws IllegalArgumentException |
File | Line |
---|---|
de\smartics\maven\exceptions\AbstractSdocCodeMojo.java | 366 |
de\smartics\maven\exceptions\ExceptionCodeReport.java | 242 |
final Locale locale, final File outputDir) throws DependencyResolutionRequiredException { final List<String> classRootDirectoryNames = project.getCompileClasspathElements(); final List<String> sourceRootDirectoryNames = new ArrayList<String>(); sourceRootDirectoryNames.addAll(StringFunction.split( this.additionalSources, ",")); sourceRootDirectoryNames.addAll(project.getCompileSourceRoots()); sourceRootDirectoryNames.addAll(ConfigUtils .discoverSourceJars(classRootDirectoryNames)); final List<String> toolClassPath = PathUtils.toClassPath(pluginArtifacts); final Log log = getLog(); if (log.isDebugEnabled()) { log.debug("Tool class path: " + toolClassPath); log.debug("Class roots : " + classRootDirectoryNames); log.debug("Source roots : " + sourceRootDirectoryNames); } final DefaultProjectConfiguration.Builder<File> builder = |