Permalink
Browse files

o Added rewritten Xpp3Dom

Original of Xpp3DomWriter is Brett Porter, added with his consent

git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1400716 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent ad640dd commit eb07be61f7b5ab0a25881ef62a74d29d5d0f91b2 @krosenvold krosenvold committed Oct 21, 2012
View
@@ -10,3 +10,4 @@ target
build
.svn/
.DS_Store
+dependency-reduced-pom.xml
@@ -300,6 +300,8 @@ private void writeDocumentHeader()
writer.write( "?>" );
+ newLine();
+
if ( docType != null )
{
newLine();
@@ -0,0 +1,205 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.shared.utils.io.IOUtil;
+import org.apache.maven.shared.utils.xml.pull.XmlPullParserException;
+import org.apache.maven.shared.utils.xml.pull.Xpp3Dom;
+import org.xml.sax.*;
+import org.xml.sax.helpers.DefaultHandler;
+
+import javax.annotation.Nonnull;
+import javax.annotation.WillClose;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Kristian Rosenvold
+ */
+public class Xpp3DomBuilder
+{
+ private static final boolean DEFAULT_TRIM = true;
+
+ public static Xpp3Dom build( @WillClose @Nonnull Reader reader )
+ throws XmlPullParserException
+ {
+ return build( reader, DEFAULT_TRIM );
+ }
+
+ public static Xpp3Dom build( @WillClose InputStream is, @Nonnull String encoding )
+ throws XmlPullParserException
+ {
+ return build( is, encoding, DEFAULT_TRIM );
+ }
+
+ public static Xpp3Dom build( @WillClose InputStream is, @Nonnull String encoding, boolean trim )
+ throws XmlPullParserException
+ {
+ try {
+ Reader reader = new InputStreamReader(is, encoding);
+ return build( reader, trim);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Xpp3Dom build( @WillClose Reader reader, boolean trim )
+ throws XmlPullParserException
+ {
+ try
+ {
+ DocHandler docHandler = parseSax(new InputSource(reader), trim);
+ return docHandler.result;
+ }
+ finally
+ {
+ IOUtil.close( reader );
+ }
+ }
+
+ private static DocHandler parseSax(@Nonnull InputSource inputSource, boolean trim) throws XmlPullParserException {
+
+ try {
+ DocHandler ch = new DocHandler(trim);
+ XMLReader parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
+ parser.setContentHandler( ch);
+ parser.parse(inputSource);
+ return ch;
+ } catch (IOException e){
+ throw new XmlPullParserException(e);
+ } catch (SAXException e) {
+ throw new XmlPullParserException(e);
+ }
+ }
+
+
+ private static class DocHandler extends DefaultHandler {
+ private final List<Xpp3Dom> elemStack = new ArrayList<Xpp3Dom>();
+ private final List<StringBuilder> values = new ArrayList<StringBuilder>();
+
+ // Todo: Use these for something smart !
+ private final List<SAXParseException> warnings = new ArrayList<SAXParseException>();
+ private final List<SAXParseException> errors = new ArrayList<SAXParseException>();
+ private final List<SAXParseException> fatals = new ArrayList<SAXParseException>();
+
+
+ Xpp3Dom result = null;
+
+ private final boolean trim;
+
+ DocHandler(boolean trim) {
+ this.trim = trim;
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+
+ Xpp3Dom child = new Xpp3Dom( localName );
+
+ attachToParent(child);
+ pushOnStack(child);
+
+ // Todo: Detecting tags that close immediately seem to be impossible in sax ?
+ // http://stackoverflow.com/questions/12968390/detecting-self-closing-tags-in-sax
+ values.add( new StringBuilder() );
+
+ int size = attributes.getLength();
+ for ( int i = 0; i < size; i++ )
+ {
+ child.setAttribute(attributes.getQName(i), attributes.getValue(i));
+ }
+ }
+
+ private boolean pushOnStack(Xpp3Dom child) {
+ return elemStack.add(child);
+ }
+
+ private void attachToParent(Xpp3Dom child) {
+ int depth = elemStack.size();
+ if ( depth > 0 )
+ {
+ elemStack.get(depth - 1).addChild(child);
+ }
+ }
+
+ @Override
+ public void warning(SAXParseException e) throws SAXException {
+ warnings.add(e);
+ }
+
+ @Override
+ public void error(SAXParseException e) throws SAXException {
+ errors.add(e);
+ }
+
+ @Override
+ public void fatalError(SAXParseException e) throws SAXException {
+ fatals.add(e);
+ }
+
+
+ private Xpp3Dom pop()
+ {
+ int depth = elemStack.size() - 1;
+ return elemStack.remove( depth );
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ int depth = elemStack.size() - 1;
+
+ Xpp3Dom finishedConfiguration = pop();
+
+ /* this Object could be null if it is a singleton tag */
+ Object accumulatedValue = values.remove( depth );
+
+ if ( finishedConfiguration.getChildCount() == 0 )
+ {
+ if ( accumulatedValue == null )
+ {
+ finishedConfiguration.setValue( null );
+ }
+ else
+ {
+ finishedConfiguration.setValue( accumulatedValue.toString() );
+ }
+ }
+
+ if ( depth == 0 )
+ {
+ result = finishedConfiguration;
+ }
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ String text = new String(ch, start, length);
+ appendToTopValue(trim ? text.trim() : text);
+ }
+
+ private void appendToTopValue(String toAppend) {
+ //noinspection MismatchedQueryAndUpdateOfStringBuilder
+ StringBuilder stringBuilder = values.get(values.size() - 1);
+ stringBuilder.append( toAppend);
+ }
+ }
+
+}
@@ -0,0 +1,75 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.shared.utils.xml.pull.Xpp3Dom;
+
+import java.io.PrintWriter;
+import java.io.Writer;
+
+/**
+ * @author Brett Porter
+ */
+public class Xpp3DomWriter
+{
+ public static void write( Writer writer, Xpp3Dom dom )
+ {
+ write( new PrettyPrintXMLWriter( writer ), dom );
+ }
+
+ public static void write( PrintWriter writer, Xpp3Dom dom )
+ {
+ write( new PrettyPrintXMLWriter( writer ), dom );
+ }
+
+ public static void write( PrettyPrintXMLWriter xmlWriter, Xpp3Dom dom )
+ {
+ write( xmlWriter, dom, true );
+ }
+
+ public static void write( PrettyPrintXMLWriter xmlWriter, Xpp3Dom dom, boolean escape )
+ {
+ xmlWriter.startElement( dom.getName() );
+ String[] attributeNames = dom.getAttributeNames();
+ for (String attributeName : attributeNames) {
+ xmlWriter.addAttribute(attributeName, dom.getAttribute(attributeName));
+ }
+ Xpp3Dom[] children = dom.getChildren();
+ for (Xpp3Dom aChildren : children) {
+ write(xmlWriter, aChildren, escape);
+ }
+
+ String value = dom.getValue();
+ if ( value != null )
+ {
+ if ( escape )
+ {
+ xmlWriter.writeText( value );
+ }
+ else
+ {
+ xmlWriter.writeMarkup( value );
+ }
+ }
+ xmlWriter.endElement();
+ }
+
+
+}
@@ -0,0 +1,36 @@
+package org.apache.maven.shared.utils.xml.pull;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.xml.sax.SAXException;
+
+import java.io.IOException;
+
+public class XmlPullParserException extends RuntimeException {
+
+ public XmlPullParserException(IOException e) {
+ super(e);
+ }
+
+ public XmlPullParserException(SAXException e) {
+ super(e);
+ }
+}
+
Oops, something went wrong.

0 comments on commit eb07be6

Please sign in to comment.