Skip to content

Commit

Permalink
Improves string search
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Mar 29, 2015
1 parent 6683252 commit 990576b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class SearchInConstantPoolsController implements IndexesChangeListener {
def matchTypeEntriesWithStringClosure = { s, index -> matchTypeEntriesWithString(s, index) }
def matchWithCharClosure = { c, index -> matchWithChar(c, index) }
def matchWithStringClosure = { s, index -> matchWithString(s, index) }
def matchStringWithCharClosure = { c, index -> matchStringWithChar(c, index) }
def matchStringWithStringClosure = { s, index -> matchStringWithString(s, index) }

if ((flags & SearchInConstantPoolsView.SEARCH_TYPE_TYPE) != 0) {
if (declarations)
Expand Down Expand Up @@ -233,7 +235,7 @@ class SearchInConstantPoolsController implements IndexesChangeListener {
if ((flags & SearchInConstantPoolsView.SEARCH_TYPE_STRING) != 0) {
if (declarations || references)
match(indexes, 'strings', pattern,
matchWithCharClosure, matchWithStringClosure, matchingEntries)
matchStringWithCharClosure, matchStringWithStringClosure, matchingEntries)
}
}

Expand Down Expand Up @@ -315,6 +317,22 @@ class SearchInConstantPoolsController implements IndexesChangeListener {
return index.findAll { String key, entries -> p.matcher(key).matches() }
}

@CompileStatic
static Map<String, Collection<Container.Entry>> matchStringWithChar(char c, Map<String, Collection<Container.Entry>> index) {
if ((c == '*') || (c == '?')) {
return Collections.emptyMap()
} else {
def p = Pattern.compile(String.valueOf(c))
return index.findAll { String key, entries -> p.matcher(key).find() }
}
}

@CompileStatic
static Map<String, Collection<Container.Entry>> matchStringWithString(String pattern, Map<String, Collection<Container.Entry>> index) {
def p = createPattern(pattern)
return index.findAll { String key, entries -> p.matcher(key).find() }
}

/**
* Create a simple regular expression
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,58 @@ class XmlFileIndexerProvider implements Indexer {
@CompileStatic
void index(API api, Container.Entry entry, Indexes indexes) {
def index = indexes.getIndex('strings')
def set = new HashSet<String>()
def reader

try {
reader = factory.createXMLStreamReader(entry.inputStream)

index.get(reader.version).add(entry)
index.get(reader.encoding).add(entry)
index.get(reader.characterEncodingScheme).add(entry)
set.add(reader.version)
set.add(reader.encoding)
set.add(reader.characterEncodingScheme)

while (reader.hasNext()) {
switch (reader.next()) {
case XMLStreamConstants.START_ELEMENT:
index.get(reader.localName).add(entry)
set.add(reader.localName)
for (int i = reader.attributeCount - 1; i >= 0; i--) {
index.get(reader.getAttributeLocalName(i)).add(entry)
index.get(reader.getAttributeValue(i)).add(entry)
set.add(reader.getAttributeLocalName(i))
set.add(reader.getAttributeValue(i))
}
for (int i = reader.namespaceCount - 1; i >= 0; i--) {
index.get(reader.getNamespacePrefix(i)).add(entry)
index.get(reader.getNamespaceURI(i)).add(entry)
set.add(reader.getNamespacePrefix(i))
set.add(reader.getNamespaceURI(i))
}
break
case XMLStreamConstants.PROCESSING_INSTRUCTION:
index.get(reader.getPITarget()).add(entry)
index.get(reader.getPIData()).add(entry)
set.add(reader.getPITarget())
set.add(reader.getPIData())
break
case XMLStreamConstants.START_DOCUMENT:
index.get(reader.version).add(entry)
index.get(reader.encoding).add(entry)
index.get(reader.characterEncodingScheme).add(entry)
set.add(reader.version)
set.add(reader.encoding)
set.add(reader.characterEncodingScheme)
break
case XMLStreamConstants.ENTITY_REFERENCE:
index.get(reader.localName).add(entry)
index.get(reader.text).add(entry)
set.add(reader.localName)
set.add(reader.text)
break
case XMLStreamConstants.ATTRIBUTE:
index.get(reader.prefix).add(entry)
index.get(reader.namespaceURI).add(entry)
index.get(reader.localName).add(entry)
index.get(reader.text).add(entry)
set.add(reader.prefix)
set.add(reader.namespaceURI)
set.add(reader.localName)
set.add(reader.text)
break
case XMLStreamConstants.COMMENT:
case XMLStreamConstants.DTD:
case XMLStreamConstants.CDATA:
case XMLStreamConstants.CHARACTERS:
index.get(reader.text.trim()).add(entry)
set.add(reader.text.trim())
break
case XMLStreamConstants.NAMESPACE:
for (int i = reader.namespaceCount - 1; i >= 0; i--) {
index.get(reader.getNamespacePrefix(i)).add(entry)
index.get(reader.getNamespaceURI(i)).add(entry)
set.add(reader.getNamespacePrefix(i))
set.add(reader.getNamespaceURI(i))
}
break
}
Expand All @@ -89,5 +90,11 @@ class XmlFileIndexerProvider implements Indexer {
} finally {
reader?.close()
}

for (def string : set) {
if (string) {
index.get(string).add(entry)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class XmlFileTreeNodeFactoryProvider extends TextFileTreeNodeFactoryProvider {
public <T extends JComponent & UriGettable> T createPage(API api) {
return new TextFileTreeNodeFactoryProvider.Page(entry) {
String getSyntaxStyle() {
SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE
SyntaxConstants.SYNTAX_STYLE_XML
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ class ClassFilePage

if (highlightFlags.indexOf('s') != -1) {
// Highlight strings
def patternForString = Pattern.compile('^"' + regexp)
def patternForString = Pattern.compile(regexp)

for (def data : strings) {
if (!highlightScope || data.owner.equals(highlightScope)) {
def matcher = patternForString.matcher(data.text)
int offset = data.startPosition

if (matcher.find()) {
int offset = data.startPosition
ranges.add(new DocumentRange(offset + matcher.start() + 1, offset + matcher.end()))
while(matcher.find()) {
ranges.add(new DocumentRange(offset + matcher.start(), offset + matcher.end()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ jd.gui.service.indexer.DirectoryIndexerProvider
jd.gui.service.indexer.ClassFileIndexerProvider
jd.gui.service.indexer.MetainfServiceFileIndexerProvider
jd.gui.service.indexer.TextFileIndexerProvider
jd.gui.service.indexer.XmlFileIndexerProvider
jd.gui.service.indexer.ZipFileIndexerProvider

0 comments on commit 990576b

Please sign in to comment.