001: package org.apache.maven.embedder.writer;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.maven.model.Model;
023: import org.apache.maven.model.io.jdom.MavenJDOMWriter;
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.Namespace;
027: import org.jdom.output.Format;
028:
029: import java.io.IOException;
030: import java.io.Writer;
031:
032: //TODO: turn this into a component
033:
034: /** @author mkleint@codehaus.org */
035: public class WriterUtils {
036: public static void write(Writer w, Model newModel)
037: throws IOException {
038: write(w, newModel, false);
039: }
040:
041: public static void write(Writer w, Model newModel,
042: boolean namespaceDeclaration) throws IOException {
043: Element root = new Element("project");
044:
045: if (namespaceDeclaration) {
046: String modelVersion = newModel.getModelVersion();
047:
048: Namespace pomNamespace = Namespace.getNamespace("",
049: "http://maven.apache.org/POM/" + modelVersion);
050:
051: root.setNamespace(pomNamespace);
052:
053: Namespace xsiNamespace = Namespace.getNamespace("xsi",
054: "http://www.w3.org/2001/XMLSchema-instance");
055:
056: root.addNamespaceDeclaration(xsiNamespace);
057:
058: if (root.getAttribute("schemaLocation", xsiNamespace) == null) {
059: root.setAttribute("schemaLocation",
060: "http://maven.apache.org/POM/" + modelVersion
061: + " http://maven.apache.org/maven-v"
062: + modelVersion.replace('.', '_')
063: + ".xsd", xsiNamespace);
064: }
065: }
066:
067: Document doc = new Document(root);
068:
069: MavenJDOMWriter writer = new MavenJDOMWriter();
070:
071: String encoding = newModel.getModelEncoding() != null ? newModel
072: .getModelEncoding()
073: : "UTF-8";
074:
075: Format format = Format.getPrettyFormat().setEncoding(encoding);
076:
077: writer.write(newModel, doc, w, format);
078: }
079:
080: /*
081: public static void writePomModel( FileObject pom,
082: Model newModel )
083: throws IOException
084: {
085: InputStream inStr = null;
086:
087: FileLock lock = null;
088:
089: OutputStreamWriter outStr = null;
090:
091: try
092: {
093: inStr = pom.getInputStream();
094:
095: SAXBuilder builder = new SAXBuilder();
096:
097: Document doc = builder.build( inStr );
098:
099: inStr.close();
100:
101: inStr = null;
102:
103: lock = pom.lock();
104:
105: MavenJDOMWriter writer = new MavenJDOMWriter();
106:
107: String encoding = newModel.getModelEncoding() != null ? newModel.getModelEncoding() : "UTF-8";
108:
109: outStr = new OutputStreamWriter( pom.getOutputStream( lock ), encoding );
110:
111: Format form = Format.getRawFormat().setEncoding( encoding );
112:
113: writer.write( newModel, doc, outStr, form );
114:
115: outStr.close();
116:
117: outStr = null;
118: }
119: catch ( JDOMException exc )
120: {
121: exc.printStackTrace();
122: throw (IOException) new IOException( "Cannot parse the POM by JDOM." ).initCause( exc );
123: }
124: finally
125: {
126: IOUtil.close( inStr );
127:
128: IOUtil.close( outStr );
129:
130: if ( lock != null )
131: {
132: lock.releaseLock();
133: }
134:
135: }
136: }
137: */
138: }
|