public static MultiMap<SchemaTypeInfo, SchemaTypeInfo> parse(final Reader reader) { try { final XsdComplexTypeInfoBuilder builder = new XsdComplexTypeInfoBuilder(); final NameSpaceHelper helper = new NameSpaceHelper(); builder.setNameSpaceHelper(helper); NanoXmlUtil.parse(reader, builder, helper); final MultiMap<SchemaTypeInfo,SchemaTypeInfo> map = builder.getMap(); return map; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // can never happen } } }
public static XsdNamespaceBuilder computeNamespace(final Reader reader) { try { final XsdNamespaceBuilder builder = new XsdNamespaceBuilder(); NanoXmlUtil.parse(reader, builder); HashSet<String> tags = new HashSet<String>(builder.getTags()); tags.removeAll(builder.myReferencedTags); builder.getRootTags().addAll(tags); return builder; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // can never happen } } }
@NotNull public static Collection<String> computeTagNames(final Reader reader) { try { final XsdTagNameBuilder builder = new XsdTagNameBuilder(); NanoXmlUtil.parse(reader, builder); return builder.myTagNames; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // can never happen } } }
private static boolean isSimilarFile(FileContent inputData) { if (CharArrayUtil.indexOf(inputData.getContentAsText(), "<" + RESOURCES_ROOT_TAG, 0) < 0) { return false; } final boolean[] ourRootTag = {false}; NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(inputData.getContentAsText()), new NanoXmlUtil.IXMLBuilderAdapter() { @Override public void startElement(String name, String nsPrefix, String nsURI, String systemID, int lineNr) throws Exception { ourRootTag[0] = RESOURCES_ROOT_TAG.equals(name) && nsPrefix == null; stop(); } }); return ourRootTag[0]; }
@Nullable private void parsePublicResCache() { final String resDirPath = myTarget.getPath(IAndroidTarget.RESOURCES); final String publicXmlPath = resDirPath + '/' + SdkConstants.FD_RES_VALUES + "/public.xml"; final VirtualFile publicXml = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(publicXmlPath)); if (publicXml != null) { try { final MyPublicResourceCacheBuilder builder = new MyPublicResourceCacheBuilder(); NanoXmlUtil.parse(publicXml.getInputStream(), builder); synchronized (myPublicResourceCacheLock) { myPublicResourceCache = builder.getPublicResourceCache(); myPublicResourceIdMap = builder.getIdMap(); } } catch (IOException e) { LOG.error(e); } } }
@Nullable protected Map<String, Set<String>> getIds(String content, final VirtualFile file, Project project) { if (!content.contains(JavaFXNamespaceProvider.JAVAFX_NAMESPACE)) { return null; } final Map<String, Set<String>> map = new HashMap<String, Set<String>>(); final String path = file.getPath(); final IXMLBuilder handler = createParseHandler(path, map); try { NanoXmlUtil.parse(new StringReader(content), handler); } catch (StopException ignore) {} final VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(file); endDocument(path, sourceRoot, map, handler); return map; }
public static XsdNamespaceBuilder computeNamespace(final Reader reader) { try { final XsdNamespaceBuilder builder = new XsdNamespaceBuilder(); NanoXmlUtil.parse(reader, builder); return builder; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // can never happen } } }
@Nullable public static Collection<String> computeTagNames(final Reader reader) { try { final XsdTagNameBuilder builder = new XsdTagNameBuilder(); NanoXmlUtil.parse(reader, builder); return builder.myTagNames; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // can never happen } } }
@Test public void testXsdHeader() throws Exception { XmlFileHeader xmlFileHeader = NanoXmlUtil.parseHeaderWithException(new StringReader("<?xml version=\"1.0\"?>\n" + "\n" + "<note\n" + "xmlns=\"https://www.w3schools.com\"\n" + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "xsi:schemaLocation=\"https://www.w3schools.com/xml/note.xsd\">\n" + " <to>Tove</to>\n" + " <from>Jani</from>\n" + " <heading>Reminder</heading>\n" + " <body>Don't forget me this weekend!</body>\n" + "</note>")); assertEquals("note", xmlFileHeader.getRootTagLocalName()); assertEquals("https://www.w3schools.com", xmlFileHeader.getRootTagNamespace()); }
public static XsdNamespaceBuilder computeNamespace(final Reader reader) { try { final XsdNamespaceBuilder builder = new XsdNamespaceBuilder(); NanoXmlUtil.parse(reader, builder); HashSet<String> tags = new HashSet<String>(builder.getTags()); tags.removeAll(builder.myReferencedTags); builder.getRootTags().addAll(tags); return builder; } finally { try { if(reader != null) { reader.close(); } } catch(IOException e) { // can never happen } } }
@NotNull @Override public FileIncludeInfo[] getIncludeInfos(FileContent content) { final ArrayList<FileIncludeInfo> infos; if (content.getFileType() == XmlFileType.INSTANCE) { CharSequence inputDataContentAsText = content.getContentAsText(); if (CharArrayUtil.indexOf(inputDataContentAsText, ApplicationLoader.RNG_NAMESPACE, 0) == -1) return FileIncludeInfo.EMPTY; infos = new ArrayList<FileIncludeInfo>(); NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(content.getContentAsText()), new RngBuilderAdapter(infos)); } else if (content.getFileType() == RncFileType.getInstance()) { infos = new ArrayList<FileIncludeInfo>(); content.getPsiFile().acceptChildren(new RncElementVisitor() { @Override public void visitElement(RncElement element) { element.acceptChildren(this); } @Override public void visitInclude(RncInclude include) { final String path = include.getFileReference(); if (path != null) { infos.add(new FileIncludeInfo(path)); } } }); } else { return FileIncludeInfo.EMPTY; } return infos.toArray(new FileIncludeInfo[infos.size()]); }
public static boolean isTestngXML(final VirtualFile virtualFile) { if ("xml".equalsIgnoreCase(virtualFile.getExtension()) && virtualFile.isValid()) { final String result = NanoXmlUtil.parseHeader(virtualFile).getRootTagLocalName(); if (result != null && result.equals(SUITE_TAG_NAME)) { return true; } } return false; }
@Nullable private static MyIXMLBuilderAdapter parse(CharSequence text, boolean stopIfAccepted) { StdXMLReader reader = new StdXMLReader(CharArrayUtil.readerFromCharSequence(text)) { @Override public Reader openStream(String publicID, String systemID) throws IOException { if (!HTTP_JAVA_SUN_COM_DTD_PROPERTIES_DTD.equals(systemID)) throw new IOException(); return new StringReader(" "); } }; MyIXMLBuilderAdapter builder = new MyIXMLBuilderAdapter(stopIfAccepted); NanoXmlUtil.parse(reader, builder); return builder; }
@Override public void startElement(String name, String nsPrefix, String nsURI, String systemID, int lineNr) throws Exception { if (!accepted) { if ("properties".equals(name)) { accepted = true; } else throw NanoXmlUtil.ParserStoppedXmlException.INSTANCE; } else { insideEntry = "entry".equals(name); } if (myStopIfAccepted) throw NanoXmlUtil.ParserStoppedXmlException.INSTANCE; }
public CachedValueProvider.Result<XsltChecker.LanguageLevel> compute(PsiFile psiFile) { if (!(psiFile instanceof XmlFile)) { return CachedValueProvider.Result.create(XsltChecker.LanguageLevel.NONE, PsiModificationTracker.MODIFICATION_COUNT); } final XmlFile xmlFile = (XmlFile)psiFile; if (psiFile instanceof PsiFileEx) { if (((PsiFileEx)psiFile).isContentsLoaded()) { final XmlDocument doc = xmlFile.getDocument(); if (doc != null) { final XmlTag rootTag = doc.getRootTag(); if (rootTag != null) { XmlAttribute v; XsltChecker.LanguageLevel level; if (isXsltRootTag(rootTag)) { v = rootTag.getAttribute("version"); level = v != null ? XsltChecker.getLanguageLevel(v.getValue()) : XsltChecker.LanguageLevel.NONE; } else { v = rootTag.getAttribute("version", XSLT_NS); level = v != null ? XsltChecker.getLanguageLevel(v.getValue()) : XsltChecker.LanguageLevel.NONE; } return CachedValueProvider.Result.create(level, rootTag); } } } } final XsltChecker xsltChecker = new XsltChecker(); NanoXmlUtil.parseFile(psiFile, xsltChecker); return CachedValueProvider.Result.create(xsltChecker.getLanguageLevel(), psiFile); }
protected IXMLBuilder createParseHandler(final String path, final Map<String, Set<String>> map) { return new NanoXmlUtil.IXMLBuilderAdapter() { @Override public void addAttribute(String key, String nsPrefix, String nsURI, String value, String type) throws Exception { if (value != null && FxmlConstants.FX_ID.equals(nsPrefix + ":" + key)) { Set<String> paths = map.get(value); if (paths == null) { paths = new HashSet<String>(); map.put(value, paths); } paths.add(path); } } }; }
@Nullable private static MyIXMLBuilderAdapter parse(CharSequence text, boolean stopIfAccepted) { StdXMLReader reader = new StdXMLReader(CharArrayUtil.readerFromCharSequence(text)) { @Override public Reader openStream(String publicID, String systemID) throws IOException { if (!"http://java.sun.com/dtd/properties.dtd".equals(systemID)) throw new IOException(); return new StringReader(" "); } }; MyIXMLBuilderAdapter builder = new MyIXMLBuilderAdapter(stopIfAccepted); NanoXmlUtil.parse(reader, builder); return builder; }
public CachedValueProvider.Result<XsltChecker.LanguageLevel> compute(PsiFile psiFile) { if (!(psiFile instanceof XmlFile)) { return CachedValueProvider.Result.create(XsltChecker.LanguageLevel.NONE); } final XmlFile xmlFile = (XmlFile)psiFile; if (psiFile instanceof PsiFileEx) { if (((PsiFileEx)psiFile).isContentsLoaded()) { final XmlDocument doc = xmlFile.getDocument(); if (doc != null) { final XmlTag rootTag = doc.getRootTag(); if (rootTag != null) { XmlAttribute v; XsltChecker.LanguageLevel level; if (isXsltRootTag(rootTag)) { v = rootTag.getAttribute("version"); level = v != null ? XsltChecker.getLanguageLevel(v.getValue()) : XsltChecker.LanguageLevel.NONE; } else { v = rootTag.getAttribute("version", XSLT_NS); level = v != null ? XsltChecker.getLanguageLevel(v.getValue()) : XsltChecker.LanguageLevel.NONE; } return CachedValueProvider.Result.create(level, rootTag); } } } } final XsltChecker xsltChecker = new XsltChecker(); NanoXmlUtil.parseFile(psiFile, xsltChecker); return CachedValueProvider.Result.create(xsltChecker.getLanguageLevel(), psiFile); }
@Test public void testDtdHeader() throws Exception { XmlFileHeader xmlFileHeader = NanoXmlUtil.parseHeaderWithException(new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" + "<!-- the XHTML document body starts here-->\n" + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" + "</html>")); assertEquals("html", xmlFileHeader.getRootTagLocalName()); assertEquals("http://www.w3.org/1999/xhtml", xmlFileHeader.getRootTagNamespace()); assertEquals("-//W3C//DTD XHTML 1.0 Transitional//EN", xmlFileHeader.getPublicId()); assertEquals("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xmlFileHeader.getSystemId()); }
@NotNull @Override public FileIncludeInfo[] getIncludeInfos(FileContent content) { final ArrayList<FileIncludeInfo> infos; if (content.getFileType() == XmlFileType.INSTANCE) { CharSequence inputDataContentAsText = content.getContentAsText(); if (CharArrayUtil.indexOf(inputDataContentAsText, ApplicationLoader.RNG_NAMESPACE, 0) == -1) return FileIncludeInfo.EMPTY; infos = new ArrayList<>(); NanoXmlUtil.parse(CharArrayUtil.readerFromCharSequence(content.getContentAsText()), new RngBuilderAdapter(infos)); } else if (content.getFileType() == RncFileType.getInstance()) { infos = new ArrayList<>(); content.getPsiFile().acceptChildren(new RncElementVisitor() { @Override public void visitElement(RncElement element) { element.acceptChildren(this); } @Override public void visitInclude(RncInclude include) { final String path = include.getFileReference(); if (path != null) { infos.add(new FileIncludeInfo(path)); } } }); } else { return FileIncludeInfo.EMPTY; } return infos.toArray(new FileIncludeInfo[infos.size()]); }
@NotNull private static XmlFileHeader parseHeaderWithException(FileContent fileContent) throws IOException { //noinspection IOResourceOpenedButNotSafelyClosed return NanoXmlUtil.parseHeaderWithException(CharArrayUtil.readerFromCharSequence(fileContent.getContentAsText())); }
@Nonnull private static XmlFileHeader parseHeaderWithException(FileContent fileContent) throws IOException { //noinspection IOResourceOpenedButNotSafelyClosed return NanoXmlUtil.parseHeaderWithException(CharArrayUtil.readerFromCharSequence(fileContent.getContentAsText())); }