/** * React to change of a settable contained in the entity and update the entity's appearance. * * @param entity The entity that contains the settable. * @param settable The settable whose value is changed. * @see ptolemy.kernel.util.ValueListener#valueChanged(Settable) */ public static void valueChanged(GTEntity entity, Settable settable) { GTIngredientsAttribute criteria = entity.getCriteriaAttribute(); if (settable == criteria) { if (GTTools.isInPattern((NamedObj) entity)) { // criteria attribute is used to set the matching criteria for // this actor. It is used only for actors in the pattern of // a transformation rule. If the actor is in the // replacement, this attribute is ignored. entity.updateAppearance(criteria); // Update the appearance of corresponding entities in the // replacement. Pattern pattern = (Pattern) GTTools.getContainingPatternOrReplacement((NamedObj) entity); NamedObj container = pattern.getContainer(); if (container instanceof TransformationRule) { Replacement replacement = ((TransformationRule) container).getReplacement(); replacement.updateEntitiesAppearance(criteria); } } } }
/** * Copies single file from <code>src</code> to <code>dest</code>. If the file is source file, * variable references will be escaped, so they'll survive Velocity template merging. */ private void copyFile(File src, File dest, Replacement replaceFn) throws IOException { if (replaceFn != null && isSourceFile(src)) { String original = FileUtils.readFileToString(src); String escapeDollarSquiggly = original; if (original.contains("${")) { String replaced = original.replaceAll(Pattern.quote("${"), "\\${D}{"); // add Velocity expression at the beginning of the result file. // Velocity is used by mvn archetype:generate escapeDollarSquiggly = "#set( $D = '$' )\n" + replaced; } // do additional replacement String text = replaceFn.replace(escapeDollarSquiggly); FileUtils.write(dest, text); } else { if (LOG.isDebugEnabled()) { LOG.warn( "Not a source dir as the extension is {}", FilenameUtils.getExtension(src.getName())); } FileUtils.copyFile(src, dest); } }
/** * This method: * * <ul> * <li>Copies POM from original project to archetype-resources * <li>Generates <code></code>archetype-descriptor.xml</code> * <li>Generates Archetype's <code>pom.xml</code> if not present in target directory. * </ul> * * @param projectPom POM file of original project * @param archetypeDir target directory of created Maven Archetype project * @param archetypePom created POM file for Maven Archetype project * @param metadataXmlOutFile generated archetype-metadata.xml file * @param replaceFn replace function * @throws IOException */ private void createArchetypeDescriptors( File projectPom, File archetypeDir, File archetypePom, File metadataXmlOutFile, Replacement replaceFn) throws IOException { LOG.debug("Parsing " + projectPom); String text = replaceFn.replace(FileUtils.readFileToString(projectPom)); // lets update the XML Document doc = archetypeUtils.parseXml(new InputSource(new StringReader(text))); Element root = doc.getDocumentElement(); // let's get some values from the original project String originalArtifactId, originalName, originalDescription; Element artifactIdEl = (Element) findChild(root, "artifactId"); Element nameEl = (Element) findChild(root, "name"); Element descriptionEl = (Element) findChild(root, "description"); if (artifactIdEl != null && artifactIdEl.getTextContent() != null && artifactIdEl.getTextContent().trim().length() > 0) { originalArtifactId = artifactIdEl.getTextContent().trim(); } else { originalArtifactId = archetypeDir.getName(); } if (nameEl != null && nameEl.getTextContent() != null && nameEl.getTextContent().trim().length() > 0) { originalName = nameEl.getTextContent().trim(); } else { originalName = originalArtifactId; } if (descriptionEl != null && descriptionEl.getTextContent() != null && descriptionEl.getTextContent().trim().length() > 0) { originalDescription = descriptionEl.getTextContent().trim(); } else { originalDescription = originalName; } Map<String, String> propertyNameSet = new LinkedHashMap<>(); if (root != null) { // remove the parent element and the following text Node NodeList parents = root.getElementsByTagName("parent"); if (parents.getLength() > 0) { if (parents.item(0).getNextSibling().getNodeType() == Node.TEXT_NODE) { root.removeChild(parents.item(0).getNextSibling()); } root.removeChild(parents.item(0)); } // lets load all the properties defined in the <properties> element in the pom. Map<String, String> pomProperties = new LinkedHashMap<>(); NodeList propertyElements = root.getElementsByTagName("properties"); if (propertyElements.getLength() > 0) { Element propertyElement = (Element) propertyElements.item(0); NodeList children = propertyElement.getChildNodes(); for (int cn = 0; cn < children.getLength(); cn++) { Node e = children.item(cn); if (e instanceof Element) { pomProperties.put(e.getNodeName(), e.getTextContent()); } } } if (LOG.isDebugEnabled()) { for (Map.Entry<String, String> entry : pomProperties.entrySet()) { LOG.debug("pom property: {}={}", entry.getKey(), entry.getValue()); } } // lets find all the property names NodeList children = root.getElementsByTagName("*"); for (int cn = 0; cn < children.getLength(); cn++) { Node e = children.item(cn); if (e instanceof Element) { // val text = e.childrenText String cText = e.getTextContent(); String prefix = "${"; if (cText.startsWith(prefix)) { int offset = prefix.length(); int idx = cText.indexOf("}", offset + 1); if (idx > 0) { String name = cText.substring(offset, idx); if (!pomProperties.containsKey(name) && isValidRequiredPropertyName(name)) { // use default value if we have one, but favor value from this pom over the bom pom String value = pomProperties.get(name); if (value == null) { value = versionProperties.get(name); } propertyNameSet.put(name, value); } } } } } String profile = replaceNodeValue(doc, root, "fabric8.profile", "${fabric8-profile}"); if (profile != null) { // we do not want a default name for the profile as the end user should be able to set that // value // and use fabric8-profile as key as there is a problem when using fabric8.profile propertyNameSet.put("fabric8-profile", null); } // now lets replace the contents of some elements (adding new elements if they are not // present) List<String> beforeNames = Arrays.asList( "artifactId", "version", "packaging", "name", "properties", "fabric8-profile"); replaceOrAddElementText(doc, root, "version", "${version}", beforeNames); replaceOrAddElementText(doc, root, "artifactId", "${artifactId}", beforeNames); replaceOrAddElementText(doc, root, "groupId", "${groupId}", beforeNames); } archetypePom.getParentFile().mkdirs(); // remove copyright header which is the first comment, as we do not want that in the archetypes removeCommentNodes(doc); archetypeUtils.writeXmlDocument(doc, archetypePom); // lets update the archetype-metadata.xml file String archetypeXmlText = defaultArchetypeXmlText(); Document archDoc = archetypeUtils.parseXml(new InputSource(new StringReader(archetypeXmlText))); Element archRoot = archDoc.getDocumentElement(); // replace @name attribute on root element archRoot.setAttribute("name", archetypeDir.getName()); LOG.debug(("Found property names: {}"), propertyNameSet); // lets add all the properties Element requiredProperties = replaceOrAddElement(archDoc, archRoot, "requiredProperties", Arrays.asList("fileSets")); // lets add the various properties in for (Map.Entry<String, String> entry : propertyNameSet.entrySet()) { requiredProperties.appendChild(archDoc.createTextNode("\n" + indent + indent)); Element requiredProperty = archDoc.createElement("requiredProperty"); requiredProperties.appendChild(requiredProperty); requiredProperty.setAttribute("key", entry.getKey()); if (entry.getValue() != null) { requiredProperty.appendChild(archDoc.createTextNode("\n" + indent + indent + indent)); Element defaultValue = archDoc.createElement("defaultValue"); requiredProperty.appendChild(defaultValue); defaultValue.appendChild(archDoc.createTextNode(entry.getValue())); } requiredProperty.appendChild(archDoc.createTextNode("\n" + indent + indent)); } requiredProperties.appendChild(archDoc.createTextNode("\n" + indent)); metadataXmlOutFile.getParentFile().mkdirs(); archetypeUtils.writeXmlDocument(archDoc, metadataXmlOutFile); File archetypeProjectPom = new File(archetypeDir, "pom.xml"); // now generate Archetype's pom if (!archetypeProjectPom.exists()) { StringWriter sw = new StringWriter(); IOUtils.copy(getClass().getResourceAsStream("default-archetype-pom.xml"), sw, "UTF-8"); Document pomDocument = archetypeUtils.parseXml(new InputSource(new StringReader(sw.toString()))); List<String> emptyList = Collections.emptyList(); // artifactId = original artifactId with "-archetype" Element artifactId = replaceOrAddElement( pomDocument, pomDocument.getDocumentElement(), "artifactId", emptyList); artifactId.setTextContent(archetypeDir.getName()); // name = "Fabric8 :: Qickstarts :: xxx" -> "Fabric8 :: Archetypes :: xxx" Element name = replaceOrAddElement(pomDocument, pomDocument.getDocumentElement(), "name", emptyList); if (originalName.contains(" :: ")) { String[] originalNameTab = originalName.split(" :: "); if (originalNameTab.length > 2) { StringBuilder sb = new StringBuilder(); sb.append("Fabric8 :: Archetypes"); for (int idx = 2; idx < originalNameTab.length; idx++) { sb.append(" :: ").append(originalNameTab[idx]); } name.setTextContent(sb.toString()); } else { name.setTextContent("Fabric8 :: Archetypes :: " + originalNameTab[1]); } } else { name.setTextContent("Fabric8 :: Archetypes :: " + originalName); } // description = "Creates a new " + originalDescription Element description = replaceOrAddElement( pomDocument, pomDocument.getDocumentElement(), "description", emptyList); description.setTextContent("Creates a new " + originalDescription); archetypeUtils.writeXmlDocument(pomDocument, archetypeProjectPom); } }
public static void main(String[] args) { Replacement test = new Replacement("Hello, World!", "keyword"); System.out.println(test.getWord()); System.out.println(test.decrypt()); System.out.println(test.getKey()); }