# HG changeset patch # Parent 25ac93f3a766a43a63e60837804c2fdeef158ac4 #192595: introduce @EntityCatalogRegistration. diff --git a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/EntityCatalogRegistrationProcessor.java b/openide.filesystems/src/org/netbeans/modules/openide/filesystems/EntityCatalogRegistrationProcessor.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/src/org/netbeans/modules/openide/filesystems/EntityCatalogRegistrationProcessor.java @@ -0,0 +1,111 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.openide.filesystems; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.tools.StandardLocation; +import org.openide.filesystems.EntityCatalogRegistration; +import org.openide.filesystems.EntityCatalogRegistrations; +import org.openide.filesystems.annotations.LayerGeneratingProcessor; +import org.openide.filesystems.annotations.LayerGenerationException; +import org.openide.util.lookup.ServiceProvider; +import org.openide.xml.EntityCatalog; + +@ServiceProvider(service=Processor.class) +@SupportedSourceVersion(SourceVersion.RELEASE_6) +public class EntityCatalogRegistrationProcessor extends LayerGeneratingProcessor { + + public @Override Set getSupportedAnnotationTypes() { + return new HashSet(Arrays.asList( + EntityCatalogRegistration.class.getCanonicalName(), + EntityCatalogRegistrations.class.getCanonicalName())); + } + + protected @Override boolean handleProcess(Set annotations, RoundEnvironment roundEnv) throws LayerGenerationException { + if (roundEnv.processingOver()) { + return false; + } + for (Element e : roundEnv.getElementsAnnotatedWith(EntityCatalogRegistration.class)) { + register(e, e.getAnnotation(EntityCatalogRegistration.class)); + } + for (Element e : roundEnv.getElementsAnnotatedWith(EntityCatalogRegistrations.class)) { + for (EntityCatalogRegistration r :e.getAnnotation(EntityCatalogRegistrations.class).value()) { + register(e, r); + } + } + return true; + } + + private void register(Element e, EntityCatalogRegistration r) throws LayerGenerationException { + String url = r.entity(); + if (!url.startsWith("/")) { + url = "/" + ((PackageElement) e).getQualifiedName().toString().replace('.', '/') + "/" + url; // XXX also handle ../something.dtd sequences? + } + try { // XXX should there be some method in LayerGeneratingProcessor such as loadResource(String name)? + try { + processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", url.substring(1)).openInputStream().close(); + } catch (FileNotFoundException x) { // #181355 + try { + processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", url.substring(1)).openInputStream().close(); + } catch (IOException x2) { + throw x; + } + } + } catch (IOException x) { + throw new LayerGenerationException("Could not open " + url + ": " + x, e); + } + layer(e).file(EntityCatalog.ENTITIES_FOLDER + "/" + EntityCatalog.convertPublicId(r.publicId())).url(url).stringvalue(EntityCatalog.ORIGINAL_PUBLIC_ID, r.publicId()).write(); + } + +} diff --git a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/LayerEntityCatalog.java b/openide.filesystems/src/org/netbeans/modules/openide/filesystems/LayerEntityCatalog.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/src/org/netbeans/modules/openide/filesystems/LayerEntityCatalog.java @@ -0,0 +1,87 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ +package org.netbeans.modules.openide.filesystems; + +import java.io.IOException; +import java.net.URL; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.lookup.ServiceProvider; +import org.openide.xml.EntityCatalog; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Implements standard entity catalog registration. + */ +@ServiceProvider(service = EntityCatalog.class) +public class LayerEntityCatalog extends EntityCatalog { + + public @Override InputSource resolveEntity(String publicID, String systemID) throws SAXException, IOException { + if (publicID == null) { + return null; + } + String id = EntityCatalog.convertPublicId(publicID); + StringBuilder sb = new StringBuilder(200); + sb.append(EntityCatalog.ENTITIES_FOLDER); + sb.append('/'); + sb.append(id); + FileObject fo = FileUtil.getConfigFile(sb.toString()); + if (fo != null) { + InputSource in = new InputSource(fo.getInputStream()); + try { + Object myPublicID = fo.getAttribute(EntityCatalog.ORIGINAL_PUBLIC_ID); + if (myPublicID instanceof String) { + in.setPublicId((String) myPublicID); + } + URL url = fo.getURL(); + in.setSystemId(url.toString()); + } catch (IOException ex) { + // do no care just no system id + } + return in; + } else { + return null; + } + } + +} diff --git a/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistration.java b/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistration.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistration.java @@ -0,0 +1,71 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.openide.filesystems; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.openide.xml.EntityCatalog; + +/** + * Registers an entity for use in {@link EntityCatalog}. + * Normally used to register XML DTDs. + * @see EntityCatalogRegistrations + * @since XXX + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.PACKAGE) +public @interface EntityCatalogRegistration { + + /** + * Public ID of the entity. + */ + String publicId(); + + /** + * Resource location of the entity (absolute, or relative to this package). + */ + String entity(); + +} diff --git a/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistrations.java b/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistrations.java new file mode 100644 --- /dev/null +++ b/openide.filesystems/src/org/openide/filesystems/EntityCatalogRegistrations.java @@ -0,0 +1,62 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.openide.filesystems; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Permits multiple {@link EntityCatalogRegistration}s on a single package. + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.PACKAGE) +public @interface EntityCatalogRegistrations { + + /** + * Registrations. + */ + public EntityCatalogRegistration[] value(); + +} diff --git a/openide.loaders/src/org/netbeans/modules/openide/loaders/FileEntityResolver.java b/openide.loaders/src/org/netbeans/modules/openide/loaders/FileEntityResolver.java --- a/openide.loaders/src/org/netbeans/modules/openide/loaders/FileEntityResolver.java +++ b/openide.loaders/src/org/netbeans/modules/openide/loaders/FileEntityResolver.java @@ -66,7 +66,6 @@ import org.openide.filesystems.FileRenameEvent; import org.openide.filesystems.FileEvent; import org.openide.filesystems.FileAttributeEvent; -import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import org.openide.filesystems.FileUtil; @@ -74,70 +73,18 @@ /** - * Entity resolver which loads entities (typically DTDs) from fixed - * locations in the system file system, according to public ID. - *

- * It expects that PUBLIC has at maximum three "//" parts - * (standard // vendor // entity name // language). It is basically - * converted to "/xml/entities/{vendor}/{entity_name}" resource name. - *

- * It also attaches Environment according to registrations + * Entity resolver which attaches Environment according to registrations * at /xml/lookups/ area. There can be registered: * Environment.Provider or deprecated XMLDataObject.Processor * and XMLDataObject.Info instances. - *

- * All above are core implementation features. - * * @author Jaroslav Tulach */ -@ServiceProviders({@ServiceProvider(service=Environment.Provider.class), @ServiceProvider(service=EntityCatalog.class)}) -public final class FileEntityResolver extends EntityCatalog implements Environment.Provider { - private static final String ENTITY_PREFIX = "/xml/entities"; // NOI18N - private static final String LOOKUP_PREFIX = "/xml/lookups"; // NOI18N +@ServiceProvider(service=Environment.Provider.class) +public final class FileEntityResolver implements Environment.Provider { + private static final String LOOKUP_PREFIX = "/xml/lookups/"; // NOI18N static final Logger ERR = Logger.getLogger(FileEntityResolver.class.getName()); - /** Constructor - */ - public FileEntityResolver() { - } - - /** Tries to find the entity on system file system. - */ - public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException { - if (publicID == null) { - return null; - } - - - String id = convertPublicId (publicID); - - StringBuffer sb = new StringBuffer (200); - sb.append (ENTITY_PREFIX); - sb.append (id); - - FileObject fo = FileUtil.getConfigFile (sb.toString ()); - if (fo != null) { - - // fill in InputSource instance - - InputSource in = new InputSource (fo.getInputStream ()); - try { - Object myPublicID = fo.getAttribute("hint.originalPublicID"); //NOI18N - if (myPublicID instanceof String) { - in.setPublicId((String)myPublicID); - } - URL url = fo.getURL(); - in.setSystemId(url.toString()); // we get nasty nbfs: instead nbres: but it is enough - } catch (IOException ex) { - // do no care just no system id - } - return in; - } else { - return null; - } - } - /** A method that tries to find the correct lookup for given XMLDataObject. * @return the lookup */ @@ -161,7 +108,7 @@ return null; } - id = convertPublicId (id); + id = EntityCatalog.convertPublicId(id); return new Lkp (id, xml); } else if (obj instanceof InstanceDataObject) { @@ -175,7 +122,7 @@ parser.parse(); String id = parser.getPublicId(); if (id == null) return null; - id = convertPublicId (id); + id = EntityCatalog.convertPublicId(id); return new Lkp (id, ido); } @@ -253,69 +200,6 @@ } } - /** Converts the publicID into filesystem friendly name. - *

- * It expects that PUBLIC has at maximum three "//" parts - * (standard // vendor // entity name // language). It is basically - * converted to "vendor/entity_name" resource name. - * - * @see EntityCatalog - */ - @SuppressWarnings("fallthrough") - private static String convertPublicId (String publicID) { - char[] arr = publicID.toCharArray (); - - - int numberofslashes = 0; - int state = 0; - int write = 0; - OUT: for (int i = 0; i < arr.length; i++) { - char ch = arr[i]; - - switch (state) { - case 0: - // initial state - if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') { - // do not write that char - continue; - } - // switch to regular state - state = 1; - // fallthru - case 1: - // regular state expecting any character - if (ch == '/') { - state = 2; - if (++numberofslashes == 3) { - // last part of the ID, exit - break OUT; - } - arr[write++] = '/'; - continue; - } - break; - case 2: - // previous character was / - if (ch == '/') { - // ignore second / and write nothing - continue; - } - state = 1; - break; - } - - // write the char into the array - if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') { - arr[write++] = ch; - } else { - arr[write++] = '_'; - } - } - - return new String (arr, 0, write); - } - - /** Finds a fileobject for given ID. * @param id string id * @param last[0] will be filled with last file object we should listen on diff --git a/openide.loaders/src/org/netbeans/modules/openide/loaders/layer.xml b/openide.loaders/src/org/netbeans/modules/openide/loaders/layer.xml --- a/openide.loaders/src/org/netbeans/modules/openide/loaders/layer.xml +++ b/openide.loaders/src/org/netbeans/modules/openide/loaders/layer.xml @@ -2,18 +2,6 @@ - - - - - - - - - - - - diff --git a/openide.loaders/src/org/netbeans/modules/openide/loaders/package-info.java b/openide.loaders/src/org/netbeans/modules/openide/loaders/package-info.java new file mode 100644 --- /dev/null +++ b/openide.loaders/src/org/netbeans/modules/openide/loaders/package-info.java @@ -0,0 +1,50 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +@EntityCatalogRegistrations({ + @EntityCatalogRegistration(publicId="-//NetBeans//Entity Mapping Registration 1.0//EN", entity="EntityCatalog.dtd"), + @EntityCatalogRegistration(publicId="-//NetBeans IDE//DTD xmlinfo//EN", entity="xmlinfo.dtd") +}) +package org.netbeans.modules.openide.loaders; + +import org.openide.filesystems.EntityCatalogRegistration; +import org.openide.filesystems.EntityCatalogRegistrations; diff --git a/openide.util/src/org/openide/xml/EntityCatalog.java b/openide.util/src/org/openide/xml/EntityCatalog.java --- a/openide.util/src/org/openide/xml/EntityCatalog.java +++ b/openide.util/src/org/openide/xml/EntityCatalog.java @@ -62,21 +62,13 @@ *

You can register your own instances via lookup to add to the resolver pool, * but for reasons of performance and predictability during startup it is best to provide * the entity (e.g. some DTD you define) as the contents of a file in - * the system filesystem, in the /xml/entities/ folder, where the file path - * beneath this folder is based on the public ID as follows: - *

    - *
  1. US-ASCII alphanumeric characters and '_' are left as is. - *
  2. Spaces and various punctuation are converted to '_' (one per character). - *
  3. Initial '-//' is dropped. - *
  4. Final '//EN' is dropped. - *
  5. Exactly two forward slashes in a row are converted to one. - *
+ * the system filesystem, in {@link #ENTITIES_FOLDER}, where the file path + * beneath this folder is based on the public ID as per {@link #convertPublicId}. * Thus for example the public ID -//NetBeans//Entity Mapping Registration 1.0//EN - * would be looked for in the file /xml/entities/NetBeans/Entity_Mapping_Registration_1_0. + * would be looked for in the file xml/entities/NetBeans/Entity_Mapping_Registration_1_0. * Naturally this only works if you are defining a fixed number of entities. *

It is recommended that the entity file in /xml/entities/ also be given a file - * attribute named hint.originalPublicID with a string value giving the public ID. - * This permits {@code org.netbeans.modules.xml.catalog} to display the entry properly. + * attribute named {@link #ORIGINAL_PUBLIC_ID}. * @author Petr Kuzel */ public abstract class EntityCatalog implements EntityResolver { @@ -102,6 +94,90 @@ } /** + * Location where entities are declared. + * @since XXX + */ + public static final String ENTITIES_FOLDER = "xml/entities"; // NOI18N + + /** + * File attribute giving the String public ID for a registration, since {@link #convertPublicId} is not reversible. + * This permits {@code org.netbeans.modules.xml.catalog} to display the entry properly. + * @since XXX + */ + public static final String ORIGINAL_PUBLIC_ID = "hint.originalPublicID"; // NOI18N + + /** + * Converts a public ID into a layer-friendly name. + * It expects that the ID has at most three "//" divisions + * (standard // vendor // entity name // language). It is basically + * converted to "vendor/entity_name" resource name. Specifically: + *

    + *
  1. US-ASCII alphanumeric characters and '_' are left as is. + *
  2. Spaces and various punctuation are converted to '_' (one per character). + *
  3. Initial '-//' is dropped. + *
  4. Final '//EN' is dropped. + *
  5. Exactly two forward slashes in a row are converted to one. + *
+ * @param publicID e.g. {@code -//NetBeans//Entity Mapping Registration 1.0//EN} + * @return e.g. {@code NetBeans/Entity_Mapping_Registration_1_0} + * @since XXX + */ + @SuppressWarnings("fallthrough") + public static String convertPublicId(String publicID) { + char[] arr = publicID.toCharArray (); + + + int numberofslashes = 0; + int state = 0; + int write = 0; + OUT: for (int i = 0; i < arr.length; i++) { + char ch = arr[i]; + + switch (state) { + case 0: + // initial state + if (ch == '+' || ch == '-' || ch == 'I' || ch == 'S' || ch == 'O') { + // do not write that char + continue; + } + // switch to regular state + state = 1; + // fallthru + case 1: + // regular state expecting any character + if (ch == '/') { + state = 2; + if (++numberofslashes == 3) { + // last part of the ID, exit + break OUT; + } + arr[write++] = '/'; + continue; + } + break; + case 2: + // previous character was / + if (ch == '/') { + // ignore second / and write nothing + continue; + } + state = 1; + break; + } + + // write the char into the array + if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') { + arr[write++] = ch; + } else { + arr[write++] = '_'; + } + } + + boolean startsWithSlash = arr[0] == '/'; + return new String (arr, startsWithSlash ? 1 : 0, startsWithSlash ? write - 1 : write); + } + + /** * This catalog is forwarding implementation. */ private static class Forwarder extends EntityCatalog { diff --git a/openide.util/test/unit/src/org/openide/xml/EntityCatalogTest.java b/openide.util/test/unit/src/org/openide/xml/EntityCatalogTest.java new file mode 100644 --- /dev/null +++ b/openide.util/test/unit/src/org/openide/xml/EntityCatalogTest.java @@ -0,0 +1,58 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.openide.xml; + +import org.netbeans.junit.NbTestCase; + +public class EntityCatalogTest extends NbTestCase { + + public EntityCatalogTest(String n) { + super(n); + } + + public void testConvertPublicId() throws Exception { + assertEquals("NetBeans/Entity_Mapping_Registration_1_0", EntityCatalog.convertPublicId("-//NetBeans//Entity Mapping Registration 1.0//EN")); + assertEquals("DTD_XMLCatalog/EN", EntityCatalog.convertPublicId("-//DTD XMLCatalog//EN")); // weird but needed for compat + } + +} diff --git a/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/SystemCatalogReader.java b/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/SystemCatalogReader.java --- a/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/SystemCatalogReader.java +++ b/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/SystemCatalogReader.java @@ -84,12 +84,12 @@ // inspect system/xml/entities - FileObject root = FileUtil.getConfigFile("xml/entities"); + FileObject root = FileUtil.getConfigFile(EntityCatalog.ENTITIES_FOLDER); Enumeration en = root.getChildren(true); while (en.hasMoreElements()) { FileObject next = (FileObject) en.nextElement(); if (next.isData()) { - Object hint = next.getAttribute("hint.originalPublicID"); + Object hint = next.getAttribute(EntityCatalog.ORIGINAL_PUBLIC_ID); if (hint instanceof String) { set.add(hint); found = true; diff --git a/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/package-info.java b/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/package-info.java new file mode 100644 --- /dev/null +++ b/xml.catalog/src/org/netbeans/modules/xml/catalog/impl/package-info.java @@ -0,0 +1,50 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +@EntityCatalogRegistrations({ + @EntityCatalogRegistration(publicId="-//DTD XMLCatalog//EN", entity="XCatalog-0_4.dtd"), + @EntityCatalogRegistration(publicId="-//DTD XCatalog//EN", entity="XCatalog-0_2.dtd") +}) +package org.netbeans.modules.xml.catalog.impl; + +import org.openide.filesystems.EntityCatalogRegistration; +import org.openide.filesystems.EntityCatalogRegistrations; diff --git a/xml.catalog/src/org/netbeans/modules/xml/catalog/resources/mf-layer.xml b/xml.catalog/src/org/netbeans/modules/xml/catalog/resources/mf-layer.xml --- a/xml.catalog/src/org/netbeans/modules/xml/catalog/resources/mf-layer.xml +++ b/xml.catalog/src/org/netbeans/modules/xml/catalog/resources/mf-layer.xml @@ -92,20 +92,8 @@ - - - - - - - - - - - - - +