private void generateWordDoc(String docName) throws FileNotFoundException, IOException { XWPFDocument doc = new XWPFDocument(); for (Theme t : themes) { for (Keyword k : t.getKeywords()) { for (Occurrence c : k.getOccurrs()) { XWPFParagraph p = doc.createParagraph(); p.setAlignment(ParagraphAlignment.LEFT); XWPFRun r = p.createRun(); setRunAttributes(r); r.setText(c.getOccurInfo()); r.addCarriageReturn(); String[] strings = c.getSentece().split(k.getName()); for (int i = 0; i < strings.length; i++) { XWPFRun r2 = p.createRun(); setRunAttributes(r2); r2.setText(strings[i]); if (i < strings.length - 1) { XWPFRun r3 = p.createRun(); setRunAttributes(r3); r3.setBold(true); r3.setItalic(true); r3.setColor(t.getHexColor()); r3.setText(k.getName()); } } } } } FileOutputStream outStream = new FileOutputStream(docName); doc.write(outStream); outStream.close(); }
public static Occurrence getInstance(final String name) { for (Occurrence entry : values()) { if (entry.name().equals(name)) { return entry; } } return null; }
@Override public int compareTo(@NonNull Occurrence other) { // First sort by length, then sort by name int delta = toString().length() - other.toString().length(); if (delta != 0) { return delta; } return toString().compareTo(other.toString()); }
static JsFunctionImpl create( JsParserResult parserResult, FunctionNode functionNode, List<Identifier> fqName, ModelBuilder modelBuilder, boolean isAnnonymous) { JsObjectImpl inObject = modelBuilder.getCurrentObject(); JsObject globalObject = modelBuilder.getGlobal(); JsObject parentObject = isAnnonymous ? globalObject : inObject; int start = Token.descPosition(functionNode.getFirstToken()); int end = Token.descPosition(functionNode.getLastToken()) + Token.descLength(functionNode.getLastToken()); List<Identifier> parameters = new ArrayList(functionNode.getParameters().size()); for (IdentNode node : functionNode.getParameters()) { parameters.add(create(parserResult, node)); } JsFunctionImpl result; if (fqName.size() > 1) { List<Identifier> objectName = fqName.subList(0, fqName.size() - 1); parentObject = isAnnonymous ? globalObject : ModelUtils.getJsObject(modelBuilder, objectName); result = new JsFunctionImpl( modelBuilder.getCurrentDeclarationScope(), parentObject, fqName.get(fqName.size() - 1), parameters, ModelUtils.documentOffsetRange(parserResult, start, end)); if (parentObject instanceof JsFunction && !"prototype".equals(parentObject.getName())) { result.addModifier(Modifier.STATIC); } } else { result = new JsFunctionImpl( modelBuilder.getCurrentDeclarationScope(), inObject, fqName.get(fqName.size() - 1), parameters, ModelUtils.documentOffsetRange(parserResult, start, end)); } String propertyName = result.getDeclarationName().getName(); JsObject property = parentObject.getProperty(propertyName); // the already existing property parentObject.addProperty(result.getDeclarationName().getName(), result); if (property != null) { result.addOccurrence(property.getDeclarationName().getOffsetRange()); for (Occurrence occurrence : property.getOccurrences()) { result.addOccurrence(occurrence.getOffsetRange()); } } JsDocumentationHolder docHolder = parserResult.getDocumentationHolder(); result.setDocumentation(docHolder.getDocumentation(functionNode)); result.setAnonymous(isAnnonymous); return result; }
@Override public void visitAttribute(@NonNull XmlContext context, @NonNull Attr attribute) { assert attribute.getName().equals(ATTR_ID) || attribute.getLocalName().equals(ATTR_ID); String id = attribute.getValue(); if (context.getPhase() == 1) { if (mIds.contains(id)) { Location location = context.getLocation(attribute); Attr first = findIdAttribute(attribute.getOwnerDocument(), id); if (first != null && first != attribute) { Location secondLocation = context.getLocation(first); secondLocation.setMessage(String.format("%1$s originally defined here", id)); location.setSecondary(secondLocation); } context.report( WITHIN_LAYOUT, attribute, location, String.format("Duplicate id %1$s, already defined earlier in this layout", id), null); } else if (id.startsWith(NEW_ID_PREFIX)) { // Skip id's on include tags if (attribute.getOwnerElement().getTagName().equals(VIEW_INCLUDE)) { return; } mIds.add(id); } } else { Collection<Multimap<String, Occurrence>> maps = mLocations.get(context.file); if (maps != null && !maps.isEmpty()) { for (Multimap<String, Occurrence> map : maps) { if (!maps.isEmpty()) { Collection<Occurrence> occurrences = map.get(id); if (occurrences != null && !occurrences.isEmpty()) { for (Occurrence occurrence : occurrences) { if (context.getDriver().isSuppressed(CROSS_LAYOUT, attribute)) { return; } Location location = context.getLocation(attribute); location.setClientData(attribute); location.setMessage(occurrence.message); location.setSecondary(occurrence.location); occurrence.location = location; } } } } } } }
@Override public void visitElement(@NonNull XmlContext context, @NonNull Element element) { // Record include graph such that we can look for inter-layout duplicates after the // project has been fully checked String layout = element.getAttribute(ATTR_LAYOUT); // NOTE: Not in android: namespace if (layout.startsWith(LAYOUT_RESOURCE_PREFIX)) { // Ignore @android:layout/ layouts layout = layout.substring(LAYOUT_RESOURCE_PREFIX.length()); if (context.getPhase() == 1) { if (!context.getProject().getReportIssues()) { // If this is a library project not being analyzed, ignore it return; } List<String> to = mIncludes.get(context.file); if (to == null) { to = new ArrayList<String>(); mIncludes.put(context.file, to); } to.add(layout); } else { assert context.getPhase() == 2; Collection<Multimap<String, Occurrence>> maps = mLocations.get(context.file); if (maps != null && !maps.isEmpty()) { for (Multimap<String, Occurrence> map : maps) { if (!maps.isEmpty()) { Collection<Occurrence> occurrences = map.get(layout); if (occurrences != null && !occurrences.isEmpty()) { for (Occurrence occurrence : occurrences) { Location location = context.getLocation(element); location.setClientData(element); location.setMessage(occurrence.message); location.setSecondary(occurrence.location); occurrence.location = location; } } } } } } } }
/** * Organized the Applications of "d" into a leveled data structure based on their dependance among * each other * * @return exactly the same Application instances in a leveled data structure */ private final List<Set<Rule.Match>> getTopology(List<Rule.Match> l) { List<Set<Occurrence>> levels = new ArrayList<Set<Occurrence>>(); Occurrence initialWord = new Occurrence(); initialWord.setSymbol(l.get(0).rule.getLeftHandSide().get(0)); initialWord.setSource(null); initialWord.setPosition(0); ensureCapacity(1, levels); levels.get(0).add(initialWord); List<Occurrence> currentWord = new ArrayList<Occurrence>(); currentWord.add(initialWord); for (int i = 0; i < l.size(); i++) { Rule.Match a = l.get(i); List<Occurrence> depSeq = dependingSubsequence(currentWord, a); List<Occurrence> newWord = apply(currentWord, a); List<Occurrence> prodSeq = producedSubsequence(newWord, a); int myLevel = getMaxLevel(depSeq, levels) + 1; ensureCapacity(myLevel + 1, levels); Set<Occurrence> myLevelSet = levels.get(myLevel); myLevelSet.addAll(prodSeq); currentWord = newWord; } List<Set<Rule.Match>> result = new ArrayList<Set<Rule.Match>>(); for (Set<Occurrence> level : levels) { Set<Rule.Match> set = new HashSet<Rule.Match>(); result.add(set); for (Occurrence o : level) { set.add(o.getSource()); } } return result; }
/** * applies "a" to the "word". * * @return the resulting word */ private final List<Occurrence> apply(List<Occurrence> word, Rule.Match a) { Rule r = a.rule; int length = word.size() - r.getLeftHandSide().length() + r.getRightHandSide().length(); int pos = a.leftHandSideStart; List<Occurrence> result = new ArrayList<Occurrence>(length); int rp = 0; for (; rp < pos; rp++) { result.add(word.get(rp)); } Occurrence sourceOcc = word.get(rp + 1); for (int i = 0; rp < pos + r.getRightHandSide().length(); rp++, i++) { Occurrence o = new Occurrence(); o.setSymbol(r.getRightHandSide().get(i)); o.setSourceOccurrence(sourceOcc); o.setPosition(rp); o.setSource(a); result.add(o); } for (int sp = pos + r.getLeftHandSide().length(); sp < word.size(); rp++, sp++) { result.add(word.get(sp)); } return result; }
/** * Computes the cumulative set of ids used in a given layout. We can't just depth-first-search * the graph and check the set of ids encountered along the way, because we need to detect when * multiple includes contribute the same ids. For example, if a file is included more than once, * that would result in duplicates. */ private Set<String> getIds(Layout layout, Deque<Layout> stack, Set<Layout> seen) { seen.add(layout); Set<String> layoutIds = layout.getIds(); List<Layout> includes = layout.getIncludes(); if (includes != null) { Set<String> ids = new HashSet<String>(); if (layoutIds != null) { ids.addAll(layoutIds); } stack.push(layout); Multimap<String, Set<String>> nameToIds = ArrayListMultimap.create(includes.size(), 4); for (Layout included : includes) { if (seen.contains(included)) { continue; } Set<String> includedIds = getIds(included, stack, seen); if (includedIds != null) { String layoutName = included.getLayoutName(); idCheck: for (String id : includedIds) { if (ids.contains(id)) { Collection<Set<String>> idSets = nameToIds.get(layoutName); if (idSets != null) { for (Set<String> siblingIds : idSets) { if (siblingIds.contains(id)) { // The id reference was added by a sibling, // so no need to complain (again) continue idCheck; } } } // Duplicate! Record location request for new phase. if (mLocations == null) { mErrors = new ArrayList<Occurrence>(); mLocations = ArrayListMultimap.create(); mContext .getDriver() .requestRepeat(DuplicateIdDetector.this, Scope.ALL_RESOURCES_SCOPE); } Map<Layout, Occurrence> occurrences = new HashMap<Layout, Occurrence>(); findId(layout, id, new ArrayDeque<Layout>(), occurrences, new HashSet<Layout>()); assert occurrences.size() >= 2; // Stash a request to find the given include Collection<Occurrence> values = occurrences.values(); List<Occurrence> sorted = new ArrayList<Occurrence>(values); Collections.sort(sorted); String msg = String.format( "Duplicate id %1$s, defined or included multiple " + "times in %2$s: %3$s", id, layout.getDisplayName(), sorted.toString()); // Store location request for the <include> tag Occurrence primary = new Occurrence(layout.getFile(), msg, null); Multimap<String, Occurrence> m = ArrayListMultimap.create(); m.put(layoutName, primary); mLocations.put(layout.getFile(), m); mErrors.add(primary); Occurrence prev = primary; // Now store all the included occurrences of the id for (Occurrence occurrence : values) { if (occurrence.file.equals(layout.getFile())) { occurrence.message = "Defined here"; } else { occurrence.message = String.format("Defined here, included via %1$s", occurrence.includePath); } m = ArrayListMultimap.create(); m.put(id, occurrence); mLocations.put(occurrence.file, m); // Link locations together prev.next = occurrence; prev = occurrence; } } ids.add(id); } // Store these ids such that on a conflict, we can tell when // an id was added by a single variation of this file nameToIds.put(layoutName, includedIds); } } Layout visited = stack.pop(); assert visited == layout; return ids; } else { return layoutIds; } }