public Object[] get(final PsiElement context, CompletionContext completionContext) { XmlFile containingFile = null; XmlFile descriptorFile = null; final XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class); if (tag != null) { containingFile = (XmlFile) tag.getContainingFile(); descriptorFile = findDescriptorFile(tag, containingFile); } else { final XmlDocument document = PsiTreeUtil.getParentOfType(context, XmlDocument.class); if (document != null) { containingFile = (XmlFile) document.getContainingFile(); final FileType ft = containingFile.getFileType(); if (ft != StdFileTypes.XML) { final String namespace = ft == StdFileTypes.XHTML || ft == StdFileTypes.JSPX ? XmlUtil.XHTML_URI : XmlUtil.HTML_URI; final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(namespace, true); if (nsDescriptor != null) { descriptorFile = nsDescriptor.getDescriptorFile(); } } } } if (descriptorFile != null) { final List<Object> results = new ArrayList<Object>(); final boolean acceptSystemEntities = containingFile.getFileType() == StdFileTypes.XML; final PsiElementProcessor processor = new PsiElementProcessor() { public boolean execute(@NotNull final PsiElement element) { if (element instanceof XmlEntityDecl) { final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element; if (xmlEntityDecl.isInternalReference() || acceptSystemEntities) { final String name = xmlEntityDecl.getName(); final Object _item = getLookupItem(xmlEntityDecl); results.add(_item == null ? name : _item); } } return true; } }; XmlUtil.processXmlElements(descriptorFile, processor, true); if (descriptorFile != containingFile && containingFile.getFileType() == StdFileTypes.XML) { final XmlProlog element = containingFile.getDocument().getProlog(); if (element != null) XmlUtil.processXmlElements(element, processor, true); } return ArrayUtil.toObjectArray(results); } return ArrayUtil.EMPTY_OBJECT_ARRAY; }
@NotNull private static XmlFileHeader calcXmlFileHeader(final XmlFile file) { if (file instanceof PsiFileEx && ((PsiFileEx) file).isContentsLoaded() && file.getNode().isParsed()) { return computeHeaderByPsi(file); } if (!XmlUtil.isStubBuilding() && file.getFileType() == XmlFileType.INSTANCE) { VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile instanceof VirtualFileWithId) { ObjectStubTree tree = StubTreeLoader.getInstance().readFromVFile(file.getProject(), virtualFile); if (tree != null) { Stub root = tree.getRoot(); if (root instanceof FileStub) { return ((FileStub) root).getHeader(); } } } } if (!file.isValid()) return XmlFileHeader.EMPTY; return NanoXmlUtil.parseHeader(file); }
public String[] knownNamespaces() { final PsiElement parentElement = getParent(); BidirectionalMap<String, String> map = initNamespaceMaps(parentElement); Set<String> known = Collections.emptySet(); if (map != null) { known = new HashSet<String>(map.values()); } if (parentElement instanceof XmlTag) { if (known.isEmpty()) return ((XmlTag) parentElement).knownNamespaces(); ContainerUtil.addAll(known, ((XmlTag) parentElement).knownNamespaces()); } else { XmlExtension xmlExtension = XmlExtension.getExtensionByElement(this); if (xmlExtension != null) { final XmlFile xmlFile = xmlExtension.getContainingFile(this); if (xmlFile != null) { final XmlTag rootTag = xmlFile.getRootTag(); if (rootTag != null && rootTag != this) { if (known.isEmpty()) return rootTag.knownNamespaces(); ContainerUtil.addAll(known, rootTag.knownNamespaces()); } } } } return ArrayUtil.toStringArray(known); }
private static void fillFromSchema(PsiFile file, ElementNames names) { if (!(file instanceof XmlFile)) return; final XmlFile f = (XmlFile) file; final XmlDocument d = f.getDocument(); if (d == null) return; final XmlTag rootTag = d.getRootTag(); if (rootTag == null) return; //noinspection unchecked names.dependencies.add(new NSDeclTracker(rootTag)); try { final Map<String, String> namespaceDeclarations = rootTag.getLocalNamespaceDeclarations(); final Collection<String> prefixes = namespaceDeclarations.keySet(); //noinspection unchecked final Set<XmlElementDescriptor> history = new THashSet<XmlElementDescriptor>(); final XmlElementFactory ef = XmlElementFactory.getInstance(file.getProject()); int noSchemaNamespaces = 0; for (String prefix : prefixes) { final String namespace = namespaceDeclarations.get(prefix); if (isIgnoredNamespace(prefix, namespace)) continue; final XmlTag tag = ef.createTagFromText("<dummy-tag xmlns='" + namespace + "' />", XMLLanguage.INSTANCE); final XmlDocument document = PsiTreeUtil.getParentOfType(tag, XmlDocument.class); final XmlNSDescriptor rootDescriptor = tag.getNSDescriptor(tag.getNamespace(), true); if (rootDescriptor == null || (rootDescriptor instanceof XmlNSDescriptorImpl && ((XmlNSDescriptorImpl) rootDescriptor).getTag() == null) || !rootDescriptor.getDeclaration().isPhysical()) { final QName any = QNameUtil.createAnyLocalName(namespace); names.elementNames.add(any); names.attributeNames.add(any); noSchemaNamespaces++; continue; } //noinspection unchecked names.dependencies.add(rootDescriptor.getDescriptorFile()); final XmlElementDescriptor[] e = rootDescriptor.getRootElementsDescriptors(document); for (XmlElementDescriptor descriptor : e) { processElementDescriptors(descriptor, tag, names, history); } } names.validateNames = names.elementNames.size() > noSchemaNamespaces; // final QName any = QNameUtil.createAnyLocalName(""); // names.elementNames.add(any); // names.attributeNames.add(any); } catch (IncorrectOperationException e) { Logger.getInstance(XsltContextProvider.class.getName()).error(e); } }
private boolean isGeneratedFromDtd(XmlNSDescriptor defaultNSDescriptorInner) { if (defaultNSDescriptorInner == null) { return false; } XmlFile descriptorFile = defaultNSDescriptorInner.getDescriptorFile(); if (descriptorFile == null) { return false; } @NonNls String otherName = XmlUtil.getContainingFile(this).getName() + ".dtd"; return descriptorFile.getName().equals(otherName); }
public static XmlFile findDescriptorFile(final XmlTag tag, final XmlFile containingFile) { final XmlElementDescriptor descriptor = tag.getDescriptor(); final XmlNSDescriptor nsDescriptor = descriptor != null ? descriptor.getNSDescriptor() : null; XmlFile descriptorFile = nsDescriptor != null ? nsDescriptor.getDescriptorFile() : containingFile.getDocument().getProlog().getDoctype() != null ? containingFile : null; if (nsDescriptor != null && (descriptorFile == null || descriptorFile.getName().equals(containingFile.getName() + ".dtd"))) { descriptorFile = containingFile; } return descriptorFile; }
@Nullable private PsiMetaOwner retrieveOwner(final XmlFile file, final String namespace) { if (file == null) { return namespace.equals(XmlUtil.getTargetSchemaNsFromTag(this)) ? this : null; } return file.getDocument(); }
@Override @NotNull public XmlFileHeader getXmlFileHeader(XmlFile file) { return file.isValid() ? ourRootTagCache.get(ROOT_TAG_NS_KEY, file, null).getValue() : XmlFileHeader.EMPTY; }
public void saveXml(XmlFile inFile, User inUser, Lock lock) throws OpenEditException { // Lock lock = getLockManager().lock("system", inFile.getPath(), null ); //this will retry 10 // times then timeout and throw an exception Page page = getPageManager().getPage(inFile.getPath(), false); ContentItem tmp = getPageManager().getRepository().getStub(inFile.getPath() + ".tmp.xml"); tmp.setMakeVersion(false); getXmlUtil().saveXml(inFile.getRoot(), tmp.getOutputStream(), page.getCharacterEncoding()); ContentItem xmlcontent = getPageManager().getRepository().getStub(inFile.getPath()); xmlcontent.setMakeVersion(false); getPageManager() .getRepository() .remove(xmlcontent); // might be a little faster to remove it first getPageManager().getRepository().move(tmp, xmlcontent); getPageManager().firePageModified(page); xmlcontent = getPageManager().getRepository().getStub(inFile.getPath()); inFile.setLastModified(xmlcontent.getLastModified()); inFile.setExist(true); // log.info("Save " + inFile.getPath()); }
@Nullable @Override public MyValidationMessageConsumer collectInformation(@NotNull final PsiFile file) { final FileType type = file.getFileType(); if (type != StdFileTypes.XML && type != RncFileType.getInstance()) { return null; } final XmlFile xmlfile = (XmlFile) file; final XmlDocument document = xmlfile.getDocument(); if (document == null) { return null; } if (type == StdFileTypes.XML) { final XmlTag rootTag = document.getRootTag(); if (rootTag == null) { return null; } if (!ApplicationLoader.RNG_NAMESPACE.equals(rootTag.getNamespace())) { return null; } } else { if (!ApplicationManager.getApplication().isUnitTestMode() && MyErrorFinder.hasError(xmlfile)) { return null; } } final Document doc = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); final MyValidationMessageConsumer consumer = new MyValidationMessageConsumer(); ErrorHandler eh = new DefaultHandler() { @Override public void warning(SAXParseException e) { handleError(e, file, doc, consumer.warning()); } @Override public void error(SAXParseException e) { handleError(e, file, doc, consumer.error()); } }; RngParser.parsePattern(file, eh, true); return consumer; }
protected CachedValue<XmlFileHeader> compute(final XmlFile file, final Object o) { return CachedValuesManager.getManager(file.getProject()) .createCachedValue( new CachedValueProvider<XmlFileHeader>() { public Result<XmlFileHeader> compute() { return new Result<XmlFileHeader>(calcXmlFileHeader(file), file); } }, false); }
@Nullable private XmlNSDescriptor getNsDescriptorFormDocType( final XmlDoctype doctype, final XmlFile containingFile, final boolean forHtml) { XmlNSDescriptor descriptor = getNSDescriptorFromMetaData(doctype.getMarkupDecl(), true); final String filePath = getFilePathForLogging(containingFile); final String dtdUri = XmlUtil.getDtdUri(doctype); LOG.debug( "DTD url for doctype " + doctype.getText() + " in file " + filePath + " is " + dtdUri); if (dtdUri != null && !dtdUri.isEmpty()) { XmlFile xmlFile = XmlUtil.findNamespace(containingFile, dtdUri); if (xmlFile == null) { // try to auto-detect it xmlFile = XmlNamespaceIndex.guessDtd(dtdUri, containingFile); } final String schemaFilePath = getFilePathForLogging(xmlFile); LOG.debug("Schema file for " + filePath + " is " + schemaFilePath); XmlNSDescriptor descriptorFromDtd = getNSDescriptorFromMetaData(xmlFile == null ? null : xmlFile.getDocument(), forHtml); LOG.debug( "Descriptor from meta data for schema file " + schemaFilePath + " is " + (descriptorFromDtd != null ? descriptorFromDtd.getClass().getCanonicalName() : "NULL")); if (descriptor != null && descriptorFromDtd != null) { descriptor = new XmlNSDescriptorSequence(new XmlNSDescriptor[] {descriptor, descriptorFromDtd}); } else if (descriptorFromDtd != null) { descriptor = descriptorFromDtd; } } return descriptor; }
@Nullable private static XmlNSDescriptor getDtdDescriptor(@NotNull XmlFile containingFile) { final XmlDocument document = containingFile.getDocument(); if (document == null) { return null; } final String url = XmlUtil.getDtdUri(document); if (url == null) { return null; } return document.getDefaultNSDescriptor(url, true); }
/** * @deprecated Use the more simple getXml(String) version * @param inId * @param path * @param inElementName * @return * @throws OpenEditException */ public XmlFile getXml(String inId, String path, String inElementName) throws OpenEditException { try { // This can be specified within the page action with a <property // name="xmlfile">./data.xml</property> XmlFile element = null; if (path.startsWith("/WEB-INF/data")) { ContentItem input = getPageManager().getRepository().get(path); element = load(inId, path, inElementName, input); } else { Page input = getPageManager().getPage(path, true); if (element == null || element.getLastModified() != input.lastModified()) { element = load(inId, path, inElementName, input.getContentItem()); } } return element; } catch (Exception e) { String actual = getPageManager().getPage(path).getContentItem().getAbsolutePath(); if (actual == null) { actual = path; } throw new OpenEditException("Path was: " + actual + " Error: " + e.getMessage(), e); } }
public void saveXml(XmlFile inFile, User inUser) throws OpenEditException { Lock lock = getLockManager() .lock( "system", inFile.getPath(), null); // this will retry 10 times then timeout and throw an exception try { saveXml(inFile, inUser, lock); // log.info("Save " + inFile.getPath()); } finally { getLockManager().release("system", lock); } }
private static void collectNamesToImport( @NotNull final Collection<Pair<String, Boolean>> names, @NotNull final Collection<String> demandedForNested, @NotNull XmlFile file) { file.accept( new JavaFxUsedClassesVisitor() { @Override protected void appendClassName(String fqn) { names.add(Pair.create(fqn, false)); } @Override protected void appendDemandedPackageName(@NotNull String packageName) { demandedForNested.add(packageName); } }); }
private static XmlFileHeader computeHeaderByPsi(XmlFile file) { final XmlDocument document = file.getDocument(); if (document == null) { return XmlFileHeader.EMPTY; } String publicId = null; String systemId = null; final XmlProlog prolog = document.getProlog(); if (prolog != null) { final XmlDoctype doctype = prolog.getDoctype(); if (doctype != null) { publicId = doctype.getPublicId(); systemId = doctype.getSystemId(); if (systemId == null) { systemId = doctype.getDtdUri(); } } } final XmlTag tag = document.getRootTag(); if (tag == null) { return XmlFileHeader.EMPTY; } String localName = tag.getLocalName(); if (StringUtil.isNotEmpty(localName)) { if (tag.getPrevSibling() instanceof PsiErrorElement) { return XmlFileHeader.EMPTY; } String psiNs = tag.getNamespace(); return new XmlFileHeader( localName, psiNs == XmlUtil.EMPTY_URI || Comparing.equal(psiNs, systemId) ? null : psiNs, publicId, systemId); } return XmlFileHeader.EMPTY; }
protected XmlFile load(String inId, String path, String inElementName, ContentItem input) throws OpenEditException { // log.info("Loading " + path); boolean found = false; XmlFile element; Element root = null; if (!input.exists()) { if (inElementName == null) { root = DocumentHelper.createElement("root"); } else { if (inElementName.endsWith("y")) { root = DocumentHelper.createElement( inElementName.substring(0, inElementName.length() - 1) + "ies"); } else { root = DocumentHelper.createElement(inElementName + "s"); } } } else { found = true; InputStream in = input.getInputStream(); try { root = getXmlUtil().getXml(in, "UTF-8"); } catch (OpenEditException ex) { log.error("file problem: " + path, ex); throw ex; } finally { FileUtils.safeClose(in); } } element = new XmlFile(); element.setRoot(root); element.setExist(found); element.setElementName(inElementName); element.setPath(path); element.setLastModified(input.lastModified().getTime()); element.setId(inId); return element; }
public static boolean isAutoGeneratedSchema(XmlFile file) { return file.getUserData(AUTO_GENERATED) != null; }
private XmlNSDescriptor getDefaultNSDescriptorInner( final String namespace, final boolean strict) { final XmlFile containingFile = XmlUtil.getContainingFile(this); if (containingFile == null) return null; final XmlProlog prolog = getProlog(); final XmlDoctype doctype = prolog != null ? prolog.getDoctype() : null; boolean dtdUriFromDocTypeIsNamespace = false; if (XmlUtil.HTML_URI.equals(namespace)) { XmlNSDescriptor nsDescriptor = doctype != null ? getNsDescriptorFormDocType(doctype, containingFile, true) : null; if (doctype != null) { LOG.debug( "Descriptor from doctype " + doctype + " is " + (nsDescriptor != null ? nsDescriptor.getClass().getCanonicalName() : "NULL")); } if (nsDescriptor == null) { String htmlns = ExternalResourceManagerEx.getInstanceEx().getDefaultHtmlDoctype(getProject()); if (htmlns.isEmpty()) { htmlns = Html5SchemaProvider.getHtml5SchemaLocation(); } nsDescriptor = getDefaultNSDescriptor(htmlns, false); } return new HtmlNSDescriptorImpl(nsDescriptor); } else if (XmlUtil.XHTML_URI.equals(namespace)) { String xhtmlNamespace = XmlUtil.getDefaultXhtmlNamespace(getProject()); if (xhtmlNamespace == null || xhtmlNamespace.isEmpty()) { xhtmlNamespace = Html5SchemaProvider.getXhtml5SchemaLocation(); } return getDefaultNSDescriptor(xhtmlNamespace, false); } else if (namespace != null && namespace != XmlUtil.EMPTY_URI) { if (doctype == null || !namespace.equals(XmlUtil.getDtdUri(doctype))) { boolean documentIsSchemaThatDefinesNs = namespace.equals(XmlUtil.getTargetSchemaNsFromTag(getRootTag())); final XmlFile xmlFile = documentIsSchemaThatDefinesNs ? containingFile : XmlUtil.findNamespace(containingFile, namespace); if (xmlFile != null) { final XmlDocument document = xmlFile.getDocument(); if (document != null) { return (XmlNSDescriptor) document.getMetaData(); } } } else { dtdUriFromDocTypeIsNamespace = true; } } if (strict && !dtdUriFromDocTypeIsNamespace) return null; if (doctype != null) { XmlNSDescriptor descr = getNsDescriptorFormDocType(doctype, containingFile, false); if (descr != null) { return XmlExtension.getExtension(containingFile) .getDescriptorFromDoctype(containingFile, descr); } } if (strict) return null; if (namespace == XmlUtil.EMPTY_URI) { final XmlFile xmlFile = XmlUtil.findNamespace(containingFile, namespace); if (xmlFile != null) { return (XmlNSDescriptor) xmlFile.getDocument().getMetaData(); } } try { final PsiFile fileFromText = PsiFileFactory.getInstance(getProject()) .createFileFromText( containingFile.getName() + ".dtd", DTDLanguage.INSTANCE, XmlUtil.generateDocumentDTD(this, false), false, false); if (fileFromText instanceof XmlFile) { fileFromText.putUserData(AUTO_GENERATED, Boolean.TRUE); return (XmlNSDescriptor) ((XmlFile) fileFromText).getDocument().getMetaData(); } } catch (ProcessCanceledException ex) { throw ex; } catch (RuntimeException ignored) { } // e.g. dtd isn't mapped to xml type return null; }
public void deleteXmlFile(XmlFile inSettings) throws OpenEditException { Page page = getPageManager().getPage(inSettings.getPath(), true); page.getContentItem().setMakeVersion(false); getPageManager().removePage(page); }
@NotNull @Override public Runnable processFile(final PsiFile file) { VirtualFile vFile = file.getVirtualFile(); if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow) vFile).getDelegate(); final Project project = file.getProject(); if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) { return EmptyRunnable.INSTANCE; } final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>(); final Set<String> demandedForNested = new HashSet<>(); collectNamesToImport(names, demandedForNested, (XmlFile) file); Collections.sort(names, (o1, o2) -> StringUtil.compare(o1.first, o2.first, true)); final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project); final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings); final HashSet<String> onDemand = new HashSet<String>(); ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings); onDemand.addAll(demandedForNested); final Set<String> imported = new HashSet<String>(); final List<String> imports = new ArrayList<String>(); for (Pair<String, Boolean> pair : sortedNames) { final String qName = pair.first; final String packageName = StringUtil.getPackageName(qName); if (imported.contains(packageName) || imported.contains(qName)) { continue; } if (onDemand.contains(packageName)) { imported.add(packageName); imports.add("<?import " + packageName + ".*?>"); } else { imported.add(qName); imports.add("<?import " + qName + "?>"); } } final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject()); final XmlFile dummyFile = (XmlFile) factory.createFileFromText( "_Dummy_.fxml", StdFileTypes.XML, StringUtil.join(imports, "\n")); final XmlDocument document = dummyFile.getDocument(); final XmlProlog newImportList = document != null ? document.getProlog() : null; if (newImportList == null) return EmptyRunnable.getInstance(); return () -> { final XmlDocument xmlDocument = ((XmlFile) file).getDocument(); final XmlProlog prolog = xmlDocument != null ? xmlDocument.getProlog() : null; if (prolog != null) { final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); for (final XmlProcessingInstruction instruction : instructions) { final ASTNode node = instruction.getNode(); final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME); if (nameNode != null && nameNode.getText().equals("import")) { instruction.delete(); } } prolog.add(newImportList); } else { document.addBefore(newImportList, document.getRootTag()); } }; }