Java Code Examples for org.jboss.forge.addon.resource.FileResource#setContents()
The following examples show how to use
org.jboss.forge.addon.resource.FileResource#setContents() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigureEndpointPropertiesStep.java From fabric8-forge with Apache License 2.0 | 6 votes |
private Result editEndpointXml(List<String> lines, String lineNumber, String endpointUrl, String uri, FileResource file, String xml) { // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumber) - 1; String line = lines.get(idx); // replace uri with new value line = StringHelper.replaceAll(line, endpointUrl, uri); lines.set(idx, line); LOG.info("Updating " + endpointUrl + " to " + uri + " at line " + lineNumber + " in file " + xml); // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Update endpoint uri: " + uri + " in file " + xml); }
Example 2
Source File: ConfigureEndpointPropertiesStep.java From fabric8-forge with Apache License 2.0 | 6 votes |
protected Result editEndpointOther(Project project, ResourcesFacet facet, FileResource file, String uri, String endpointUrl, String currentFile, String lineNumber) throws Exception { List<String> lines = LineNumberHelper.readLines(file.getResourceInputStream()); // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumber) - 1; String line = lines.get(idx); // replace uri with new value line = StringHelper.replaceAll(line, endpointUrl, uri); lines.set(idx, line); LOG.info("Updating " + endpointUrl + " to " + uri + " at line " + lineNumber + " in file " + currentFile); // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Update endpoint uri: " + uri + " in file " + currentFile); }
Example 3
Source File: EditNodeXmlStep.java From fabric8-forge with Apache License 2.0 | 5 votes |
@Override protected Result editModelXml(String pattern, List<String> lines, String lineNumber, String lineNumberEnd, String modelXml, FileResource file, String xml) throws Exception { // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumber) - 1; int idx2 = Integer.valueOf(lineNumberEnd) - 1; int delta = (idx2 - idx) + 1; // use the same indent from the eip we are replacing int spaces = LineNumberHelper.leadingSpaces(lines, idx); // remove the old lines while (delta > 0) { delta--; lines.remove(idx); } LOG.info("Spaces " + spaces); String[] editLines = modelXml.split("\n"); for (String line : editLines) { // use the same indent from the eip we are replacing line = LineNumberHelper.padString(line, spaces); // add the new line at the old starting position lines.add(idx, line); idx++; } // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Edited: " + modelXml); }
Example 4
Source File: ConfigureEndpointPropertiesStep.java From fabric8-forge with Apache License 2.0 | 5 votes |
private Result addEndpointXml(FileResource file, String uri, String endpointInstanceName, String xml, String cursorPosition) throws Exception { LOG.info("Adding uri " + uri + " at position " + cursorPosition + " in file " + xml); // we do not want to change the current code formatting so we need to search // replace the unformatted class source code StringBuilder sb = new StringBuilder(file.getContents()); int pos = Integer.valueOf(cursorPosition); // move to end if pos is after the content pos = Math.min(sb.length(), pos); LOG.info("Adding endpoint at pos: " + pos + " in file: " + xml); // check if prev and next is a quote and if not then add it automatic int prev = pos - 1; int next = pos + 1; char ch = sb.charAt(prev); char ch2 = next < sb.length() ? sb.charAt(next) : ' '; if (ch != '"' && ch2 != '"') { uri = "\"" + uri + "\""; } // insert uri at position sb.insert(pos, uri); String text = sb.toString(); // use this code currently to save content unformatted file.setContents(text); return Results.success("Added endpoint " + uri + " in " + xml); }
Example 5
Source File: EditFromOrToEndpointXmlStep.java From fabric8-forge with Apache License 2.0 | 5 votes |
@Override protected Result addOrEditEndpointXml(FileResource file, String uri, String endpointUrl, String endpointInstanceName, String xml, String lineNumber, String lineNumberEnd) throws Exception { String key = parentNode.getKey(); if (Strings.isNullOrBlank(key)) { return Results.fail("Parent node has no key! " + parentNode + " in file " + file.getName()); } Document root = XmlLineNumberParser.parseXml(file.getResourceInputStream()); if (root != null) { Node selectedNode = CamelXmlHelper.findCamelNodeInDocument(root, key); if (selectedNode != null) { // we need to add after the parent node, so use line number information from the parent lineNumber = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER); lineNumberEnd = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER_END); if (lineNumber != null && lineNumberEnd != null) { // read all the lines List<String> lines = LineNumberHelper.readLines(file.getResourceInputStream()); // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumber) - 1; String line = lines.get(idx); // replace uri with new value line = StringHelper.replaceAll(line, endpointUrl, uri); lines.set(idx, line); // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Updated: " + line.trim()); } } return Results.fail("Cannot find Camel node in XML file: " + key); } else { return Results.fail("Cannot load Camel XML file: " + file.getName()); } }
Example 6
Source File: AddNodeXmlStep.java From fabric8-forge with Apache License 2.0 | 5 votes |
@Override protected Result addModelXml(String pattern, List<String> lines, String lineNumber, String lineNumberEnd, String modelXml, FileResource file, String xml) throws Exception { // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumberEnd); if ("route".equals(pattern)) { // we want to add at the end of the existing route, not after the route idx = idx - 1; } // use the same indent from the parent line int spaces = LineNumberHelper.leadingSpaces(lines, idx - 1); LOG.info("Spaces " + spaces); String[] editLines = modelXml.split("\n"); for (String line : editLines) { // use the same indent from the eip we are replacing line = LineNumberHelper.padString(line, spaces); // add the new line at the old starting position lines.add(idx, line); idx++; } // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Added: " + modelXml); }
Example 7
Source File: ConfigureEndpointPropertiesStep.java From fabric8-forge with Apache License 2.0 | 4 votes |
protected Result addEndpointOther(Project project, ResourcesFacet facet, FileResource file, String uri, String currentFile, String cursorPosition) throws Exception { StringBuilder sb = new StringBuilder(file.getContents()); int pos = Integer.valueOf(cursorPosition); // move to end if pos is after the content pos = Math.min(sb.length(), pos); LOG.info("Adding endpoint at pos: " + pos + " in file: " + currentFile); // insert uri at position sb.insert(pos, uri); // use this code currently to save content unformatted file.setContents(sb.toString()); return Results.success("Added endpoint " + uri + " in " + currentFile); }
Example 8
Source File: AddFromOrToEndpointXmlStep.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override protected Result addOrEditEndpointXml(FileResource file, String uri, String endpointUrl, String endpointInstanceName, String xml, String lineNumber, String lineNumberEnd) throws Exception { String key = parentNode.getKey(); if (Strings.isNullOrBlank(key)) { return Results.fail("Parent node has no key! " + parentNode + " in file " + file.getName()); } Document root = XmlLineNumberParser.parseXml(file.getResourceInputStream()); if (root != null) { Node selectedNode = CamelXmlHelper.findCamelNodeInDocument(root, key); if (selectedNode != null) { // we need to add after the parent node, so use line number information from the parent lineNumber = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER); lineNumberEnd = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER_END); LOG.info("Add endpoint at line number " + lineNumber + "-" + lineNumberEnd + " at selected node " + selectedNode.getNodeName()); if (lineNumber != null && lineNumberEnd != null) { String line; if (isFrom) { line = String.format("<from uri=\"%s\"/>", uri); } else { line = String.format("<to uri=\"%s\"/>", uri); } // read all the lines List<String> lines = LineNumberHelper.readLines(file.getResourceInputStream()); // the list is 0-based, and line number is 1-based // if from then use the start line number, otherwise use the end line number int idx = isFrom ? Integer.valueOf(lineNumber) : Integer.valueOf(lineNumberEnd); // use the same indent from the parent line int spaces = LineNumberHelper.leadingSpaces(lines, idx - 1); if (isFrom) { // and append 2 if we are starting a new route with <from> spaces += 2; } line = LineNumberHelper.padString(line, spaces); // add the line at the position lines.add(idx, line); // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Added: " + line.trim()); } } return Results.fail("Cannot find Camel node in XML file: " + key); } else { return Results.fail("Cannot load Camel XML file: " + file.getName()); } }
Example 9
Source File: AddRouteFromEndpointXmlStep.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override protected Result addOrEditEndpointXml(FileResource file, String uri, String endpointUrl, String endpointInstanceName, String xml, String lineNumber, String lineNumberEnd) throws Exception { Document root = XmlLineNumberParser.parseXml(file.getResourceInputStream()); if (root != null) { NodeList camels = getCamelContextElements(root); // TODO: what about 2+ camel's ? if (camels != null && camels.getLength() == 1) { Node camel = camels.item(0); Node camelContext = null; for (int i = 0; i < camel.getChildNodes().getLength(); i++) { if (CamelXmlHelper.isCamelContextOrRoutesNode(camel)) { camelContext = camel; } Node child = camel.getChildNodes().item(i); if (CamelXmlHelper.isCamelContextOrRoutesNode(child)) { camelContext = child; } } if (camelContext == null) { return Results.fail("Cannot find <camelContext> or <routes> in XML file " + xml); } // we need to add after the parent node, so use line number information from the parent lineNumber = (String) camelContext.getUserData(XmlLineNumberParser.LINE_NUMBER); lineNumberEnd = (String) camelContext.getUserData(XmlLineNumberParser.LINE_NUMBER_END); if (lineNumber != null && lineNumberEnd != null) { String line1; if (routeId != null) { line1 = String.format("<route id=\"%s\">", routeId); } else { line1 = "<route>"; } String line2 = String.format("<from uri=\"%s\"/>", uri); String line3 = "</route>"; // if we created a new endpoint, then insert a new line with the endpoint details List<String> lines = LineNumberHelper.readLines(file.getResourceInputStream()); // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumberEnd) - 1; int spaces = LineNumberHelper.leadingSpaces(lines, idx); line3 = LineNumberHelper.padString(line3, spaces + 2); line2 = LineNumberHelper.padString(line2, spaces + 4); line1 = LineNumberHelper.padString(line1, spaces + 2); // check if previous line is empty or not String text = lines.get(idx - 1); boolean emptyLine = text == null || text.trim().isEmpty(); lines.add(idx, ""); lines.add(idx, line3); lines.add(idx, line2); lines.add(idx, line1); if (!emptyLine) { // insert empty lines around the added route (if needed to avoid 2x empty lines) lines.add(idx, ""); } // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); } return Results.success("Added route"); } return Results.fail("Cannot find Camel node in XML file: " + xml); } else { return Results.fail("Could not load camel XML"); } }
Example 10
Source File: CamelDeleteNodeXmlCommand.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public Result execute(UIExecutionContext context) throws Exception { Project project = getSelectedProject(context); NodeDto nodeValue = null; int selectedIdx = node.getSelectedIndex(); if (selectedIdx != -1) { nodeValue = nodes.get(selectedIdx); } if (nodeValue == null) { return Results.fail("No node to delete!"); } String key = nodeValue.getKey(); if (Strings.isNullOrBlank(key)) { return Results.fail("Selected node does not have a key so cannot delete it!"); } String xmlResourceName = xml.getValue(); FileResource file = getXmlResourceFile(project, xmlResourceName); if (file == null || !file.exists()) { return Results.fail("Cannot find XML file " + xmlResourceName); } List<ContextDto> camelContexts = CamelXmlHelper.loadCamelContext(getCamelCatalog(), context.getUIContext(), project, xmlResourceName); if (camelContexts == null) { return Results.fail("No file found for: " + xmlResourceName); } Document root = XmlLineNumberParser.parseXml(file.getResourceInputStream()); if (root != null) { Node selectedNode = CamelXmlHelper.findCamelNodeInDocument(root, key); if (selectedNode != null) { // we need to add after the parent node, so use line number information from the parent String lineNumber = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER); String lineNumberEnd = (String) selectedNode.getUserData(XmlLineNumberParser.LINE_NUMBER_END); if (lineNumber != null && lineNumberEnd != null) { // read all the lines List<String> lines = LineNumberHelper.readLines(file.getResourceInputStream()); // the list is 0-based, and line number is 1-based int idx = Integer.valueOf(lineNumber) - 1; int idx2 = Integer.valueOf(lineNumberEnd) - 1; int delta = (idx2 - idx) + 1; // remove the lines while (delta > 0) { delta--; lines.remove(idx); } // and save the file back String content = LineNumberHelper.linesToString(lines); file.setContents(content); return Results.success("Removed node"); } } return Results.fail("Cannot find Camel node in XML file: " + nodeValue); } else { return Results.fail("Cannot load Camel XML file: " + file.getName()); } }