neilcsmith-net commented on code in PR #9449:
URL: https://github.com/apache/netbeans/pull/9449#discussion_r3441368083


##########
nb/o.n.upgrader/src/org/netbeans/upgrade/CopyFiles.java:
##########
@@ -232,6 +256,79 @@ private EditableProperties getProperties(String 
relativePath) throws IOException
         return properties;
     }
 
+    /**
+     * Copies var/attributes.xml, filtering to keep only fileobject entries 
that
+     * contain an AuxilaryConfiguration attribute.
+     *
+     * @param sourceFile source attributes.xml
+     * @param targetFile target attributes.xml
+     * @throws IOException if parsing or writing fails
+     */
+    private static void copyFilteredAttributesXml(File sourceFile, File 
targetFile) throws IOException {
+        try {
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd";,
 false); //NOI18N
+            DocumentBuilder db = dbf.newDocumentBuilder();
+            Document doc = db.parse(sourceFile);
+            Element root = doc.getDocumentElement();
+            NodeList children = root.getChildNodes();
+
+            // Collect fileobject nodes to remove (can't modify NodeList while 
iterating)
+            List<Node> toRemove = new ArrayList<>();
+            for (int i = 0; i < children.getLength(); i++) {
+                if (children.item(i) instanceof Element el
+                        && "fileobject".equals(el.getTagName()) //NOI18N
+                        && !hasAuxiliaryConfiguration(el)) {
+                    toRemove.add(el);
+                }
+            }
+
+            for (Node node : toRemove) {
+                root.removeChild(node);
+            }
+
+            // Only write if there are remaining entries
+            if (root.getElementsByTagName("fileobject").getLength() == 0) {  
//NOI18N
+                LOGGER.log(Level.FINE, "No AuxiliaryConfiguration entries 
found in {0}, skipping", sourceFile);  //NOI18N
+                return;
+            }
+
+            TransformerFactory tf = TransformerFactory.newInstance();
+            Transformer transformer = tf.newTransformer();
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");  //NOI18N
+            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
+                    "-//NetBeans//DTD DefaultAttributes 1.0//EN"); //NOI18N
+            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
+                    "http://www.netbeans.org/dtds/attributes-1_0.dtd";); 
//NOI18N
+            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
//NOI18N
+            ensureParent(targetFile);
+            try (OutputStream out = new FileOutputStream(targetFile)) {
+                transformer.transform(new DOMSource(doc), new 
StreamResult(out));
+            }
+            LOGGER.log(Level.FINE, "Filtered attributes.xml copied to {0}", 
targetFile);  //NOI18N
+        } catch (ParserConfigurationException | SAXException | 
TransformerException ex) {
+            throw new IOException("Failed to filter attributes.xml: " + 
ex.getMessage(), ex);  //NOI18N
+        }
+    }
+
+    /**
+     * Checks whether a fileobject element contains an AuxilaryConfiguration
+     * attribute.
+     *
+     * @param fileObject the fileobject element to check
+     * @return true if the element has a AuxiliaryConfiguration attribute
+     */
+    private static boolean hasAuxiliaryConfiguration(Element fileObject) {
+        NodeList attrs = fileObject.getElementsByTagName("attr");  //NOI18N
+        for (int i = 0; i < attrs.getLength(); i++) {
+            Element attr = (Element) attrs.item(i);
+            if ("AuxilaryConfiguration".equals(attr.getAttribute("name"))) {  
//NOI18N

Review Comment:
   This should possibly check if the `name` attribute contains (not equals) 
either `AuxilaryConfiguration` or `AuxiliaryConfiguration`.  The default 
fallback implementation of this uses classname (without typo) plus additional 
namespace for the attribute.
   
   
https://github.com/apache/netbeans/blob/master/ide/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigImpl.java#L96



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to