mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
avoid using NanoXml with InputStream to reduce unneeded garbage
This commit is contained in:
@@ -18,7 +18,7 @@ package com.intellij.framework.detection;
|
||||
import com.intellij.patterns.*;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.indexing.FileContent;
|
||||
import com.intellij.util.text.CharSequenceReader;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import com.intellij.util.xml.XmlFileHeader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -101,7 +101,7 @@ public class FileContentPattern extends ObjectPattern<FileContent, FileContentPa
|
||||
@NotNull
|
||||
private static XmlFileHeader parseHeaderWithException(FileContent fileContent) throws IOException {
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
return NanoXmlUtil.parseHeaderWithException(new CharSequenceReader(fileContent.getContentAsText()));
|
||||
return NanoXmlUtil.parseHeaderWithException(CharArrayUtil.readerFromCharSequence(fileContent.getContentAsText()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -134,6 +134,7 @@ public class NanoXmlUtil {
|
||||
return parseHeader(new MyXMLReader(file.getInputStream()));
|
||||
}
|
||||
|
||||
@Deprecated // TODO: remove
|
||||
@NotNull
|
||||
public static XmlFileHeader parseHeader(final InputStream inputStream) {
|
||||
try {
|
||||
@@ -145,6 +146,11 @@ public class NanoXmlUtil {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static XmlFileHeader parseHeader(final Reader reader) {
|
||||
return parseHeader(new MyXMLReader(reader));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static XmlFileHeader parseHeader(PsiFile file) {
|
||||
return parseHeader(createReader(file));
|
||||
|
||||
@@ -17,14 +17,13 @@
|
||||
package com.intellij.xml.index;
|
||||
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
@@ -33,15 +32,20 @@ public class XsdNamespaceBuilder extends NanoXmlUtil.IXMLBuilderAdapter {
|
||||
|
||||
@Nullable
|
||||
public static String computeNamespace(final InputStream is) {
|
||||
return computeNamespace(new InputStreamReader(is));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String computeNamespace(final Reader reader) {
|
||||
try {
|
||||
final XsdNamespaceBuilder builder = new XsdNamespaceBuilder();
|
||||
NanoXmlUtil.parse(is, builder);
|
||||
NanoXmlUtil.parse(reader, builder);
|
||||
return builder.myNamespace;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
@@ -49,15 +53,6 @@ public class XsdNamespaceBuilder extends NanoXmlUtil.IXMLBuilderAdapter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String computeNamespace(final VirtualFile file) {
|
||||
return VfsUtil.processInputStream(file, new NullableFunction<InputStream, String>() {
|
||||
public String fun(final InputStream inputStream) {
|
||||
return computeNamespace(inputStream);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String myNamespace;
|
||||
|
||||
|
||||
@@ -549,8 +549,8 @@ public class CharArrayUtil {
|
||||
public static Reader readerFromCharSequence(CharSequence text) {
|
||||
Reader reader;
|
||||
char[] chars = fromSequenceWithoutCopying(text);
|
||||
if (chars != null) reader = new CharArrayReader(chars, 0, text.length());
|
||||
else reader = new StringReader(text.toString());
|
||||
if (chars != null) reader = new UnsyncCharArrayReader(chars, 0, text.length());
|
||||
else reader = new CharSequenceReader(text.toString());
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.util.text;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class UnsyncCharArrayReader extends Reader {
|
||||
private final char[] myText;
|
||||
private final int myLength;
|
||||
private int myCurPos;
|
||||
|
||||
public UnsyncCharArrayReader(final char[] text, int offset, int length) {
|
||||
myText = text;
|
||||
myLength = length;
|
||||
myCurPos = offset;
|
||||
}
|
||||
|
||||
public void close() {}
|
||||
|
||||
public int read(char[] cbuf, int off, int len) {
|
||||
if (off < 0 || off > cbuf.length || len < 0 || off + len > cbuf.length || off + len < 0) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int charsToCopy = Math.min(len, myLength - myCurPos);
|
||||
if (charsToCopy <= 0) return -1;
|
||||
|
||||
System.arraycopy(myText, myCurPos, cbuf, off, charsToCopy);
|
||||
|
||||
myCurPos += charsToCopy;
|
||||
return charsToCopy;
|
||||
}
|
||||
|
||||
public int read() {
|
||||
if (myCurPos >= myLength) return -1;
|
||||
return myText[myCurPos++];
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import com.intellij.util.xml.NanoXmlUtil;
|
||||
import org.jetbrains.android.util.AndroidResourceUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -61,7 +60,7 @@ public class AndroidIdIndex extends ScalarIndexExtension<String> {
|
||||
}
|
||||
final HashMap<String, Void> ids = new HashMap<String, Void>();
|
||||
|
||||
NanoXmlUtil.parse(new ByteArrayInputStream(inputData.getContent()), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
@Override
|
||||
public void addAttribute(String key, String nsPrefix, String nsURI, String value, String type) throws Exception {
|
||||
super.addAttribute(key, nsPrefix, nsURI, value, type);
|
||||
|
||||
@@ -16,7 +16,6 @@ import org.jetbrains.android.util.ValueResourcesFileParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
@@ -49,7 +48,7 @@ public class AndroidValueResourcesIndex extends FileBasedIndexExtension<Resource
|
||||
}
|
||||
final Map<ResourceEntry, Set<ResourceEntry>> result = new HashMap<ResourceEntry, Set<ResourceEntry>>();
|
||||
|
||||
NanoXmlUtil.parse(new ByteArrayInputStream(inputData.getContent()), new ValueResourcesFileParser() {
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()), new ValueResourcesFileParser() {
|
||||
@Override
|
||||
protected void stop() {
|
||||
throw new NanoXmlUtil.ParserStoppedException();
|
||||
|
||||
+1
-2
@@ -22,7 +22,6 @@ import com.intellij.psi.impl.include.FileIncludeInfo;
|
||||
import com.intellij.psi.impl.include.FileIncludeProvider;
|
||||
import com.intellij.util.indexing.FileContent;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.text.CharSequenceReader;
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import org.intellij.lang.xpath.xslt.XsltSupport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -77,7 +76,7 @@ public class XsltIncludeProvider extends FileIncludeProvider {
|
||||
}
|
||||
};
|
||||
|
||||
NanoXmlUtil.parse(new CharSequenceReader(contentAsText), builder);
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(contentAsText), builder);
|
||||
return infos.toArray(new FileIncludeInfo[infos.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public class XsltSymbolIndex extends FileBasedIndexExtension<String, XsltSymbolI
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
final HashMap<String, Kind> map = new HashMap<String, Kind>();
|
||||
NanoXmlUtil.parse(new UnsyncByteArrayInputStream(inputData.getContent()), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.IXMLBuilderAdapter attributeHandler;
|
||||
int depth;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.indexing.*;
|
||||
import com.intellij.util.io.EnumeratorStringDescriptor;
|
||||
import com.intellij.util.io.KeyDescriptor;
|
||||
import com.intellij.util.io.UnsyncByteArrayInputStream;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.xml.impl.DomApplicationComponent;
|
||||
import gnu.trove.THashMap;
|
||||
import gnu.trove.THashSet;
|
||||
@@ -52,7 +52,7 @@ public class DomFileIndex extends ScalarIndexExtension<String>{
|
||||
@NotNull
|
||||
public Map<String, Void> map(final FileContent inputData) {
|
||||
final Set<String> namespaces = new THashSet<String>();
|
||||
final XmlFileHeader header = NanoXmlUtil.parseHeader(new UnsyncByteArrayInputStream(inputData.getContent()));
|
||||
final XmlFileHeader header = NanoXmlUtil.parseHeader(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()));
|
||||
ContainerUtil.addIfNotNull(header.getPublicId(), namespaces);
|
||||
ContainerUtil.addIfNotNull(header.getSystemId(), namespaces);
|
||||
ContainerUtil.addIfNotNull(header.getRootTagNamespace(), namespaces);
|
||||
|
||||
@@ -24,7 +24,7 @@ import com.intellij.util.indexing.FileBasedIndex;
|
||||
import com.intellij.util.indexing.FileContent;
|
||||
import com.intellij.util.indexing.ID;
|
||||
import com.intellij.util.io.DataExternalizer;
|
||||
import com.intellij.util.io.UnsyncByteArrayInputStream;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -73,7 +73,7 @@ public class XmlNamespaceIndex extends XmlIndex<String> {
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<String, String> map(final FileContent inputData) {
|
||||
final String ns = XsdNamespaceBuilder.computeNamespace(new UnsyncByteArrayInputStream(inputData.getContent()));
|
||||
final String ns = XsdNamespaceBuilder.computeNamespace(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()));
|
||||
final HashMap<String, String> map = new HashMap<String, String>(2);
|
||||
if (ns != null) {
|
||||
map.put(ns, "");
|
||||
|
||||
@@ -20,7 +20,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import com.intellij.util.indexing.*;
|
||||
import com.intellij.util.io.DataExternalizer;
|
||||
import com.intellij.util.io.UnsyncByteArrayInputStream;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -55,7 +55,7 @@ public class XmlTagNamesIndex extends XmlIndex<Void> {
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<String, Void> map(final FileContent inputData) {
|
||||
final Collection<String> tags = XsdTagNameBuilder.computeTagNames(new UnsyncByteArrayInputStream(inputData.getContent()));
|
||||
final Collection<String> tags = XsdTagNameBuilder.computeTagNames(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()));
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
final HashMap<String, Void> map = new HashMap<String, Void>(tags.size());
|
||||
for (String tag : tags) {
|
||||
|
||||
@@ -16,14 +16,13 @@
|
||||
package com.intellij.xml.index;
|
||||
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -34,15 +33,20 @@ public class XsdTagNameBuilder extends NanoXmlUtil.IXMLBuilderAdapter {
|
||||
|
||||
@Nullable
|
||||
public static Collection<String> computeTagNames(final InputStream is) {
|
||||
return computeTagNames(new InputStreamReader(is));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Collection<String> computeTagNames(final Reader reader) {
|
||||
try {
|
||||
final XsdTagNameBuilder builder = new XsdTagNameBuilder();
|
||||
NanoXmlUtil.parse(is, builder);
|
||||
NanoXmlUtil.parse(reader, builder);
|
||||
return builder.myTagNames;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
@@ -51,15 +55,6 @@ public class XsdTagNameBuilder extends NanoXmlUtil.IXMLBuilderAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Collection<String> computeTagNames(final VirtualFile file) {
|
||||
return VfsUtil.processInputStream(file, new NullableFunction<InputStream, Collection<String>>() {
|
||||
public Collection<String> fun(final InputStream inputStream) {
|
||||
return computeTagNames(inputStream);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private final Collection<String> myTagNames = new ArrayList<String>();
|
||||
private boolean myElementStarted;
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.intellij.psi.impl.include.FileIncludeInfo;
|
||||
import com.intellij.psi.impl.include.FileIncludeProvider;
|
||||
import com.intellij.util.indexing.FileContent;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.text.CharSequenceReader;
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -51,7 +50,7 @@ public class XIncludeProvider extends FileIncludeProvider {
|
||||
CharSequence contentAsText = content.getContentAsText();
|
||||
if (CharArrayUtil.indexOf(contentAsText, XmlUtil.XINCLUDE_URI, 0) == -1) return FileIncludeInfo.EMPTY;
|
||||
final ArrayList<FileIncludeInfo> infos = new ArrayList<FileIncludeInfo>();
|
||||
NanoXmlUtil.parse(new CharSequenceReader(contentAsText), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(contentAsText), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
|
||||
boolean isXInclude;
|
||||
@Override
|
||||
|
||||
+1
-2
@@ -7,7 +7,6 @@ import com.intellij.psi.impl.include.FileIncludeInfo;
|
||||
import com.intellij.psi.impl.include.FileIncludeProvider;
|
||||
import com.intellij.util.indexing.FileContent;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.text.CharSequenceReader;
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import org.intellij.plugins.relaxNG.ApplicationLoader;
|
||||
import org.intellij.plugins.relaxNG.compact.RncFileType;
|
||||
@@ -45,7 +44,7 @@ public class RelaxIncludeProvider extends FileIncludeProvider {
|
||||
CharSequence inputDataContentAsText = content.getContentAsText();
|
||||
if (CharArrayUtil.indexOf(inputDataContentAsText, ApplicationLoader.RNG_NAMESPACE, 0) == -1) return FileIncludeInfo.EMPTY;
|
||||
infos = new ArrayList<FileIncludeInfo>();
|
||||
NanoXmlUtil.parse(new CharSequenceReader(inputDataContentAsText), new RngBuilderAdapter(infos));
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(content.getContentAsText()), new RngBuilderAdapter(infos));
|
||||
} else if (content.getFileType() == RncFileType.getInstance()) {
|
||||
infos = new ArrayList<FileIncludeInfo>();
|
||||
content.getPsiFile().acceptChildren(new RncElementVisitor() {
|
||||
|
||||
@@ -22,7 +22,6 @@ import com.intellij.util.indexing.*;
|
||||
import com.intellij.util.io.EnumeratorStringDescriptor;
|
||||
import com.intellij.util.io.KeyDescriptor;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import com.intellij.util.text.CharSequenceReader;
|
||||
import com.intellij.util.xml.NanoXmlUtil;
|
||||
import org.intellij.plugins.relaxNG.ApplicationLoader;
|
||||
import org.intellij.plugins.relaxNG.compact.RncFileType;
|
||||
@@ -73,7 +72,7 @@ public class RelaxSymbolIndex extends ScalarIndexExtension<String> {
|
||||
if (inputData.getFileType() == XmlFileType.INSTANCE) {
|
||||
CharSequence inputDataContentAsText = inputData.getContentAsText();
|
||||
if (CharArrayUtil.indexOf(inputDataContentAsText, ApplicationLoader.RNG_NAMESPACE, 0) == -1) return Collections.EMPTY_MAP;
|
||||
NanoXmlUtil.parse(new CharSequenceReader(inputDataContentAsText), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()), new NanoXmlUtil.IXMLBuilderAdapter() {
|
||||
NanoXmlUtil.IXMLBuilderAdapter attributeHandler;
|
||||
int depth;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user