diff --git a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/Bundle.properties b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/Bundle.properties --- a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/Bundle.properties +++ b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/Bundle.properties @@ -49,7 +49,8 @@ LBL_ByExtension=by &Filename Extension LBL_Extension=&Extension(s)\: LBL_ByElement=by &XML Root Element -LBL_Element=&Namespace\: +LBL_Namespace=&Namespace\: +LBL_ElementName=&Element Name\: ACS_FileRecognitionPanel=File Recognition Panel ACS_CTL_ByElement=Recognize by xml namespace @@ -93,4 +94,6 @@ MSG_NoExtension=Please specify at least one file extension. MSG_BadExtensionPattern=Invalid extension pattern. Please enter a list of extensions separated by spaces or commas. MSG_BadMimeTypeForXML=Inappropriate MIME type. Must be one of "text/myformat+xml" or "application/myformat+xml". +MSG_BadElementName=Invalid element name. MSG_EmptyMIMEType=Please specify MIME type +MSG_EmptyElementOrNamespace=Please specify an element name and/or a namespace. diff --git a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.form b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.form --- a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.form +++ b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.form @@ -1,4 +1,4 @@ - +
@@ -99,19 +99,19 @@ - + - + - + @@ -129,5 +129,27 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.java b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.java --- a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.java +++ b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanel.java @@ -67,6 +67,13 @@ private static final Pattern EXTENSION_PATTERN = Pattern.compile("([.]?[a-zA-Z0-9_]+){1}([ ,]+[.]?[a-zA-Z0-9_]+)*[ ]*"); // NOI18N private static final Pattern ELEMENT_PATTERN = Pattern.compile("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.-])*\\+xml)"); // NOI18N private static final Pattern MIME_TYPE_PATTERN = Pattern.compile("[\\w.]+(?:[+-][\\w.]+)?/[\\w.]+(?:[+-][\\w.]+)*"); // NOI18N + private static final Pattern ROOT_ELEMENT_PATTERN = Pattern.compile("^[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02ff\\u0370-\\u037d" + + "\\u037f-\\u1fff\\u200c-\\u200d\\u2070-\\u218f\\u2c00-\\u2fef\\u3001-\\ud7ff" + + "\\uf900-\\ufdcf\\ufdf0-\\ufffd]" + + "[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02ff\\u0370-\\u037d" + + "\\u037f-\\u1fff\\u200c-\\u200d\\u2070-\\u218f\\u2c00-\\u2fef\\u3001-\\ud7ff" + + "\\uf900-\\ufdcf\\ufdf0-\\ufffd\\-\\.0-9" + + "\\u00b7\\u0300-\\u036f\\u203f-\\u2040]*\\Z"); // NOI18N private NewLoaderIterator.DataModel data; private ButtonGroup group; @@ -88,6 +95,7 @@ public void actionPerformed(ActionEvent event) { txtExtension.setEnabled(rbByExtension.isSelected()); txtNamespace.setEnabled(rbByElement.isSelected()); + txtElementName.setEnabled(rbByElement.isSelected()); checkValidity(); } }; @@ -103,7 +111,7 @@ putClientProperty("NewFileWizard_Title", getMessage("LBL_LoaderWizardTitle")); } - static String checkValidity(AtomicBoolean error, String mimeType, String namespace, String extension, boolean byElement) { + static String checkValidity(AtomicBoolean error, String mimeType, String namespace, String elementName, String extension, boolean byElement) { if (mimeType.isEmpty()) { return getMessage("MSG_EmptyMIMEType"); } else if (!MIME_TYPE_PATTERN.matcher(mimeType).matches()) { @@ -111,13 +119,14 @@ return getMessage("MSG_NotValidMimeType"); } else { if (byElement) { - if (namespace.isEmpty()) { - return getMessage("MSG_NoNamespace"); - } else { - if (!ELEMENT_PATTERN.matcher(mimeType).matches()) { - error.set(true); - return getMessage("MSG_BadMimeTypeForXML"); - } + if(!ELEMENT_PATTERN.matcher(mimeType).matches()) { + error.set(true); + return getMessage("MSG_BadMimeTypeForXML"); + } else if (elementName.isEmpty() && namespace.isEmpty()) { + return getMessage("MSG_EmptyElementOrNamespace"); + } else if (!elementName.isEmpty() && !ROOT_ELEMENT_PATTERN.matcher(elementName).matches()) { + error.set(true); + return getMessage("MSG_BadElementName"); } } else { if (extension.isEmpty()) { @@ -136,7 +145,7 @@ private void checkValidity() { AtomicBoolean error = new AtomicBoolean(); String msg = checkValidity(error, txtMimeType.getText().trim(), txtNamespace.getText().trim(), - txtExtension.getText().trim(), rbByElement.isSelected()); + txtElementName.getText().trim(), txtExtension.getText().trim(), rbByElement.isSelected()); if (msg == null) { markValid(); } else if (error.get()) { @@ -161,6 +170,7 @@ private void attachDocumentListeners() { if (!listenersAttached) { txtNamespace.getDocument().addDocumentListener(docList); + txtElementName.getDocument().addDocumentListener(docList); txtExtension.getDocument().addDocumentListener(docList); txtMimeType.getDocument().addDocumentListener(docList); listenersAttached = true; @@ -170,6 +180,7 @@ private void removeDocumentListeners() { if (listenersAttached) { txtNamespace.getDocument().removeDocumentListener(docList); + txtElementName.getDocument().removeDocumentListener(docList); txtExtension.getDocument().removeDocumentListener(docList); txtMimeType.getDocument().removeDocumentListener(docList); listenersAttached = false; @@ -183,9 +194,11 @@ if (data.isExtensionBased()) { data.setExtension(txtExtension.getText().trim()); data.setNamespace(null); + data.setElementName(null); } else { data.setExtension(null); data.setNamespace(txtNamespace.getText().trim()); + data.setElementName(txtElementName.getText().trim()); } } @@ -202,8 +215,10 @@ } txtExtension.setEnabled(rbByExtension.isSelected()); txtNamespace.setEnabled(rbByElement.isSelected()); + txtElementName.setEnabled(rbByElement.isSelected()); txtExtension.setText(data.getExtension()); txtNamespace.setText(data.getNamespace()); + txtElementName.setText(data.getElementName()); checkValidity(); } @@ -238,6 +253,8 @@ lblNamespace = new javax.swing.JLabel(); txtNamespace = new javax.swing.JTextField(); mimeTypeHint = new javax.swing.JLabel(); + lblElementName = new javax.swing.JLabel(); + txtElementName = new javax.swing.JTextField(); setLayout(new java.awt.GridBagLayout()); @@ -292,17 +309,17 @@ add(rbByElement, gridBagConstraints); lblNamespace.setLabelFor(txtNamespace); - org.openide.awt.Mnemonics.setLocalizedText(lblNamespace, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_Element")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(lblNamespace, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_Namespace")); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 6; + gridBagConstraints.gridy = 7; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0); add(lblNamespace, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 6; + gridBagConstraints.gridy = 7; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weighty = 0.1; @@ -319,15 +336,34 @@ add(mimeTypeHint, gridBagConstraints); mimeTypeHint.getAccessibleContext().setAccessibleName("MIME Type Hint"); mimeTypeHint.getAccessibleContext().setAccessibleDescription("MIME Type Hint"); + + lblElementName.setLabelFor(txtElementName); + org.openide.awt.Mnemonics.setLocalizedText(lblElementName, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_ElementName")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 6; + gridBagConstraints.gridwidth = 2; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; + gridBagConstraints.insets = new java.awt.Insets(6, 20, 0, 0); + add(lblElementName, gridBagConstraints); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 6; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; + gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0); + add(txtElementName, gridBagConstraints); }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel lblElementName; private javax.swing.JLabel lblExtension; private javax.swing.JLabel lblMimeType; private javax.swing.JLabel lblNamespace; private javax.swing.JLabel mimeTypeHint; private javax.swing.JRadioButton rbByElement; private javax.swing.JRadioButton rbByExtension; + private javax.swing.JTextField txtElementName; private javax.swing.JTextField txtExtension; private javax.swing.JTextField txtMimeType; private javax.swing.JTextField txtNamespace; diff --git a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/NewLoaderIterator.java b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/NewLoaderIterator.java --- a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/NewLoaderIterator.java +++ b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/NewLoaderIterator.java @@ -122,6 +122,7 @@ private boolean extensionBased = true; private String extension; private String namespace; + private String elementName; private boolean useMultiview; private CreatedModifiedFiles files; @@ -209,6 +210,14 @@ public void setNamespace(String namespace) { this.namespace = namespace; } + + public String getElementName() { + return elementName; + } + + public void setElementName(String elementName) { + this.elementName = elementName; + } } @@ -235,7 +244,7 @@ replaceTokens.put("PACKAGENAME", packageName);//NOI18N replaceTokens.put("MIMETYPE", mime);//NOI18N replaceTokens.put("EXTENSIONS", formatExtensions(model.isExtensionBased(), model.getExtension(), mime));//NOI18N - replaceTokens.put("NAMESPACES", formatNameSpace(model.isExtensionBased(), model.getNamespace(), mime));//NOI18N + replaceTokens.put("NAMESPACES", formatNameSpace(model.isExtensionBased(), model.getNamespace(), model.getElementName(), mime));//NOI18N // Copy action icon File origIconPath = model.getIconPath(); @@ -428,6 +437,7 @@ suffix = "Template.xml"; // NOI18N try { replaceTokens.put("NAMESPACE", XMLUtil.toElementContent(model.getNamespace())); // NOI18N + replaceTokens.put("ELEMENT", model.getElementName().isEmpty()? "root": XMLUtil.toElementContent(model.getElementName())); // NOI18N } catch (CharConversionException ex) { assert false: ex; } @@ -489,7 +499,7 @@ return element; } - private static String formatNameSpace(boolean isExtensionBased, String namespace, String mime) { + private static String formatNameSpace(boolean isExtensionBased, String namespace, java.lang.String elementName, String mime) { if (isExtensionBased) { return ""; } @@ -498,7 +508,10 @@ buff.append(" \n"); //NOI18N buff.append(" \n"); // NOI18N try { - buff.append(" \n"); //NOI18N + StringBuffer attrStringBuff = new StringBuffer(); + if(!elementName.isEmpty()) attrStringBuff.append(" name=\"").append(XMLUtil.toElementContent(elementName)).append("\""); //NOI18N + if(!namespace.isEmpty()) attrStringBuff.append(" ns=\"").append(XMLUtil.toElementContent(namespace)).append("\""); //NOI18N + buff.append(" \n"); //NOI18N } catch (CharConversionException ex) { assert false : ex; } diff --git a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/templateNew2 b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/templateNew2 --- a/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/templateNew2 +++ b/apisupport.wizards/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/templateNew2 @@ -1,2 +1,2 @@ - - \ No newline at end of file +<${ELEMENT} xmlns="${NAMESPACE}"> + \ No newline at end of file diff --git a/apisupport.wizards/test/unit/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanelTest.java b/apisupport.wizards/test/unit/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanelTest.java --- a/apisupport.wizards/test/unit/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanelTest.java +++ b/apisupport.wizards/test/unit/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/FileRecognitionPanelTest.java @@ -52,20 +52,28 @@ } public void testCheckValidity() throws Exception { - assertValidity(Result.VALID, "text/x-foo", "", "foo", false); - assertValidity(Result.INFO, "", "", "foo", false); - assertValidity(Result.ERROR, "some-type", "", "foo", false); - assertValidity(Result.INFO, "text/x-foo", "", "", false); - assertValidity(Result.ERROR, "text/x-foo", "", "bad/ext", false); - assertValidity(Result.INFO, "text/x-foo", "", "", true); - assertValidity(Result.ERROR, "text/docbook", "whatever", "", true); - assertValidity(Result.VALID, "text/docbook+xml", "whatever", "", true); - assertValidity(Result.VALID, "text/x-docbook+xml", "whatever", "", true); + assertValidity(Result.VALID, "text/x-foo", "", "", "foo", false); + assertValidity(Result.INFO, "", "", "", "foo", false); + assertValidity(Result.ERROR, "some-type", "", "", "foo", false); + assertValidity(Result.INFO, "text/x-foo", "", "", "", false); + assertValidity(Result.ERROR, "text/x-foo", "", "", "bad/ext", false); + assertValidity(Result.ERROR, "text/x-foo", "", "", "", true); + assertValidity(Result.ERROR, "text/docbook", "whatever", "", "", true); + assertValidity(Result.ERROR, "text/docbook", "", "whatever", "", true); + assertValidity(Result.VALID, "text/docbook+xml", "", "whatever", "", true); + assertValidity(Result.VALID, "text/x-docbook+xml", "", "whatever", "", true); + assertValidity(Result.VALID, "text/x-docbook+xml", "whatever", "", "", true); + assertValidity(Result.ERROR, "text/x-docbook+xml", "7whatever", "", "", true); + assertValidity(Result.ERROR, "text/x-docbook+xml", "what ever", "", "", true); + assertValidity(Result.ERROR, "text/x-docbook+xml", "what&ever", "", "", true); + assertValidity(Result.VALID, "text/x-docbook+xml", "äøñ", "", "", true); + assertValidity(Result.VALID, "text/x-docbook+xml", "whatever", "whatever", "", true); + assertValidity(Result.ERROR, "text/x-docbook+xml", "7whatever", "whatever", "", true); } enum Result {VALID, INFO, ERROR} - private static void assertValidity(Result expected, String mimeType, String namespace, String extension, boolean byElement) { + private static void assertValidity(Result expected, String mimeType, java.lang.String elementName, String namespace, String extension, boolean byElement) { AtomicBoolean error = new AtomicBoolean(); - String msg = FileRecognitionPanel.checkValidity(error, mimeType, namespace, extension, byElement); + String msg = FileRecognitionPanel.checkValidity(error, mimeType, namespace, elementName, extension, byElement); assertEquals(msg, expected, msg != null ? (error.get() ? Result.ERROR : Result.INFO) : Result.VALID); }