Search in sources :

Example 1 with DTD

use of javax.swing.text.html.parser.DTD in project camel by apache.

the class JavadocParserTest method testGetMethods.

@Test
public void testGetMethods() throws Exception {
    final DTD dtd = DTD.getDTD("html.dtd");
    final String javaDocPath = String.class.getName().replaceAll("\\.", "/") + ".html";
    final JavadocParser htmlParser = new JavadocParser(dtd, javaDocPath);
    htmlParser.parse(new InputStreamReader(new URL(JAVA6_STRING).openStream(), "UTF-8"));
    assertNull("Java6 getErrorMessage", htmlParser.getErrorMessage());
    assertEquals("Java6 getMethods", 65, htmlParser.getMethods().size());
    htmlParser.reset();
    htmlParser.parse(new InputStreamReader(new URL(JAVA7_STRING).openStream(), "UTF-8"));
    assertNull("Java7 getErrorMessage", htmlParser.getErrorMessage());
    assertEquals("Java7 getMethods", 65, htmlParser.getMethods().size());
    htmlParser.reset();
    htmlParser.parse(new InputStreamReader(new URL(JAVA8_STRING).openStream(), "UTF-8"));
    assertNull("Java8 getErrorMessage", htmlParser.getErrorMessage());
    assertEquals("Java8 getMethods", 67, htmlParser.getMethods().size());
}
Also used : InputStreamReader(java.io.InputStreamReader) DTD(javax.swing.text.html.parser.DTD) URL(java.net.URL) Test(org.junit.Test)

Example 2 with DTD

use of javax.swing.text.html.parser.DTD in project jdk8u_jdk by JetBrains.

the class Test8017492 method main.

public static void main(String[] args) throws Exception {
    Runnable task = new Runnable() {

        @Override
        public void run() {
            try {
                SunToolkit.createNewAppContext();
                DTD dtd = DTD.getDTD("dtd");
                dtd.elements = new Vector<Element>() {

                    @Override
                    public synchronized int size() {
                        return Integer.MAX_VALUE;
                    }
                };
                dtd.getElement("element");
            } catch (Exception exception) {
                throw new Error("unexpected", exception);
            }
        }
    };
    // run task with different AppContext
    Thread thread = new Thread(new ThreadGroup("$$$"), task);
    thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            throwable.printStackTrace();
            System.exit(1);
        }
    });
    thread.start();
    thread.join();
    // add error handling
    HTMLDocument document = new HTMLDocument() {

        @Override
        public HTMLEditorKit.ParserCallback getReader(int pos) {
            return getReader(pos, 0, 0, null);
        }

        @Override
        public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, int pushDepth, HTML.Tag insertTag) {
            return new HTMLDocument.HTMLReader(pos, popDepth, pushDepth, insertTag) {

                @Override
                public void handleError(String error, int pos) {
                    throw new Error(error);
                }
            };
        }
    };
    // run parser
    new HTMLEditorKit().insertHTML(document, 0, "<html><body>text", 0, 0, null);
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.html.parser.Element) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) DTD(javax.swing.text.html.parser.DTD)

Example 3 with DTD

use of javax.swing.text.html.parser.DTD in project camel by apache.

the class JavadocApiMethodGeneratorMojo method getSignatureList.

@Override
public List<String> getSignatureList() throws MojoExecutionException {
    // signatures as map from signature with no arg names to arg names from JavadocParser
    Map<String, String> result = new HashMap<String, String>();
    final Pattern packagePatterns = Pattern.compile(excludePackages);
    final Pattern classPatterns = (excludeClasses != null) ? Pattern.compile(excludeClasses) : null;
    final Pattern includeMethodPatterns = (includeMethods != null) ? Pattern.compile(includeMethods) : null;
    final Pattern excludeMethodPatterns = (excludeMethods != null) ? Pattern.compile(excludeMethods) : null;
    // for proxy class and super classes not matching excluded packages or classes
    for (Class<?> aClass = getProxyType(); aClass != null && !packagePatterns.matcher(aClass.getPackage().getName()).matches() && (classPatterns == null || !classPatterns.matcher(aClass.getSimpleName()).matches()); aClass = aClass.getSuperclass()) {
        log.debug("Processing " + aClass.getName());
        final String javaDocPath = aClass.getName().replaceAll("\\.", "/").replace('$', '.') + ".html";
        // read javadoc html text for class
        InputStream inputStream = null;
        try {
            inputStream = getProjectClassLoader().getResourceAsStream(javaDocPath);
            if (inputStream == null) {
                log.debug("JavaDoc not found on classpath for " + aClass.getName());
                break;
            }
            // transform the HTML to get method summary as text
            // dummy DTD
            final DTD dtd = DTD.getDTD("html.dtd");
            final JavadocParser htmlParser = new JavadocParser(dtd, javaDocPath);
            htmlParser.parse(new InputStreamReader(inputStream, "UTF-8"));
            // look for parse errors
            final String parseError = htmlParser.getErrorMessage();
            if (parseError != null) {
                throw new MojoExecutionException(parseError);
            }
            // get public method signature
            final Map<String, String> methodMap = htmlParser.getMethodText();
            for (String method : htmlParser.getMethods()) {
                if (!result.containsKey(method) && (includeMethodPatterns == null || includeMethodPatterns.matcher(method).find()) && (excludeMethodPatterns == null || !excludeMethodPatterns.matcher(method).find())) {
                    final int leftBracket = method.indexOf('(');
                    final String name = method.substring(0, leftBracket);
                    final String args = method.substring(leftBracket + 1, method.length() - 1);
                    String[] types;
                    if (args.isEmpty()) {
                        types = new String[0];
                    } else {
                        // get raw types from args
                        final List<String> rawTypes = new ArrayList<String>();
                        final Matcher argTypesMatcher = RAW_ARGTYPES_PATTERN.matcher(args);
                        while (argTypesMatcher.find()) {
                            rawTypes.add(argTypesMatcher.group(1));
                        }
                        types = rawTypes.toArray(new String[rawTypes.size()]);
                    }
                    final String resultType = getResultType(aClass, name, types);
                    if (resultType != null) {
                        result.put(method, resultType + " " + name + methodMap.get(method));
                    }
                }
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } finally {
            IOUtil.close(inputStream);
        }
    }
    if (result.isEmpty()) {
        throw new MojoExecutionException("No public non-static methods found, " + "make sure Javadoc is available as project test dependency");
    }
    return new ArrayList<String>(result.values());
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DTD(javax.swing.text.html.parser.DTD)

Example 4 with DTD

use of javax.swing.text.html.parser.DTD in project jdk8u_jdk by JetBrains.

the class bug8074956 method main.

public static void main(String[] args) throws Exception {
    final DTD html32 = DTD.getDTD("html32");
    ContentModel contentModel = new ContentModel('&', new ContentModel());
    Element elem1 = html32.getElement("html-element");
    contentModel.first(elem1);
    Element elem2 = html32.getElement("test-element");
    // Shouldn't throw ArrayIndexOutOfBoundsException
    contentModel.first(elem2);
}
Also used : ContentModel(javax.swing.text.html.parser.ContentModel) DTD(javax.swing.text.html.parser.DTD) Element(javax.swing.text.html.parser.Element)

Example 5 with DTD

use of javax.swing.text.html.parser.DTD in project jdk8u_jdk by JetBrains.

the class bug6938813 method validate.

private static void validate() throws Exception {
    AppContext appContext = AppContext.getAppContext();
    assertTrue(DTD.getDTD(DTD_KEY).getName().equals(DTD_KEY), "DTD.getDTD() mixed AppContexts");
    // Spoil hash value
    DTD invalidDtd = DTD.getDTD("invalid DTD");
    DTD.putDTDHash(DTD_KEY, invalidDtd);
    assertTrue(DTD.getDTD(DTD_KEY) == invalidDtd, "Something wrong with DTD.getDTD()");
    Object dtdKey = getParserDelegator_DTD_KEY();
    assertTrue(appContext.get(dtdKey) == null, "ParserDelegator mixed AppContexts");
    // Init default DTD
    new ParserDelegator();
    Object dtdValue = appContext.get(dtdKey);
    assertTrue(dtdValue != null, "ParserDelegator.defaultDTD isn't initialized");
    // Try reinit default DTD
    new ParserDelegator();
    assertTrue(dtdValue == appContext.get(dtdKey), "ParserDelegator.defaultDTD created a duplicate");
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
    if (styleSheet == null) {
        // First AppContext
        styleSheet = htmlEditorKit.getStyleSheet();
        assertTrue(styleSheet != null, "htmlEditorKit.getStyleSheet() returns null");
        assertTrue(htmlEditorKit.getStyleSheet() == styleSheet, "Something wrong with htmlEditorKit.getStyleSheet()");
    } else {
        assertTrue(htmlEditorKit.getStyleSheet() != styleSheet, "HtmlEditorKit.getStyleSheet() mixed AppContexts");
    }
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) DTD(javax.swing.text.html.parser.DTD) AppContext(sun.awt.AppContext) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Aggregations

DTD (javax.swing.text.html.parser.DTD)5 InputStreamReader (java.io.InputStreamReader)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 Element (javax.swing.text.html.parser.Element)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 HTMLDocument (javax.swing.text.html.HTMLDocument)1 ContentModel (javax.swing.text.html.parser.ContentModel)1 ParserDelegator (javax.swing.text.html.parser.ParserDelegator)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 Test (org.junit.Test)1 AppContext (sun.awt.AppContext)1