/** Use xml:base attributes at feed and entry level to resolve relative links */ private String resolveURI(URL baseURI, Parent parent, String url) { url = (url.equals(".") || url.equals("./")) ? "" : url; if (isRelativeURI(url) && parent != null && parent instanceof Element) { Attribute baseAtt = ((Element)parent).getAttribute("base", Namespace.XML_NAMESPACE); String xmlBase = (baseAtt == null) ? "" : baseAtt.getValue(); if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) { xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/")+1); } return resolveURI(baseURI, parent.getParent(), xmlBase + url); } else if (isRelativeURI(url) && parent == null) { return baseURI + url; } else if (baseURI != null && url.startsWith("/")) { String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost(); if (baseURI.getPort() != baseURI.getDefaultPort()) { hostURI = hostURI + ":" + baseURI.getPort(); } return hostURI + url; } return url; }
private XMLNode createTreeNode(Content content) { XMLNode node = new XMLNode(content); if (content instanceof Parent) { Parent parent = (Parent) content; for (Object child : parent.getContent()) { if (child instanceof Element) node.add(createTreeNode((Content) child)); else if (textSizeLimit != 0 && child instanceof Text) { Text text = (Text) child; if (!text.getTextNormalize().isEmpty()) node.add(createTreeNode(text)); } } } return node; }
/** * Indicates the end of an element * (<code></[element name]></code>) is reached. Note that * the parser does not distinguish between empty * elements and non-empty elements, so this will occur uniformly. * * @param namespaceURI <code>String</code> URI of namespace this * element is associated with * @param localName <code>String</code> name of element without prefix * @param qName <code>String</code> name of element in XML 1.0 form * @throws SAXException when things go wrong */ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (suppress) return; flushCharacters(); if (!atRoot) { Parent p = currentElement.getParent(); if (p instanceof Document) { atRoot = true; } else { currentElement = (Element) p; } } else { throw new SAXException( "Ill-formed XML document (missing opening tag for " + localName + ")"); } }
/** * Method to determine if the navigation file has changed since it was last imported. If so then we need to check for old <items> and remove them from the document * * @param items */ protected void removeOldNodes(final String[] items) { final Vector v = new Vector(); final List itemList = getDocument().getRootElement().getChildren(ITEM_NODE); if (itemList != null && !itemList.isEmpty()) { final Iterator itemListElement = itemList.iterator(); while (itemListElement.hasNext()) { final Element anItem = (Element) itemListElement.next(); if (!doesItemExist(anItem.getAttributeValue(ITEM_IDENTIFIER), items)) { // mark this node - (can't delete at the same time as looping // otherwise get concurrent access errors) v.add(anItem); } } final Iterator itemsToRemove = v.iterator(); while (itemsToRemove.hasNext()) { final Element anItemToDelete = (Element) itemsToRemove.next(); final Parent parent = anItemToDelete.getParent(); if (parent != null) { log.warn("item no longer exists so remove " + anItemToDelete.getAttributeValue(ITEM_IDENTIFIER), null); parent.removeContent(anItemToDelete); } } } v.clear(); }
public void removeByXpath(Set<String> toRemove) throws Exception { for (Iterator idIt = toRemove.iterator(); idIt.hasNext();) { String xpath = (String) idIt.next(); Element element = null; element = getElementByXPath(xpath); if ( element != null ) { Parent p = element.getParent(); p.removeContent(element); } else { log.debug("Could not find "+xpath); } } }
public ShelveChangesManager(final Project project, final MessageBus bus) { super(project); myPathMacroSubstitutor = PathMacroManager.getInstance(myProject).createTrackingSubstitutor(); myBus = bus; mySchemeManager = SchemesManagerFactory.getInstance(project).create(SHELVE_MANAGER_DIR_PATH, new SchemeProcessor<ShelvedChangeList>() { @Nullable @Override public ShelvedChangeList readScheme(@NotNull Element element) throws InvalidDataException { return readOneShelvedChangeList(element); } @Override public Parent writeScheme(@NotNull ShelvedChangeList scheme) throws WriteExternalException { Element child = new Element(ELEMENT_CHANGELIST); scheme.writeExternal(child); myPathMacroSubstitutor.collapsePaths(child); return child; } }); File shelfDirectory = mySchemeManager.getRootDirectory(); myFileProcessor = new CompoundShelfFileProcessor(shelfDirectory); // do not try to ignore when new project created, // because it may lead to predefined ignore creation conflict; see ConvertExcludedToIgnoredTest etc if (shelfDirectory.exists()) { ChangeListManager.getInstance(project).addDirectoryToIgnoreImplicitly(shelfDirectory.getAbsolutePath()); } }
protected void generateForeignMarkup(Element e, List foreignMarkup) { if (foreignMarkup != null) { Iterator elems = (Iterator) foreignMarkup.iterator(); while (elems.hasNext()) { Element elem = (Element) elems.next(); Parent parent = elem.getParent(); if (parent != null) { parent.removeContent(elem); } e.addContent(elem); } } }
/** * Iterator for the descendants of the supplied object. * * @param parent document or element whose descendants will be iterated */ DescendantIterator(Parent parent) { if (parent == null) { throw new IllegalArgumentException("parent parameter was null"); } this.iterator = parent.getContent().iterator(); }
@Nullable static VirtualFile save(@NotNull IFile file, Parent element, Object requestor) throws StateStorageException { try { VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file); Pair<String, String> pair = loadFile(vFile); String text = JDOMUtil.writeParent(element, pair.second); if (file.exists()) { if (text.equals(pair.first)) { return null; } } else { file.createParentDirs(); } // mark this action as modifying the file which daemon analyzer should ignore AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(DocumentRunnable.IgnoreDocumentRunnable.class); try { VirtualFile virtualFile = getOrCreateVirtualFile(requestor, file); byte[] bytes = text.getBytes(CharsetToolkit.UTF8); virtualFile.setBinaryContent(bytes, -1, -1, requestor); return virtualFile; } finally { token.finish(); } } catch (IOException e) { throw new StateStorageException(e); } }
public static boolean isEmpty(@Nullable Parent element) { if (element == null) { return true; } else if (element instanceof Element) { return JDOMUtil.isEmpty((Element)element); } else { Document document = (Document)element; return !document.hasRootElement() || JDOMUtil.isEmpty(document.getRootElement()); } }
@Nullable public static BufferExposingByteArrayOutputStream newContentIfDiffers(@Nonnull Parent element, @Nullable VirtualFile file) { try { Pair<byte[], String> pair = loadFile(file); BufferExposingByteArrayOutputStream out = writeToBytes(element, pair.second); return pair.first != null && equal(pair.first, out) ? null : out; } catch (IOException e) { LOG.debug(e); return null; } }
public static void sendContent(@Nonnull StreamProvider provider, @Nonnull String fileSpec, @Nonnull Parent element, @Nonnull RoamingType type) { if (!provider.isApplicable(fileSpec, type)) { return; } try { doSendContent(provider, fileSpec, element, type); } catch (IOException e) { LOG.warn(e); } }
@Override public Parent writeScheme(@Nonnull final ToolsGroup<T> scheme) throws WriteExternalException { Element groupElement = new Element(TOOL_SET); if (scheme.getName() != null) { groupElement.setAttribute(ATTRIBUTE_NAME, scheme.getName()); } for (T tool : scheme.getElements()) { saveTool(tool, groupElement); } return groupElement; }
@Nonnull public static BufferExposingByteArrayOutputStream writeToBytes(@Nonnull Parent element, @Nonnull String lineSeparator) throws IOException { BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(512); JDOMUtil.writeParent(element, out, lineSeparator); return out; }
@Nonnull public static BufferExposingByteArrayOutputStream elementToBytes(@Nonnull Parent element, boolean useSystemLineSeparator) throws IOException { return writeToBytes(element, useSystemLineSeparator ? SystemProperties.getLineSeparator() : "\n"); }
/** * You must call {@link StreamProvider#isApplicable(String, com.intellij.openapi.components.RoamingType)} before */ public static void doSendContent(@Nonnull StreamProvider provider, @Nonnull String fileSpec, @Nonnull Parent element, @Nonnull RoamingType type) throws IOException { // we should use standard line-separator (\n) - stream provider can share file content on any OS BufferExposingByteArrayOutputStream content = elementToBytes(element, false); provider.saveContent(fileSpec, content.getInternalBuffer(), content.size(), type); }
public ShelveChangesManager(final Project project, final MessageBus bus) { super(project); myPathMacroSubstitutor = PathMacroManager.getInstance(myProject).createTrackingSubstitutor(); myBus = bus; mySchemeManager = SchemesManagerFactory.getInstance().createSchemesManager(SHELVE_MANAGER_DIR_PATH, new BaseSchemeProcessor<ShelvedChangeList>() { @Nullable @Override public ShelvedChangeList readScheme(@Nonnull Element element, boolean duringLoad) throws InvalidDataException { return readOneShelvedChangeList(element); } @Nonnull @Override public Parent writeScheme(@Nonnull ShelvedChangeList scheme) throws WriteExternalException { Element child = new Element(ELEMENT_CHANGELIST); scheme.writeExternal(child); myPathMacroSubstitutor.collapsePaths(child); return child; } }, RoamingType.PER_USER); myCleaningFuture = JobScheduler.getScheduler().scheduleWithFixedDelay(new Runnable() { @Override public void run() { cleanSystemUnshelvedOlderOneWeek(); } }, 1, 1, TimeUnit.DAYS); Disposer.register(project, new Disposable() { @Override public void dispose() { stopCleanScheduler(); } }); File shelfDirectory = mySchemeManager.getRootDirectory(); myFileProcessor = new CompoundShelfFileProcessor(shelfDirectory); // do not try to ignore when new project created, // because it may lead to predefined ignore creation conflict; see ConvertExcludedToIgnoredTest etc if (shelfDirectory.exists()) { ChangeListManager.getInstance(project).addDirectoryToIgnoreImplicitly(shelfDirectory.getAbsolutePath()); } }
/** * } * Resolve URI based considering xml:base and baseURI. * @param baseURI Base URI of feed * @param parent Parent from which to consider xml:base * @param url URL to be resolved */ private String resolveURI(String baseURI, Parent parent, String url) { if (isRelativeURI(url)) { url = (!".".equals(url) && !"./".equals(url)) ? url : ""; // Relative URI with parent if (parent != null && parent instanceof Element) { // Do we have an xml:base? String xmlbase = ((Element)parent).getAttributeValue( "base", Namespace.XML_NAMESPACE); if (xmlbase != null && xmlbase.trim().length() > 0) { if (isAbsoluteURI(xmlbase)) { // Absolute xml:base, so form URI right now if (url.startsWith("/")) { // Host relative URI int slashslash = xmlbase.indexOf("//"); int nextslash = xmlbase.indexOf("/", slashslash + 2); if (nextslash != -1) xmlbase = xmlbase.substring(0, nextslash); return formURI(xmlbase, url); } if (!xmlbase.endsWith("/")) { // Base URI is filename, strip it off xmlbase = xmlbase.substring(0, xmlbase.lastIndexOf("/")); } return formURI(xmlbase, url); } else { // Relative xml:base, so walk up tree return resolveURI(baseURI, parent.getParent(), stripTrailingSlash(xmlbase) + "/"+ stripStartingSlash(url)); } } // No xml:base so walk up tree return resolveURI(baseURI, parent.getParent(), url); // Relative URI with no parent (i.e. top of tree), so form URI right now } else if (parent == null || parent instanceof Document) { return formURI(baseURI, url); } } return url; }
public abstract Parent writeScheme(@NotNull T scheme) throws WriteExternalException;
Parent writeScheme(@Nonnull T scheme) throws WriteExternalException;